Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Modeling Database Backend/sources/HeuristicLab.CEDMA.Server/3.3/DispatcherBase.cs @ 2204

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

Changed dispatcher to allow all variables as input or target variable. #712

File size: 5.4 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.Core;
37using HeuristicLab.Modeling;
38using HeuristicLab.Modeling.Database;
39
40namespace HeuristicLab.CEDMA.Server {
41  public abstract class DispatcherBase : IDispatcher {
42    private IModelingDatabase database;
43    private List<int> allowedTargetVariables;
44    private Dictionary<int, List<int>> activeInputVariables;
45    private Problem problem;
46    internal event EventHandler Changed;
47    private object locker = new object();
48
49    public IEnumerable<string> TargetVariables {
50      get {
51        if (problem != null) {
52          return Enumerable.Range(0, problem.Dataset.Columns).Select(x => problem.Dataset.GetVariableName(x));
53        } else return new string[0];
54      }
55    }
56
57    public IEnumerable<string> InputVariables {
58      get {
59        if (problem != null) {
60          return TargetVariables;
61        } else return new string[0];
62      }
63    }
64
65    public DispatcherBase(IModelingDatabase database, Problem problem) {
66      this.problem = problem;
67      allowedTargetVariables = new List<int>();
68      activeInputVariables = new Dictionary<int, List<int>>();
69      problem.Changed += (sender, args) => {
70        lock (locker) {
71          allowedTargetVariables.Clear();
72          activeInputVariables.Clear();
73        }
74        OnChanged();
75      };
76      this.database = database;
77    }
78
79    public HeuristicLab.Modeling.IAlgorithm GetNextJob() {
80      if (allowedTargetVariables.Count > 0) {
81        int[] targetVariables, inputVariables;
82        lock (locker) {
83          targetVariables = allowedTargetVariables.ToArray();
84        }
85
86        int targetVariable = SelectTargetVariable(targetVariables);
87
88        lock (locker) {
89          inputVariables = activeInputVariables[targetVariable].ToArray();
90        }
91
92        HeuristicLab.Modeling.IAlgorithm selectedAlgorithm = SelectAndConfigureAlgorithm(targetVariable, inputVariables, problem);
93
94        return selectedAlgorithm;
95      } else return null;
96    }
97
98    public virtual int SelectTargetVariable(int[] targetVariables) {
99      Random rand = new Random();
100      return targetVariables[rand.Next(targetVariables.Length)];
101    }
102    public abstract HeuristicLab.Modeling.IAlgorithm SelectAndConfigureAlgorithm(int targetVariable, int[] inputVariables, Problem problem);
103
104    #region IViewable Members
105
106    public IView CreateView() {
107      return new DispatcherView(this);
108    }
109
110    #endregion
111
112    internal void EnableTargetVariable(string name) {
113      lock (locker)
114        allowedTargetVariables.Add(problem.Dataset.GetVariableIndex(name));
115    }
116
117    internal void DisableTargetVariable(string name) {
118      lock (locker)
119        allowedTargetVariables.Remove(problem.Dataset.GetVariableIndex(name));
120    }
121
122    internal void EnableInputVariable(string target, string name) {
123      lock (locker) {
124        int targetIndex = problem.Dataset.GetVariableIndex(target);
125        int inputIndex = problem.Dataset.GetVariableIndex(name);
126        if (!activeInputVariables.ContainsKey(targetIndex)) activeInputVariables[targetIndex] = new List<int>();
127        if (!activeInputVariables[targetIndex].Contains(inputIndex)) {
128          activeInputVariables[targetIndex].Add(inputIndex);
129        }
130      }
131    }
132
133    internal void DisableInputVariable(string target, string name) {
134      lock (locker) {
135        int targetIndex = problem.Dataset.GetVariableIndex(target);
136        int inputIndex = problem.Dataset.GetVariableIndex(name);
137        if (!activeInputVariables.ContainsKey(targetIndex)) activeInputVariables[targetIndex] = new List<int>();
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      int targetIndex = problem.Dataset.GetVariableIndex(target);
148      if (!activeInputVariables.ContainsKey(targetIndex)) activeInputVariables[targetIndex] = new List<int>();
149      return activeInputVariables[targetIndex]
150        .Select(i => problem.Dataset.GetVariableName(i));
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.