Free cookie consent management tool by TermsFeed Policy Generator

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

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

Fixed a few bugs in CEDMA dispatching. #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 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 problem.AllowedTargetVariables.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 problem.AllowedInputVariables.Select(x => problem.Dataset.GetVariableName(x));
61        } else return new string[0];
62      }
63    }
64
65    public DispatcherBase(IModelingDatabase database, Problem problem) {
66      this.problem = problem;
67      problem.Changed += (sender, args) => {
68        allowedTargetVariables = new List<int>(problem.AllowedTargetVariables);
69        activeInputVariables = new Dictionary<int, List<int>>();
70        foreach (int targetVar in problem.AllowedTargetVariables) {
71          activeInputVariables.Add(targetVar, new List<int>());
72          activeInputVariables[targetVar].AddRange(problem.AllowedInputVariables);
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[targetIndex].Contains(inputIndex)) {
127          activeInputVariables[targetIndex].Add(inputIndex);
128        }
129      }
130    }
131
132    internal void DisableInputVariable(string target, string name) {
133      lock (locker) {
134        int targetIndex = problem.Dataset.GetVariableIndex(target);
135        int inputIndex = problem.Dataset.GetVariableIndex(name);
136        while (activeInputVariables[targetIndex].Remove(inputIndex)) { }
137      }
138    }
139
140    private void OnChanged() {
141      if (Changed != null) Changed(this, new EventArgs());
142    }
143
144    internal IEnumerable<string> GetInputVariables(string target) {
145      return activeInputVariables[problem.Dataset.GetVariableIndex(target)]
146        .Select(i => problem.Dataset.GetVariableName(i));
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.