Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/08/09 12:48:18 (15 years ago)
Author:
gkronber
Message:

Merged change sets from CEDMA branch to trunk:

File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.CEDMA.Server/Executer.cs

    r1060 r1287  
    4141namespace HeuristicLab.CEDMA.Server {
    4242  public class Executer {
    43     private static int MaxActiveJobs {
    44       get { return 20; }
    45     }
    46     private Dispatcher dispatcher;
     43    private IDispatcher dispatcher;
    4744    private JobManager jobManager;
    4845    private IStore store;
     46    private Dictionary<WaitHandle, Execution> activeExecutions;
    4947
    50     public Executer(Dispatcher dispatcher, IStore store, string gridUrl) {
     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;
    5168      this.dispatcher = dispatcher;
    5269      this.store = store;
     
    6178    private void StartJobs() {
    6279      List<WaitHandle> wh = new List<WaitHandle>();
    63       Dictionary<WaitHandle, AtomicOperation> activeOperations = new Dictionary<WaitHandle,AtomicOperation>();
    64       Dictionary<WaitHandle, Execution> activeExecutions = new Dictionary<WaitHandle,Execution>();
     80      Dictionary<WaitHandle, AtomicOperation> activeOperations = new Dictionary<WaitHandle, AtomicOperation>();
    6581      while (true) {
    6682        try {
    6783          // start new jobs as long as there are less than MaxActiveJobs
    6884          while (wh.Count < MaxActiveJobs) {
     85            Thread.Sleep(StartJobInterval);
    6986            // get an execution from the dispatcher and execute in grid via job-manager
    7087            Execution execution = dispatcher.GetNextJob();
    71             AtomicOperation op = new AtomicOperation(execution.Engine.OperatorGraph.InitialOperator, execution.Engine.GlobalScope);
    72             WaitHandle opWh = jobManager.BeginExecuteOperation(execution.Engine.GlobalScope, op);
    73             wh.Add(opWh);
    74             activeOperations.Add(opWh, op);
     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            }
    7597          }
    7698          // wait until any job is finished
    7799          WaitHandle[] whArr = wh.ToArray();
    78           int readyHandleIndex = WaitHandle.WaitAny(whArr);
    79           WaitHandle readyHandle = whArr[readyHandleIndex];
    80           AtomicOperation finishedOp = activeOperations[readyHandle];
    81           Execution finishedExecution = activeExecutions[readyHandle];
    82           wh.Remove(readyHandle);
    83           activeExecutions.Remove(readyHandle);
    84           activeOperations.Remove(readyHandle);
    85           ProcessingEngine finishedEngine = null;
    86           try {
    87             finishedEngine = jobManager.EndExecuteOperation(finishedOp);
    88           } catch (Exception badEx) {
    89             Trace.WriteLine("CEDMA Executer: Exception in job execution thread. " + badEx.Message);
    90           }
    91           if (finishedEngine != null) {
    92             Entity modelEntity = new Entity(Ontology.CedmaNameSpace+Guid.NewGuid());
    93             store.Add(new Statement(modelEntity, Ontology.PredicateInstanceOf, Ontology.TypeGeneticProgrammingFunctionTree));
    94             Entity targetVariableAttr = new Entity(Ontology.CedmaNameSpace+Guid.NewGuid());
    95             store.Add(new Statement(modelEntity, Ontology.PredicateModelAttribute, targetVariableAttr));
    96             store.Add(new Statement(targetVariableAttr, Ontology.PredicateModelAttributeName, new Literal("TargetVariable")));
    97             store.Add(new Statement(targetVariableAttr, Ontology.PredicateModelAttributeValue, new Literal(finishedExecution.TargetVariable)));
    98             store.Add(new Statement(targetVariableAttr, Ontology.PredicateModelAttributeType, Ontology.TypeCategoricalAttribute));
    99             store.Add(new Statement(finishedExecution.DataSetEntity, Ontology.PredicateHasModel, modelEntity));
     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            }
    100121          }
    101122        }
     
    105126      }
    106127    }
     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      Scope bestModelScope = finishedEngine.GlobalScope.GetVariableValue<Scope>("BestValidationSolution", false);
     135      StoreModelVariable(model, Ontology.TrainingMeanSquaredError, bestModelScope, "Quality");
     136      StoreModelVariable(model, Ontology.ValidationMeanSquaredError, bestModelScope, "ValidationQuality");
     137      StoreModelVariable(model, Ontology.TestMeanSquaredError, bestModelScope, "TestQuality");
     138      StoreModelVariable(model, Ontology.TrainingMeanAbsolutePercentageError, bestModelScope, "TrainingMAPE");
     139      StoreModelVariable(model, Ontology.ValidationMeanAbsolutePercentageError, bestModelScope, "ValidationMAPE");
     140      StoreModelVariable(model, Ontology.TestMeanAbsolutePercentageError, bestModelScope, "TestMAPE");
     141      StoreModelVariable(model, Ontology.TrainingMeanAbsolutePercentageOfRangeError, bestModelScope, "TrainingMAPRE");
     142      StoreModelVariable(model, Ontology.ValidationMeanAbsolutePercentageOfRangeError, bestModelScope, "ValidationMAPRE");
     143      StoreModelVariable(model, Ontology.TestMeanAbsolutePercentageOfRangeError, bestModelScope, "TestMAPRE");
     144      StoreModelVariable(model, Ontology.TrainingCoefficientOfDetermination, bestModelScope, "TrainingR2");
     145      StoreModelVariable(model, Ontology.ValidationCoefficientOfDetermination, bestModelScope, "ValidationR2");
     146      StoreModelVariable(model, Ontology.TestCoefficientOfDetermination, bestModelScope, "TestR2");
     147      StoreModelVariable(model, Ontology.TrainingTheilsInequalityCoefficient, bestModelScope, "TrainingTheilInequalityCoefficient");
     148      StoreModelVariable(model, Ontology.ValidationTheilsInequalityCoefficient, bestModelScope, "ValidationTheilInequalityCoefficient");
     149      StoreModelVariable(model, Ontology.TestTheilsInequalityCoefficient, bestModelScope, "TestTheilInequalityCoefficient");
     150      StoreModelVariable(model, Ontology.TrainingAccuracy, bestModelScope, "TrainingAccuracy");
     151      StoreModelVariable(model, Ontology.ValidationAccuracy, bestModelScope, "ValidationAccuracy");
     152      StoreModelVariable(model, Ontology.TestAccuracy, bestModelScope, "TestAccuracy");
     153      StoreModelVariable(model, Ontology.TreeSize, bestModelScope, "TreeSize");
     154      StoreModelVariable(model, Ontology.TreeHeight, bestModelScope, "TreeHeight");
     155      StoreModelVariable(model, Ontology.EvaluatedSolutions, bestModelScope, "EvaluatedSolutions");
     156
     157      byte[] serializedModel = PersistenceManager.SaveToGZip(bestModelScope.GetVariableValue("FunctionTree", false));
     158      store.Add(new Statement(model, Ontology.PredicateSerializedData, new Literal(Convert.ToBase64String(serializedModel))));
     159    }
     160
     161    private void StoreModelVariable(Entity model, Entity entity, Scope scope, string variableName) {
     162      if (scope.GetVariable(variableName) != null)
     163        StoreModelAttribute(model, entity, scope.GetVariableValue<ObjectData>(variableName, false).Data);
     164    }
     165
     166    private void StoreModelAttribute(Entity model, Entity predicate, object value) {
     167      store.Add(new Statement(model, predicate, new Literal(value)));
     168    }
     169
     170    internal string[] GetJobs() {
     171      lock (activeExecutions) {
     172        string[] retVal = new string[activeExecutions.Count];
     173        int i = 0;
     174        foreach (Execution e in activeExecutions.Values) {
     175          retVal[i++] = "Target-Variable: " + e.TargetVariable + " " + e.Description;
     176        }
     177        return retVal;
     178      }
     179    }
    107180  }
    108181}
Note: See TracChangeset for help on using the changeset viewer.