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