Changeset 2119
- Timestamp:
- 06/26/09 17:33:13 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.CEDMA.Server/3.3
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.CEDMA.Server/3.3/DispatcherBase.cs
r2094 r2119 42 42 private DataSet dataset; 43 43 internal event EventHandler Changed; 44 public IEnumerable<string> AllowedTargetVariables { 44 private object locker = new object(); 45 46 public IEnumerable<string> TargetVariables { 45 47 get { 46 48 if (dataset != null) { … … 50 52 } 51 53 52 public IEnumerable<string> AllowedInputVariables {54 public IEnumerable<string> InputVariables { 53 55 get { 54 56 if (dataset != null) { … … 58 60 } 59 61 62 private List<int> allowedInputVariables; 63 private List<int> allowedTargetVariables; 64 60 65 public DispatcherBase(IStore store) { 61 66 this.store = store; 67 allowedInputVariables = new List<int>(); 68 allowedTargetVariables = new List<int>(); 62 69 } 63 70 … … 70 77 OnChanged(); 71 78 } 72 int targetVariable = SelectTargetVariable(dataset.Problem.AllowedTargetVariables.ToArray()); 73 IAlgorithm selectedAlgorithm = SelectAlgorithm(targetVariable, dataset.Problem.LearningTask); 74 string targetVariableName = dataset.Problem.GetVariableName(targetVariable); 79 if (allowedTargetVariables.Count > 0 && allowedInputVariables.Count > 0) { 80 int[] targetVariables, inputVariables; 81 lock (locker) { 82 targetVariables = allowedTargetVariables.ToArray(); 83 inputVariables = allowedInputVariables.ToArray(); 84 } 75 85 76 if (selectedAlgorithm != null) {77 SetProblemParameters(selectedAlgorithm, dataset.Problem, targetVariable);78 }79 return selectedAlgorithm;86 IAlgorithm selectedAlgorithm = SelectAndConfigureAlgorithm(targetVariables, inputVariables, dataset.Problem); 87 88 return selectedAlgorithm; 89 } else return null; 80 90 } 81 91 82 public abstract int SelectTargetVariable(int[] targetVariables); 83 public abstract IAlgorithm SelectAlgorithm(int targetVariable, LearningTask learningTask); 84 85 private void SetProblemParameters(IAlgorithm algo, Problem problem, int targetVariable) { 86 algo.Dataset = problem.Dataset; 87 algo.TargetVariable = targetVariable; 88 algo.ProblemInjector.GetVariable("TrainingSamplesStart").GetValue<IntData>().Data = problem.TrainingSamplesStart; 89 algo.ProblemInjector.GetVariable("TrainingSamplesEnd").GetValue<IntData>().Data = problem.TrainingSamplesEnd; 90 algo.ProblemInjector.GetVariable("ValidationSamplesStart").GetValue<IntData>().Data = problem.ValidationSamplesStart; 91 algo.ProblemInjector.GetVariable("ValidationSamplesEnd").GetValue<IntData>().Data = problem.ValidationSamplesEnd; 92 algo.ProblemInjector.GetVariable("TestSamplesStart").GetValue<IntData>().Data = problem.TestSamplesStart; 93 algo.ProblemInjector.GetVariable("TestSamplesEnd").GetValue<IntData>().Data = problem.TestSamplesEnd; 94 ItemList<IntData> allowedFeatures = algo.ProblemInjector.GetVariable("AllowedFeatures").GetValue<ItemList<IntData>>(); 95 foreach (int allowedFeature in problem.AllowedInputVariables) allowedFeatures.Add(new IntData(allowedFeature)); 96 97 if (problem.LearningTask == LearningTask.TimeSeries) { 98 algo.ProblemInjector.GetVariable("Autoregressive").GetValue<BoolData>().Data = problem.AutoRegressive; 99 algo.ProblemInjector.GetVariable("MinTimeOffset").GetValue<IntData>().Data = problem.MinTimeOffset; 100 algo.ProblemInjector.GetVariable("MaxTimeOffset").GetValue<IntData>().Data = problem.MaxTimeOffset; 101 } else if (problem.LearningTask == LearningTask.Classification) { 102 ItemList<DoubleData> classValues = algo.ProblemInjector.GetVariable("TargetClassValues").GetValue<ItemList<DoubleData>>(); 103 foreach (double classValue in GetDifferentClassValues(problem.Dataset, targetVariable)) classValues.Add(new DoubleData(classValue)); 104 } 105 } 106 107 private IEnumerable<double> GetDifferentClassValues(HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable) { 108 return Enumerable.Range(0, dataset.Rows).Select(x => dataset.GetValue(x, targetVariable)).Distinct(); 109 } 110 111 private void OnChanged() { 112 if (Changed != null) Changed(this, new EventArgs()); 113 } 92 public abstract IAlgorithm SelectAndConfigureAlgorithm(int[] targetVariables, int[] inputVariables, Problem problem); 114 93 115 94 #region IViewable Members … … 120 99 121 100 #endregion 101 102 internal void EnableInputVariable(string name) { 103 lock (locker) 104 allowedInputVariables.Add(dataset.Problem.Dataset.GetVariableIndex(name)); 105 } 106 107 internal void EnableTargetVariable(string name) { 108 lock (locker) 109 allowedTargetVariables.Add(dataset.Problem.Dataset.GetVariableIndex(name)); 110 } 111 112 internal void DisableTargetVariable(string name) { 113 lock (locker) 114 allowedTargetVariables.Remove(dataset.Problem.Dataset.GetVariableIndex(name)); 115 } 116 117 internal void DisableInputVariable(string name) { 118 lock (locker) 119 allowedInputVariables.Remove(dataset.Problem.Dataset.GetVariableIndex(name)); 120 } 121 122 private void OnChanged() { 123 if (Changed != null) Changed(this, new EventArgs()); 124 } 122 125 } 123 126 } -
trunk/sources/HeuristicLab.CEDMA.Server/3.3/DispatcherView.Designer.cs
r2088 r2119 37 37 this.targetVariableList.Size = new System.Drawing.Size(171, 409); 38 38 this.targetVariableList.TabIndex = 0; 39 this.targetVariableList.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.targetVariableList_ItemCheck); 39 40 // 40 41 // inputVariableList … … 45 46 this.inputVariableList.Size = new System.Drawing.Size(170, 409); 46 47 this.inputVariableList.TabIndex = 1; 48 this.inputVariableList.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.inputVariableList_ItemCheck); 47 49 // 48 50 // targetVariablesLabel … … 73 75 this.Controls.Add(this.targetVariableList); 74 76 this.Name = "DispatcherView"; 75 this.Size = new System.Drawing.Size(4 17, 427);77 this.Size = new System.Drawing.Size(429, 482); 76 78 this.ResumeLayout(false); 77 79 this.PerformLayout(); -
trunk/sources/HeuristicLab.CEDMA.Server/3.3/DispatcherView.cs
r2094 r2119 22 22 base.UpdateControls(); 23 23 24 foreach (string targetVar in dispatcher. AllowedTargetVariables) {24 foreach (string targetVar in dispatcher.TargetVariables) { 25 25 targetVariableList.Items.Add(targetVar); 26 26 } 27 27 28 foreach (string inputVar in dispatcher. AllowedInputVariables) {28 foreach (string inputVar in dispatcher.InputVariables) { 29 29 inputVariableList.Items.Add(inputVar); 30 } 31 } 32 33 private void targetVariableList_ItemCheck(object sender, ItemCheckEventArgs e) { 34 if (e.NewValue == CheckState.Checked) { 35 dispatcher.EnableTargetVariable((string)targetVariableList.Items[e.Index]); 36 } else if (e.NewValue == CheckState.Unchecked) { 37 dispatcher.DisableTargetVariable((string)targetVariableList.Items[e.Index]); 38 } 39 } 40 41 private void inputVariableList_ItemCheck(object sender, ItemCheckEventArgs e) { 42 if (e.NewValue == CheckState.Checked) { 43 dispatcher.EnableInputVariable((string)inputVariableList.Items[e.Index]); 44 } else if (e.NewValue == CheckState.Unchecked) { 45 dispatcher.DisableInputVariable((string)inputVariableList.Items[e.Index]); 30 46 } 31 47 } -
trunk/sources/HeuristicLab.CEDMA.Server/3.3/ServerApplication.cs
r2088 r2119 36 36 serverControl.Dock = DockStyle.Fill; 37 37 mainForm.Controls.Add(serverControl); 38 mainForm.Name = "CEDMA Server"; 38 39 Application.Run(mainForm); 39 40 } -
trunk/sources/HeuristicLab.CEDMA.Server/3.3/SimpleDispatcher.cs
r2049 r2119 39 39 namespace HeuristicLab.CEDMA.Server { 40 40 public class SimpleDispatcher : DispatcherBase { 41 private class AlgorithmConfiguration { 42 public string name; 43 public int targetVariable; 44 public List<int> inputVariables; 45 } 46 41 47 private Random random; 42 48 private IStore store; 43 private Dictionary<int, List< string>> finishedAndDispatchedRuns;49 private Dictionary<int, List<AlgorithmConfiguration>> finishedAndDispatchedRuns; 44 50 45 51 public SimpleDispatcher(IStore store) … … 47 53 this.store = store; 48 54 random = new Random(); 49 finishedAndDispatchedRuns = new Dictionary<int, List< string>>();55 finishedAndDispatchedRuns = new Dictionary<int, List<AlgorithmConfiguration>>(); 50 56 PopulateFinishedRuns(); 51 57 } 52 58 53 public override IAlgorithm SelectAlgorithm(int targetVariable, LearningTask learningTask) { 59 public override IAlgorithm SelectAndConfigureAlgorithm(int[] targetVariables, int[] inputVariables, Problem problem) { 60 int targetVariable = SelectTargetVariable(targetVariables); 61 54 62 DiscoveryService ds = new DiscoveryService(); 55 63 IAlgorithm[] algos = ds.GetInstances<IAlgorithm>(); 56 64 IAlgorithm selectedAlgorithm = null; 57 switch ( learningTask) {65 switch (problem.LearningTask) { 58 66 case LearningTask.Regression: { 59 67 var regressionAlgos = algos.Where(a => (a as IClassificationAlgorithm) == null && (a as ITimeSeriesAlgorithm) == null); 60 selectedAlgorithm = ChooseDeterministic(targetVariable, regressionAlgos) ?? ChooseStochastic(regressionAlgos);68 selectedAlgorithm = ChooseDeterministic(targetVariable, inputVariables, regressionAlgos) ?? ChooseStochastic(regressionAlgos); 61 69 break; 62 70 } 63 71 case LearningTask.Classification: { 64 72 var classificationAlgos = algos.Where(a => (a as IClassificationAlgorithm) != null); 65 selectedAlgorithm = ChooseDeterministic(targetVariable, classificationAlgos) ?? ChooseStochastic(classificationAlgos);73 selectedAlgorithm = ChooseDeterministic(targetVariable, inputVariables, classificationAlgos) ?? ChooseStochastic(classificationAlgos); 66 74 break; 67 75 } 68 76 case LearningTask.TimeSeries: { 69 77 var timeSeriesAlgos = algos.Where(a => (a as ITimeSeriesAlgorithm) != null); 70 selectedAlgorithm = ChooseDeterministic(targetVariable, timeSeriesAlgos) ?? ChooseStochastic(timeSeriesAlgos);78 selectedAlgorithm = ChooseDeterministic(targetVariable, inputVariables, timeSeriesAlgos) ?? ChooseStochastic(timeSeriesAlgos); 71 79 break; 72 80 } 73 81 } 82 83 SetProblemParameters(selectedAlgorithm, problem, targetVariable, inputVariables); 84 74 85 if (selectedAlgorithm != null) { 75 AddDispatchedRun(targetVariable, selectedAlgorithm.Name);86 AddDispatchedRun(targetVariable, inputVariables, selectedAlgorithm.Name); 76 87 } 77 88 return selectedAlgorithm; 78 89 } 79 90 80 private IAlgorithm ChooseDeterministic(int targetVariable, IEnumerable<IAlgorithm> algos) {91 private IAlgorithm ChooseDeterministic(int targetVariable, int[] inputVariables, IEnumerable<IAlgorithm> algos) { 81 92 var deterministicAlgos = algos 82 93 .Where(a => (a as IStochasticAlgorithm) == null) 83 .Where(a => AlgorithmFinishedOrDispatched(targetVariable, a.Name) == false);94 .Where(a => AlgorithmFinishedOrDispatched(targetVariable, inputVariables, a.Name) == false); 84 95 85 96 if (deterministicAlgos.Count() == 0) return null; … … 93 104 } 94 105 95 public overrideint SelectTargetVariable(int[] targetVariables) {106 public int SelectTargetVariable(int[] targetVariables) { 96 107 return targetVariables[random.Next(targetVariables.Length)]; 97 108 } 98 109 99 110 private void PopulateFinishedRuns() { 111 Dictionary<Entity, Entity> processedModels = new Dictionary<Entity, Entity>(); 100 112 var datasetBindings = store 101 113 .Query( … … 112 124 "?Model <" + Ontology.Name + "> ?AlgoName .", 113 125 0, 1000) 114 .Select(x => new Resource[] { (Literal)x.Get("TargetVariable"), (Literal)x.Get("AlgoName") });126 .Select(x => new Resource[] { (Literal)x.Get("TargetVariable"), (Literal)x.Get("AlgoName"), (Entity)x.Get("Model") }); 115 127 116 128 foreach (Resource[] row in result) { 117 string targetVariable = (string)((Literal)row[0]).Value; 118 string algoName = (string)((Literal)row[1]).Value; 119 120 int targetVariableIndex = ds.Problem.Dataset.GetVariableIndex(targetVariable); 121 if (!AlgorithmFinishedOrDispatched(targetVariableIndex, algoName)) 122 AddDispatchedRun(targetVariableIndex, algoName); 129 Entity model = ((Entity)row[2]); 130 if (!processedModels.ContainsKey(model)) { 131 processedModels.Add(model, model); 132 133 string targetVariable = (string)((Literal)row[0]).Value; 134 string algoName = (string)((Literal)row[1]).Value; 135 int targetVariableIndex = ds.Problem.Dataset.GetVariableIndex(targetVariable); 136 137 var inputVariableLiterals = store 138 .Query( 139 "<" + model.Uri + "> <" + Ontology.HasInputVariable + "> ?InputVariable ." + Environment.NewLine + 140 "?InputVariable <" + Ontology.Name + "> ?Name .", 141 0, 1000) 142 .Select(x => (Literal)x.Get("Name")) 143 .Select(l => (string)l.Value) 144 .Distinct(); 145 146 List<int> inputVariables = new List<int>(); 147 foreach (string variableName in inputVariableLiterals) { 148 int variableIndex = ds.Problem.Dataset.GetVariableIndex(variableName); 149 inputVariables.Add(variableIndex); 150 } 151 if (!AlgorithmFinishedOrDispatched(targetVariableIndex, inputVariables.ToArray(), algoName)) { 152 AddDispatchedRun(targetVariableIndex, inputVariables.ToArray(), algoName); 153 } 154 } 123 155 } 124 156 } 125 157 } 126 158 127 private void AddDispatchedRun(int targetVariable, string algoName) { 159 private void SetProblemParameters(IAlgorithm algo, Problem problem, int targetVariable, int[] inputVariables) { 160 algo.Dataset = problem.Dataset; 161 algo.TargetVariable = targetVariable; 162 algo.ProblemInjector.GetVariable("TrainingSamplesStart").GetValue<IntData>().Data = problem.TrainingSamplesStart; 163 algo.ProblemInjector.GetVariable("TrainingSamplesEnd").GetValue<IntData>().Data = problem.TrainingSamplesEnd; 164 algo.ProblemInjector.GetVariable("ValidationSamplesStart").GetValue<IntData>().Data = problem.ValidationSamplesStart; 165 algo.ProblemInjector.GetVariable("ValidationSamplesEnd").GetValue<IntData>().Data = problem.ValidationSamplesEnd; 166 algo.ProblemInjector.GetVariable("TestSamplesStart").GetValue<IntData>().Data = problem.TestSamplesStart; 167 algo.ProblemInjector.GetVariable("TestSamplesEnd").GetValue<IntData>().Data = problem.TestSamplesEnd; 168 ItemList<IntData> allowedFeatures = algo.ProblemInjector.GetVariable("AllowedFeatures").GetValue<ItemList<IntData>>(); 169 foreach (int inputVariable in inputVariables) allowedFeatures.Add(new IntData(inputVariable)); 170 171 if (problem.LearningTask == LearningTask.TimeSeries) { 172 algo.ProblemInjector.GetVariable("Autoregressive").GetValue<BoolData>().Data = problem.AutoRegressive; 173 algo.ProblemInjector.GetVariable("MinTimeOffset").GetValue<IntData>().Data = problem.MinTimeOffset; 174 algo.ProblemInjector.GetVariable("MaxTimeOffset").GetValue<IntData>().Data = problem.MaxTimeOffset; 175 } else if (problem.LearningTask == LearningTask.Classification) { 176 ItemList<DoubleData> classValues = algo.ProblemInjector.GetVariable("TargetClassValues").GetValue<ItemList<DoubleData>>(); 177 foreach (double classValue in GetDifferentClassValues(problem.Dataset, targetVariable)) classValues.Add(new DoubleData(classValue)); 178 } 179 } 180 181 private IEnumerable<double> GetDifferentClassValues(HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable) { 182 return Enumerable.Range(0, dataset.Rows).Select(x => dataset.GetValue(x, targetVariable)).Distinct(); 183 } 184 185 private void AddDispatchedRun(int targetVariable, int[] inputVariables, string algoName) { 128 186 if (!finishedAndDispatchedRuns.ContainsKey(targetVariable)) { 129 finishedAndDispatchedRuns[targetVariable] = new List<string>(); 130 } 131 finishedAndDispatchedRuns[targetVariable].Add(algoName); 132 } 133 134 private bool AlgorithmFinishedOrDispatched(int targetVariable, string algoName) { 187 finishedAndDispatchedRuns[targetVariable] = new List<AlgorithmConfiguration>(); 188 } 189 AlgorithmConfiguration conf = new AlgorithmConfiguration(); 190 conf.name = algoName; 191 conf.inputVariables = new List<int>(inputVariables); 192 conf.targetVariable = targetVariable; 193 finishedAndDispatchedRuns[targetVariable].Add(conf); 194 } 195 196 private bool AlgorithmFinishedOrDispatched(int targetVariable, int[] inputVariables, string algoName) { 135 197 return 136 198 finishedAndDispatchedRuns.ContainsKey(targetVariable) && 137 finishedAndDispatchedRuns[targetVariable].Contains(algoName); 199 finishedAndDispatchedRuns[targetVariable].Any(x => targetVariable == x.targetVariable && 200 algoName == x.name && 201 inputVariables.Count() == x.inputVariables.Count() && 202 inputVariables.All(v => x.inputVariables.Contains(v))); 138 203 } 139 204 }
Note: See TracChangeset
for help on using the changeset viewer.