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
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.Data;
35using HeuristicLab.Core;
36using HeuristicLab.Modeling;
37
38namespace HeuristicLab.CEDMA.Server {
39  public abstract class DispatcherBase : IDispatcher {
40    private IStore store;
41    private DataSet dataset;
42    private List<int> allowedTargetVariables;
43    private Dictionary<int, List<int>> activeInputVariables;
44
45    internal event EventHandler Changed;
46    private object locker = new object();
47
48    public IEnumerable<string> TargetVariables {
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
56    public IEnumerable<string> InputVariables {
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
64    public DispatcherBase(IStore store) {
65      this.store = store;
66      allowedTargetVariables = new List<int>();
67      activeInputVariables = new Dictionary<int, List<int>>();
68    }
69
70    public IAlgorithm GetNextJob() {
71      if (dataset == null) {
72        var datasetEntities = store.Query("?DataSet <" + Ontology.InstanceOf.Uri + "> <" + Ontology.TypeDataSet.Uri + "> .", 0, 1)
73          .Select(x => (Entity)x.Get("DataSet"));
74        if (datasetEntities.Count() == 0) return null;
75        dataset = new DataSet(store, datasetEntities.ElementAt(0));
76        foreach (int targetVar in dataset.Problem.AllowedTargetVariables) {
77          activeInputVariables.Add(targetVar, new List<int>());
78          activeInputVariables[targetVar].AddRange(dataset.Problem.AllowedInputVariables);
79        }
80        OnChanged();
81      }
82      if (allowedTargetVariables.Count > 0) {
83        int[] targetVariables, inputVariables;
84        lock (locker) {
85          targetVariables = allowedTargetVariables.ToArray();
86        }
87
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
96        return selectedAlgorithm;
97      } else return null;
98    }
99
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);
105
106    #region IViewable Members
107
108    public IView CreateView() {
109      return new DispatcherView(this);
110    }
111
112    #endregion
113
114    internal void EnableTargetVariable(string name) {
115      lock (locker)
116        allowedTargetVariables.Add(dataset.Problem.Dataset.GetVariableIndex(name));
117    }
118
119    internal void DisableTargetVariable(string name) {
120      lock (locker)
121        allowedTargetVariables.Remove(dataset.Problem.Dataset.GetVariableIndex(name));
122    }
123
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      }
132    }
133
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
142    private void OnChanged() {
143      if (Changed != null) Changed(this, new EventArgs());
144    }
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    }
150  }
151}
Note: See TracBrowser for help on using the repository browser.