[1060] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using System.Windows.Forms;
|
---|
| 26 | using HeuristicLab.PluginInfrastructure;
|
---|
| 27 | using System.Net;
|
---|
| 28 | using System.ServiceModel;
|
---|
| 29 | using HeuristicLab.CEDMA.DB.Interfaces;
|
---|
| 30 | using HeuristicLab.CEDMA.DB;
|
---|
| 31 | using System.ServiceModel.Description;
|
---|
| 32 | using System.Linq;
|
---|
| 33 | using HeuristicLab.CEDMA.Core;
|
---|
| 34 | using HeuristicLab.GP.StructureIdentification;
|
---|
| 35 | using HeuristicLab.Data;
|
---|
| 36 | using HeuristicLab.Grid;
|
---|
| 37 | using System.Diagnostics;
|
---|
| 38 | using HeuristicLab.Core;
|
---|
| 39 | using System.Threading;
|
---|
| 40 |
|
---|
| 41 | namespace HeuristicLab.CEDMA.Server {
|
---|
| 42 | public class Executer {
|
---|
| 43 | private static int MaxActiveJobs {
|
---|
| 44 | get { return 20; }
|
---|
| 45 | }
|
---|
| 46 | private Dispatcher dispatcher;
|
---|
| 47 | private JobManager jobManager;
|
---|
| 48 | private IStore store;
|
---|
| 49 |
|
---|
| 50 | public Executer(Dispatcher dispatcher, IStore store, string gridUrl) {
|
---|
| 51 | this.dispatcher = dispatcher;
|
---|
| 52 | this.store = store;
|
---|
| 53 | this.jobManager = new JobManager(gridUrl);
|
---|
| 54 | jobManager.Reset();
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | internal void Start() {
|
---|
| 58 | new Thread(StartJobs).Start();
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | private void StartJobs() {
|
---|
| 62 | List<WaitHandle> wh = new List<WaitHandle>();
|
---|
| 63 | Dictionary<WaitHandle, AtomicOperation> activeOperations = new Dictionary<WaitHandle,AtomicOperation>();
|
---|
| 64 | Dictionary<WaitHandle, Execution> activeExecutions = new Dictionary<WaitHandle,Execution>();
|
---|
| 65 | while (true) {
|
---|
| 66 | try {
|
---|
| 67 | // start new jobs as long as there are less than MaxActiveJobs
|
---|
| 68 | while (wh.Count < MaxActiveJobs) {
|
---|
| 69 | // get an execution from the dispatcher and execute in grid via job-manager
|
---|
| 70 | Execution execution = dispatcher.GetNextJob();
|
---|
[1072] | 71 | if (execution != null) {
|
---|
| 72 | AtomicOperation op = new AtomicOperation(execution.Engine.OperatorGraph.InitialOperator, execution.Engine.GlobalScope);
|
---|
| 73 | WaitHandle opWh = jobManager.BeginExecuteOperation(execution.Engine.GlobalScope, op);
|
---|
| 74 | wh.Add(opWh);
|
---|
| 75 | activeOperations.Add(opWh, op);
|
---|
| 76 | activeExecutions.Add(opWh, execution);
|
---|
| 77 | }
|
---|
[1060] | 78 | }
|
---|
| 79 | // wait until any job is finished
|
---|
| 80 | WaitHandle[] whArr = wh.ToArray();
|
---|
| 81 | int readyHandleIndex = WaitHandle.WaitAny(whArr);
|
---|
| 82 | WaitHandle readyHandle = whArr[readyHandleIndex];
|
---|
| 83 | AtomicOperation finishedOp = activeOperations[readyHandle];
|
---|
| 84 | Execution finishedExecution = activeExecutions[readyHandle];
|
---|
| 85 | wh.Remove(readyHandle);
|
---|
| 86 | activeExecutions.Remove(readyHandle);
|
---|
| 87 | activeOperations.Remove(readyHandle);
|
---|
| 88 | ProcessingEngine finishedEngine = null;
|
---|
| 89 | try {
|
---|
| 90 | finishedEngine = jobManager.EndExecuteOperation(finishedOp);
|
---|
| 91 | } catch (Exception badEx) {
|
---|
| 92 | Trace.WriteLine("CEDMA Executer: Exception in job execution thread. " + badEx.Message);
|
---|
| 93 | }
|
---|
| 94 | if (finishedEngine != null) {
|
---|
[1072] | 95 | StoreResults(finishedExecution, finishedEngine);
|
---|
[1060] | 96 | }
|
---|
| 97 | }
|
---|
| 98 | catch (Exception ex) {
|
---|
| 99 | Trace.WriteLine("CEDMA Executer: Exception in job-management thread. " + ex.Message);
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
[1072] | 103 |
|
---|
| 104 | private void StoreResults(Execution finishedExecution, ProcessingEngine finishedEngine) {
|
---|
| 105 | Entity modelEntity = new Entity(Ontology.CedmaNameSpace + Guid.NewGuid());
|
---|
| 106 | store.Add(new Statement(modelEntity, Ontology.PredicateInstanceOf, Ontology.TypeGeneticProgrammingFunctionTree));
|
---|
| 107 | store.Add(new Statement(finishedExecution.DataSetEntity, Ontology.PredicateHasModel, modelEntity));
|
---|
[1115] | 108 | StoreModelAttribute(modelEntity, "TargetVariable", finishedExecution.TargetVariable);
|
---|
| 109 | Scope bestModelScope = finishedEngine.GlobalScope.GetVariableValue<Scope>("BestValidationSolution", false);
|
---|
| 110 | StoreModelAttribute(modelEntity, "TrainingMeanSquaredError", bestModelScope.GetVariableValue<DoubleData>("Quality", false).Data);
|
---|
| 111 | StoreModelAttribute(modelEntity, "ValidationMeanSquaredError", bestModelScope.GetVariableValue<DoubleData>("ValidationQuality", false).Data);
|
---|
| 112 | StoreModelAttribute(modelEntity, "TrainingMeanAbsolutePercentageError", bestModelScope.GetVariableValue<DoubleData>("TrainingMAPE", false).Data);
|
---|
| 113 | StoreModelAttribute(modelEntity, "ValidationMeanAbsolutePercentageError", bestModelScope.GetVariableValue<DoubleData>("ValidationMAPE", false).Data);
|
---|
| 114 | StoreModelAttribute(modelEntity, "TreeSize", bestModelScope.GetVariableValue<IntData>("TreeSize", false).Data);
|
---|
| 115 | StoreModelAttribute(modelEntity, "TreeHeight", bestModelScope.GetVariableValue<IntData>("TreeHeight", false).Data);
|
---|
| 116 | StoreModelAttribute(modelEntity, "EvaluatedSolutions", bestModelScope.GetVariableValue<IntData>("EvaluatedSolutions", false).Data);
|
---|
| 117 |
|
---|
| 118 | byte[] serializedModel = PersistenceManager.SaveToGZip(bestModelScope.GetVariableValue("FunctionTree", false));
|
---|
[1116] | 119 | store.Add(new Statement(modelEntity, Ontology.PredicateSerializedData, new Literal(Convert.ToBase64String(serializedModel))));
|
---|
[1072] | 120 |
|
---|
| 121 | }
|
---|
[1115] | 122 |
|
---|
| 123 | private void StoreModelAttribute(Entity model, string name, object value) {
|
---|
| 124 | Entity attr = new Entity(Ontology.CedmaNameSpace + Guid.NewGuid());
|
---|
| 125 | store.Add(new Statement(model, Ontology.PredicateModelAttribute, attr));
|
---|
| 126 | store.Add(new Statement(attr, Ontology.PredicateModelAttributeName, new Literal(name)));
|
---|
| 127 | store.Add(new Statement(attr, Ontology.PredicateModelAttributeValue, new Literal(value)));
|
---|
| 128 | store.Add(new Statement(attr, Ontology.PredicateModelAttributeType, Ontology.TypeOrdinalAttribute));
|
---|
| 129 | }
|
---|
[1060] | 130 | }
|
---|
| 131 | }
|
---|