Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.CEDMA.Server/3.3/Executer.cs @ 6347

Last change on this file since 6347 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

File size: 8.6 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Windows.Forms;
26using HeuristicLab.PluginInfrastructure;
27using System.Net;
28using System.ServiceModel;
29using HeuristicLab.CEDMA.DB.Interfaces;
30using HeuristicLab.CEDMA.DB;
31using System.ServiceModel.Description;
32using System.Linq;
33using HeuristicLab.CEDMA.Core;
34using HeuristicLab.GP.StructureIdentification;
35using HeuristicLab.Data;
36using HeuristicLab.Grid;
37using System.Diagnostics;
38using HeuristicLab.Core;
39using System.Threading;
40
41namespace HeuristicLab.CEDMA.Server {
42  public class Executer {
43    private IDispatcher dispatcher;
44    private JobManager jobManager;
45    private IStore store;
46    private Dictionary<WaitHandle, Execution> activeExecutions;
47
48    private TimeSpan StartJobInterval {
49      get { return TimeSpan.FromMilliseconds(500); }
50    }
51
52    private TimeSpan WaitForFinishedJobsTimeout {
53      get { return TimeSpan.FromMilliseconds(100); }
54    }
55
56    private int maxActiveJobs;
57    public int MaxActiveJobs {
58      get { return maxActiveJobs; }
59      set {
60        if (value < 0) throw new ArgumentException("Only positive values are allowed for MaxActiveJobs");
61        maxActiveJobs = value;
62      }
63    }
64
65    public Executer(IDispatcher dispatcher, IStore store, string gridUrl) {
66      activeExecutions = new Dictionary<WaitHandle, Execution>();
67      maxActiveJobs = 10;
68      this.dispatcher = dispatcher;
69      this.store = store;
70      this.jobManager = new JobManager(gridUrl);
71      jobManager.Reset();
72    }
73
74    internal void Start() {
75      new Thread(StartJobs).Start();
76    }
77
78    private void StartJobs() {
79      List<WaitHandle> wh = new List<WaitHandle>();
80      Dictionary<WaitHandle, AtomicOperation> activeOperations = new Dictionary<WaitHandle, AtomicOperation>();
81      while (true) {
82        try {
83          // start new jobs as long as there are less than MaxActiveJobs
84          while (wh.Count < MaxActiveJobs) {
85            Thread.Sleep(StartJobInterval);
86            // get an execution from the dispatcher and execute in grid via job-manager
87            Execution execution = dispatcher.GetNextJob();
88            if (execution != null) {
89              AtomicOperation op = new AtomicOperation(execution.Engine.OperatorGraph.InitialOperator, execution.Engine.GlobalScope);
90              WaitHandle opWh = jobManager.BeginExecuteOperation(execution.Engine.GlobalScope, op);
91              wh.Add(opWh);
92              activeOperations.Add(opWh, op);
93              lock (activeExecutions) {
94                activeExecutions.Add(opWh, execution);
95              }
96            }
97          }
98          // wait until any job is finished
99          WaitHandle[] whArr = wh.ToArray();
100          int readyHandleIndex = WaitHandle.WaitAny(whArr, WaitForFinishedJobsTimeout);
101          if (readyHandleIndex != WaitHandle.WaitTimeout) {
102            WaitHandle readyHandle = whArr[readyHandleIndex];
103            AtomicOperation finishedOp = activeOperations[readyHandle];
104            wh.Remove(readyHandle);
105            Execution finishedExecution = null;
106            lock (activeExecutions) {
107              finishedExecution = activeExecutions[readyHandle];
108              activeExecutions.Remove(readyHandle);
109            }
110            activeOperations.Remove(readyHandle);
111            ProcessingEngine finishedEngine = null;
112            try {
113              finishedEngine = jobManager.EndExecuteOperation(finishedOp);
114            }
115            catch (Exception badEx) {
116              Trace.WriteLine("CEDMA Executer: Exception in job execution thread. " + badEx.Message);
117            }
118            if (finishedEngine != null) {
119              StoreResults(finishedExecution, finishedEngine);
120            }
121          }
122        }
123        catch (Exception ex) {
124          Trace.WriteLine("CEDMA Executer: Exception in job-management thread. " + ex.Message);
125        }
126      }
127    }
128
129    private void StoreResults(Execution finishedExecution, ProcessingEngine finishedEngine) {
130      Entity model = new Entity(Ontology.CedmaNameSpace + Guid.NewGuid());
131      store.Add(new Statement(model, Ontology.PredicateInstanceOf, Ontology.TypeGeneticProgrammingFunctionTree));
132      store.Add(new Statement(finishedExecution.DataSetEntity, Ontology.PredicateHasModel, model));
133      StoreModelAttribute(model, Ontology.TargetVariable, finishedExecution.TargetVariable);
134      StoreModelAttribute(model, Ontology.AlgorithmName, finishedExecution.Description);
135      Scope bestModelScope = finishedEngine.GlobalScope.GetVariableValue<Scope>("BestValidationSolution", false);
136      StoreModelVariable(model, Ontology.TrainingMeanSquaredError, bestModelScope, "Quality");
137      StoreModelVariable(model, Ontology.ValidationMeanSquaredError, bestModelScope, "ValidationQuality");
138      StoreModelVariable(model, Ontology.TestMeanSquaredError, bestModelScope, "TestQuality");
139      StoreModelVariable(model, Ontology.TrainingMeanAbsolutePercentageError, bestModelScope, "TrainingMAPE");
140      StoreModelVariable(model, Ontology.ValidationMeanAbsolutePercentageError, bestModelScope, "ValidationMAPE");
141      StoreModelVariable(model, Ontology.TestMeanAbsolutePercentageError, bestModelScope, "TestMAPE");
142      StoreModelVariable(model, Ontology.TrainingMeanAbsolutePercentageOfRangeError, bestModelScope, "TrainingMAPRE");
143      StoreModelVariable(model, Ontology.ValidationMeanAbsolutePercentageOfRangeError, bestModelScope, "ValidationMAPRE");
144      StoreModelVariable(model, Ontology.TestMeanAbsolutePercentageOfRangeError, bestModelScope, "TestMAPRE");
145      StoreModelVariable(model, Ontology.TrainingCoefficientOfDetermination, bestModelScope, "TrainingR2");
146      StoreModelVariable(model, Ontology.ValidationCoefficientOfDetermination, bestModelScope, "ValidationR2");
147      StoreModelVariable(model, Ontology.TestCoefficientOfDetermination, bestModelScope, "TestR2");
148      StoreModelVariable(model, Ontology.TrainingTheilsInequalityCoefficient, bestModelScope, "TrainingTheilInequalityCoefficient");
149      StoreModelVariable(model, Ontology.ValidationTheilsInequalityCoefficient, bestModelScope, "ValidationTheilInequalityCoefficient");
150      StoreModelVariable(model, Ontology.TestTheilsInequalityCoefficient, bestModelScope, "TestTheilInequalityCoefficient");
151      StoreModelVariable(model, Ontology.TrainingAccuracy, bestModelScope, "TrainingAccuracy");
152      StoreModelVariable(model, Ontology.ValidationAccuracy, bestModelScope, "ValidationAccuracy");
153      StoreModelVariable(model, Ontology.TestAccuracy, bestModelScope, "TestAccuracy");
154      StoreModelVariable(model, Ontology.TreeSize, bestModelScope, "TreeSize");
155      StoreModelVariable(model, Ontology.TreeHeight, bestModelScope, "TreeHeight");
156      StoreModelVariable(model, Ontology.EvaluatedSolutions, bestModelScope, "EvaluatedSolutions");
157
158      byte[] serializedModel = PersistenceManager.SaveToGZip(bestModelScope.GetVariableValue("FunctionTree", false));
159      store.Add(new Statement(model, Ontology.PredicateSerializedData, new Literal(Convert.ToBase64String(serializedModel))));
160    }
161
162    private void StoreModelVariable(Entity model, Entity entity, Scope scope, string variableName) {
163      if (scope.GetVariable(variableName) != null)
164        StoreModelAttribute(model, entity, scope.GetVariableValue<ObjectData>(variableName, false).Data);
165    }
166
167    private void StoreModelAttribute(Entity model, Entity predicate, object value) {
168      store.Add(new Statement(model, predicate, new Literal(value)));
169    }
170
171    internal string[] GetJobs() {
172      lock (activeExecutions) {
173        string[] retVal = new string[activeExecutions.Count];
174        int i = 0;
175        foreach (Execution e in activeExecutions.Values) {
176          retVal[i++] = "Target-Variable: " + e.TargetVariable + " " + e.Description;
177        }
178        return retVal;
179      }
180    }
181  }
182}
Note: See TracBrowser for help on using the repository browser.