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