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