Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2257 was 2223, checked in by mkommend, 15 years ago

reintegrated branch new heuristic.modeling database backend (ticket #712)

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