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