#region License Information
/* HeuristicLab
* Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using HeuristicLab.PluginInfrastructure;
using System.Net;
using System.ServiceModel;
using HeuristicLab.CEDMA.DB.Interfaces;
using HeuristicLab.CEDMA.DB;
using System.ServiceModel.Description;
using System.Linq;
using HeuristicLab.CEDMA.Core;
using HeuristicLab.GP.StructureIdentification;
using HeuristicLab.Data;
using HeuristicLab.Grid;
using System.Diagnostics;
using HeuristicLab.Core;
using System.Threading;
namespace HeuristicLab.CEDMA.Server {
public class Executer {
private static int MaxActiveJobs {
get { return 100; }
}
private Dispatcher dispatcher;
private JobManager jobManager;
private IStore store;
public Executer(Dispatcher dispatcher, IStore store, string gridUrl) {
this.dispatcher = dispatcher;
this.store = store;
this.jobManager = new JobManager(gridUrl);
jobManager.Reset();
}
internal void Start() {
new Thread(StartJobs).Start();
}
private void StartJobs() {
List wh = new List();
Dictionary activeOperations = new Dictionary();
Dictionary activeExecutions = new Dictionary();
while (true) {
try {
// start new jobs as long as there are less than MaxActiveJobs
while (wh.Count < MaxActiveJobs) {
// get an execution from the dispatcher and execute in grid via job-manager
Execution execution = dispatcher.GetNextJob();
if (execution != null) {
AtomicOperation op = new AtomicOperation(execution.Engine.OperatorGraph.InitialOperator, execution.Engine.GlobalScope);
WaitHandle opWh = jobManager.BeginExecuteOperation(execution.Engine.GlobalScope, op);
wh.Add(opWh);
activeOperations.Add(opWh, op);
activeExecutions.Add(opWh, execution);
}
}
// wait until any job is finished
WaitHandle[] whArr = wh.ToArray();
int readyHandleIndex = WaitHandle.WaitAny(whArr);
WaitHandle readyHandle = whArr[readyHandleIndex];
AtomicOperation finishedOp = activeOperations[readyHandle];
Execution finishedExecution = activeExecutions[readyHandle];
wh.Remove(readyHandle);
activeExecutions.Remove(readyHandle);
activeOperations.Remove(readyHandle);
ProcessingEngine finishedEngine = null;
try {
finishedEngine = jobManager.EndExecuteOperation(finishedOp);
} catch (Exception badEx) {
Trace.WriteLine("CEDMA Executer: Exception in job execution thread. " + badEx.Message);
}
if (finishedEngine != null) {
StoreResults(finishedExecution, finishedEngine);
}
}
catch (Exception ex) {
Trace.WriteLine("CEDMA Executer: Exception in job-management thread. " + ex.Message);
}
}
}
private void StoreResults(Execution finishedExecution, ProcessingEngine finishedEngine) {
Entity model = new Entity(Ontology.CedmaNameSpace + Guid.NewGuid());
store.Add(new Statement(model, Ontology.PredicateInstanceOf, Ontology.TypeGeneticProgrammingFunctionTree));
store.Add(new Statement(finishedExecution.DataSetEntity, Ontology.PredicateHasModel, model));
StoreModelAttribute(model, Ontology.TargetVariable, finishedExecution.TargetVariable);
Scope bestModelScope = finishedEngine.GlobalScope.GetVariableValue("BestValidationSolution", false);
StoreModelAttribute(model, Ontology.TrainingMeanSquaredError, bestModelScope.GetVariableValue("Quality", false).Data);
StoreModelAttribute(model, Ontology.ValidationMeanSquaredError, bestModelScope.GetVariableValue("ValidationQuality", false).Data);
StoreModelAttribute(model, Ontology.TrainingMeanAbsolutePercentageError, bestModelScope.GetVariableValue("TrainingMAPE", false).Data);
StoreModelAttribute(model, Ontology.ValidationMeanAbsolutePercentageError, bestModelScope.GetVariableValue("ValidationMAPE", false).Data);
StoreModelAttribute(model, Ontology.TreeSize, bestModelScope.GetVariableValue("TreeSize", false).Data);
StoreModelAttribute(model, Ontology.TreeHeight, bestModelScope.GetVariableValue("TreeHeight", false).Data);
StoreModelAttribute(model, Ontology.EvaluatedSolutions, bestModelScope.GetVariableValue("EvaluatedSolutions", false).Data);
byte[] serializedModel = PersistenceManager.SaveToGZip(bestModelScope.GetVariableValue("FunctionTree", false));
store.Add(new Statement(model, Ontology.PredicateSerializedData, new Literal(Convert.ToBase64String(serializedModel))));
}
private void StoreModelAttribute(Entity model, Entity predicate, object value) {
store.Add(new Statement(model, predicate, new Literal(value)));
}
}
}