Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CEDMA.Server/3.3/DispatcherBase.cs @ 2222

Last change on this file since 2222 was 2222, checked in by gkronber, 15 years ago

Merged changes from GP-refactoring branch back into the trunk #713.

File size: 5.3 KB
RevLine 
[1044]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;
[1053]34using HeuristicLab.Data;
[1060]35using HeuristicLab.Core;
[1857]36using HeuristicLab.Modeling;
[1044]37
38namespace HeuristicLab.CEDMA.Server {
[1287]39  public abstract class DispatcherBase : IDispatcher {
[1044]40    private IStore store;
[2012]41    private DataSet dataset;
[2153]42    private List<int> allowedTargetVariables;
43    private Dictionary<int, List<int>> activeInputVariables;
44
[2094]45    internal event EventHandler Changed;
[2119]46    private object locker = new object();
47
48    public IEnumerable<string> TargetVariables {
[2088]49      get {
50        if (dataset != null) {
51          return dataset.Problem.AllowedTargetVariables.Select(x => dataset.Problem.Dataset.GetVariableName(x));
52        } else return new string[0];
53      }
54    }
55
[2119]56    public IEnumerable<string> InputVariables {
[2088]57      get {
58        if (dataset != null) {
59          return dataset.Problem.AllowedInputVariables.Select(x => dataset.Problem.Dataset.GetVariableName(x));
60        } else return new string[0];
61      }
62    }
63
[1217]64    public DispatcherBase(IStore store) {
[1044]65      this.store = store;
[2119]66      allowedTargetVariables = new List<int>();
[2153]67      activeInputVariables = new Dictionary<int, List<int>>();
[1044]68    }
69
[1922]70    public IAlgorithm GetNextJob() {
[2012]71      if (dataset == null) {
[2047]72        var datasetEntities = store.Query("?DataSet <" + Ontology.InstanceOf.Uri + "> <" + Ontology.TypeDataSet.Uri + "> .", 0, 1)
[2012]73          .Select(x => (Entity)x.Get("DataSet"));
74        if (datasetEntities.Count() == 0) return null;
75        dataset = new DataSet(store, datasetEntities.ElementAt(0));
[2153]76        foreach (int targetVar in dataset.Problem.AllowedTargetVariables) {
77          activeInputVariables.Add(targetVar, new List<int>());
78          activeInputVariables[targetVar].AddRange(dataset.Problem.AllowedInputVariables);
79        }
[2094]80        OnChanged();
[2012]81      }
[2153]82      if (allowedTargetVariables.Count > 0) {
[2119]83        int[] targetVariables, inputVariables;
84        lock (locker) {
85          targetVariables = allowedTargetVariables.ToArray();
86        }
[1130]87
[2153]88        int targetVariable = SelectTargetVariable(targetVariables);
89
90        lock (locker) {
91          inputVariables = activeInputVariables[targetVariable].ToArray();
92        }
93
94        IAlgorithm selectedAlgorithm = SelectAndConfigureAlgorithm(targetVariable, inputVariables, dataset.Problem);
95
[2119]96        return selectedAlgorithm;
97      } else return null;
[1044]98    }
99
[2153]100    public virtual int SelectTargetVariable(int[] targetVariables) {
101      Random rand = new Random();
102      return targetVariables[rand.Next(targetVariables.Length)];
103    }
104    public abstract IAlgorithm SelectAndConfigureAlgorithm(int targetVariable, int[] inputVariables, Problem problem);
[1060]105
[2119]106    #region IViewable Members
[1287]107
[2119]108    public IView CreateView() {
109      return new DispatcherView(this);
[1053]110    }
[1287]111
[2119]112    #endregion
113
114    internal void EnableTargetVariable(string name) {
115      lock (locker)
116        allowedTargetVariables.Add(dataset.Problem.Dataset.GetVariableIndex(name));
[2094]117    }
118
[2119]119    internal void DisableTargetVariable(string name) {
120      lock (locker)
121        allowedTargetVariables.Remove(dataset.Problem.Dataset.GetVariableIndex(name));
122    }
[2088]123
[2153]124    internal void EnableInputVariable(string target, string name) {
125      lock (locker) {
126        int targetIndex = dataset.Problem.Dataset.GetVariableIndex(target);
127        int inputIndex = dataset.Problem.Dataset.GetVariableIndex(name);
128        if (!activeInputVariables[targetIndex].Contains(inputIndex)) {
129          activeInputVariables[targetIndex].Add(inputIndex);
130        }
131      }
[2088]132    }
133
[2153]134    internal void DisableInputVariable(string target, string name) {
135      lock (locker) {
136        int targetIndex = dataset.Problem.Dataset.GetVariableIndex(target);
137        int inputIndex = dataset.Problem.Dataset.GetVariableIndex(name);
138        while (activeInputVariables[targetIndex].Remove(inputIndex)) { }
139      }
140    }
141
[2119]142    private void OnChanged() {
143      if (Changed != null) Changed(this, new EventArgs());
144    }
[2153]145
146    internal IEnumerable<string> GetInputVariables(string target) {
147      return activeInputVariables[dataset.Problem.Dataset.GetVariableIndex(target)]
148        .Select(i => dataset.Problem.Dataset.GetVariableName(i));
149    }
[1044]150  }
151}
Note: See TracBrowser for help on using the repository browser.