- Timestamp:
- 08/14/09 19:56:04 (15 years ago)
- Location:
- trunk/sources
- Files:
-
- 16 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.CEDMA.Server/3.3/DispatcherBase.cs
r2258 r2290 37 37 namespace HeuristicLab.CEDMA.Server { 38 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 Members112 113 public IView CreateView() {114 return new DispatcherView(this);115 }116 117 #endregion118 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 39 } 160 40 } -
trunk/sources/HeuristicLab.CEDMA.Server/3.3/DispatcherView.Designer.cs
r2258 r2290 29 29 this.inputVariablesLabel = new System.Windows.Forms.Label(); 30 30 this.splitContainer = new System.Windows.Forms.SplitContainer(); 31 this.algorithmsListBox = new System.Windows.Forms.CheckedListBox(); 32 this.algorithmsLabel = new System.Windows.Forms.Label(); 31 33 this.setAllButton = new System.Windows.Forms.Button(); 32 34 this.splitContainer.Panel1.SuspendLayout(); … … 42 44 this.targetVariableList.FormattingEnabled = true; 43 45 this.targetVariableList.HorizontalScrollbar = true; 44 this.targetVariableList.Location = new System.Drawing.Point(6, 1 6);46 this.targetVariableList.Location = new System.Drawing.Point(6, 181); 45 47 this.targetVariableList.Name = "targetVariableList"; 46 this.targetVariableList.Size = new System.Drawing.Size(193, 454);48 this.targetVariableList.Size = new System.Drawing.Size(193, 289); 47 49 this.targetVariableList.TabIndex = 0; 48 50 this.targetVariableList.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.targetVariableList_ItemCheck); … … 65 67 // 66 68 this.targetVariablesLabel.AutoSize = true; 67 this.targetVariablesLabel.Location = new System.Drawing.Point(3, 0);69 this.targetVariablesLabel.Location = new System.Drawing.Point(3, 165); 68 70 this.targetVariablesLabel.Name = "targetVariablesLabel"; 69 71 this.targetVariablesLabel.Size = new System.Drawing.Size(86, 13); … … 88 90 // splitContainer.Panel1 89 91 // 92 this.splitContainer.Panel1.Controls.Add(this.algorithmsListBox); 93 this.splitContainer.Panel1.Controls.Add(this.algorithmsLabel); 90 94 this.splitContainer.Panel1.Controls.Add(this.targetVariablesLabel); 91 95 this.splitContainer.Panel1.Controls.Add(this.targetVariableList); … … 100 104 this.splitContainer.SplitterWidth = 1; 101 105 this.splitContainer.TabIndex = 4; 106 // 107 // algorithmsListBox 108 // 109 this.algorithmsListBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 110 | System.Windows.Forms.AnchorStyles.Right))); 111 this.algorithmsListBox.FormattingEnabled = true; 112 this.algorithmsListBox.HorizontalScrollbar = true; 113 this.algorithmsListBox.Location = new System.Drawing.Point(6, 16); 114 this.algorithmsListBox.Name = "algorithmsListBox"; 115 this.algorithmsListBox.Size = new System.Drawing.Size(193, 139); 116 this.algorithmsListBox.TabIndex = 4; 117 this.algorithmsListBox.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.algorithmsListBox_ItemCheck); 118 // 119 // algorithmsLabel 120 // 121 this.algorithmsLabel.AutoSize = true; 122 this.algorithmsLabel.Location = new System.Drawing.Point(3, 0); 123 this.algorithmsLabel.Name = "algorithmsLabel"; 124 this.algorithmsLabel.Size = new System.Drawing.Size(58, 13); 125 this.algorithmsLabel.TabIndex = 3; 126 this.algorithmsLabel.Text = "Algorithms:"; 102 127 // 103 128 // setAllButton … … 136 161 private System.Windows.Forms.SplitContainer splitContainer; 137 162 private System.Windows.Forms.Button setAllButton; 163 private System.Windows.Forms.CheckedListBox algorithmsListBox; 164 private System.Windows.Forms.Label algorithmsLabel; 138 165 } 139 166 } -
trunk/sources/HeuristicLab.CEDMA.Server/3.3/DispatcherView.cs
r2258 r2290 11 11 namespace HeuristicLab.CEDMA.Server { 12 12 public partial class DispatcherView : ViewBase { 13 private DispatcherBasedispatcher;14 public DispatcherView( DispatcherBasedispatcher)13 private SimpleDispatcher dispatcher; 14 public DispatcherView(SimpleDispatcher dispatcher) 15 15 : base() { 16 16 this.dispatcher = dispatcher; … … 26 26 } else { 27 27 base.UpdateControls(); 28 algorithmsListBox.Items.Clear(); 28 29 targetVariableList.Items.Clear(); 29 30 inputVariableList.Items.Clear(); … … 35 36 foreach (string inputVar in dispatcher.InputVariables) { 36 37 inputVariableList.Items.Add(inputVar, false); 38 } 39 40 foreach (HeuristicLab.Modeling.IAlgorithm algo in dispatcher.Algorithms) { 41 algorithmsListBox.Items.Add(algo, false); 37 42 } 38 43 targetVariableList.ClearSelected(); … … 83 88 } 84 89 } 90 91 private void algorithmsListBox_ItemCheck(object sender, ItemCheckEventArgs e) { 92 if(e.NewValue == CheckState.Checked) { 93 dispatcher.EnableAlgorithm((HeuristicLab.Modeling.IAlgorithm)algorithmsListBox.Items[e.Index]); 94 } else if(e.NewValue == CheckState.Unchecked) { 95 dispatcher.DisableAlgorithm((HeuristicLab.Modeling.IAlgorithm)algorithmsListBox.Items[e.Index]); 96 } 97 } 85 98 } 86 99 } -
trunk/sources/HeuristicLab.CEDMA.Server/3.3/ExecuterBase.cs
r2238 r2290 58 58 } 59 59 60 public int CalculatedJobs { 61 get; 62 set; 63 } 64 public int StoredJobs { 65 get; 66 set; 67 } 68 60 69 public ExecuterBase(IDispatcher dispatcher, IModelingDatabase databaseService) { 70 CalculatedJobs = 0; 61 71 maxActiveJobs = 10; 62 72 this.dispatcher = dispatcher; 63 73 this.databaseService = databaseService; 74 StoredJobs = databaseService.GetAllModels().Count(); 64 75 } 65 76 … … 83 94 84 95 protected void StoreResults(HeuristicLab.Modeling.IAlgorithm finishedAlgorithm) { 96 CalculatedJobs++; 85 97 databaseService.Persist(finishedAlgorithm); 98 StoredJobs++; 99 OnChanged(); 86 100 } 87 101 -
trunk/sources/HeuristicLab.CEDMA.Server/3.3/ExecuterView.Designer.cs
r2125 r2290 27 27 this.maxJobsLabel = new System.Windows.Forms.Label(); 28 28 this.maxActiveJobs = new System.Windows.Forms.NumericUpDown(); 29 this.finishedLabel = new System.Windows.Forms.Label(); 30 this.finishedTextBox = new System.Windows.Forms.TextBox(); 29 31 ((System.ComponentModel.ISupportInitialize)(this.maxActiveJobs)).BeginInit(); 30 32 this.SuspendLayout(); … … 36 38 | System.Windows.Forms.AnchorStyles.Right))); 37 39 this.jobsList.FormattingEnabled = true; 38 this.jobsList.Location = new System.Drawing.Point(3, 29);40 this.jobsList.Location = new System.Drawing.Point(3, 55); 39 41 this.jobsList.Name = "jobsList"; 40 this.jobsList.Size = new System.Drawing.Size(327, 2 64);42 this.jobsList.Size = new System.Drawing.Size(327, 238); 41 43 this.jobsList.TabIndex = 0; 42 44 // … … 52 54 // maxActiveJobs 53 55 // 54 this.maxActiveJobs.Location = new System.Drawing.Point( 98, 3);56 this.maxActiveJobs.Location = new System.Drawing.Point(117, 3); 55 57 this.maxActiveJobs.Maximum = new decimal(new int[] { 56 58 200, … … 63 65 this.maxActiveJobs.ValueChanged += new System.EventHandler(this.maxActiveJobs_ValueChanged); 64 66 // 67 // finishedLabel 68 // 69 this.finishedLabel.AutoSize = true; 70 this.finishedLabel.Location = new System.Drawing.Point(5, 32); 71 this.finishedLabel.Name = "finishedLabel"; 72 this.finishedLabel.Size = new System.Drawing.Size(106, 13); 73 this.finishedLabel.TabIndex = 3; 74 this.finishedLabel.Text = "Stored models (new):"; 75 // 76 // finishedTextBox 77 // 78 this.finishedTextBox.Location = new System.Drawing.Point(117, 29); 79 this.finishedTextBox.Name = "finishedTextBox"; 80 this.finishedTextBox.ReadOnly = true; 81 this.finishedTextBox.Size = new System.Drawing.Size(100, 20); 82 this.finishedTextBox.TabIndex = 4; 83 // 65 84 // ExecuterView 66 85 // 67 86 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 68 87 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 88 this.Controls.Add(this.finishedTextBox); 89 this.Controls.Add(this.finishedLabel); 69 90 this.Controls.Add(this.maxActiveJobs); 70 91 this.Controls.Add(this.maxJobsLabel); … … 83 104 private System.Windows.Forms.Label maxJobsLabel; 84 105 private System.Windows.Forms.NumericUpDown maxActiveJobs; 106 private System.Windows.Forms.Label finishedLabel; 107 private System.Windows.Forms.TextBox finishedTextBox; 85 108 } 86 109 } -
trunk/sources/HeuristicLab.CEDMA.Server/3.3/ExecuterView.cs
r2094 r2290 24 24 else { 25 25 base.UpdateControls(); 26 finishedTextBox.Text = executer.StoredJobs+" ("+executer.CalculatedJobs+")"; 26 27 maxActiveJobs.Value = executer.MaxActiveJobs; 27 28 jobsList.DataSource = executer.GetJobs(); -
trunk/sources/HeuristicLab.CEDMA.Server/3.3/HeuristicLab.CEDMA.Server-3.3.csproj
r2223 r2290 113 113 <Compile Include="ExecuterBase.cs" /> 114 114 <Compile Include="IExecuter.cs" /> 115 <Compile Include="DispatcherBase.cs" />116 115 <Compile Include="GridExecuter.cs" /> 117 116 <Compile Include="IDispatcher.cs" /> -
trunk/sources/HeuristicLab.CEDMA.Server/3.3/ProblemView.cs
r2285 r2290 111 111 private void autoregressiveCheckBox_CheckedChanged(object sender, EventArgs e) { 112 112 problem.AutoRegressive = autoregressiveCheckBox.Checked; 113 problem.FireChanged(); 113 114 } 114 115 … … 145 146 problem.TestSamplesStart = int.Parse(testSamplesStartTextBox.Text); 146 147 problem.TestSamplesEnd = int.Parse(testSamplesEndTextBox.Text); 148 problem.FireChanged(); 147 149 } 148 150 … … 193 195 } 194 196 195 #region gui events196 197 private void radioButton_CheckedChanged(object sender, EventArgs e) { 197 198 minTimeOffsetLabel.Enabled = timeSeriesRadioButton.Checked; … … 204 205 else if (classificationRadioButton.Checked) problem.LearningTask = LearningTask.Classification; 205 206 else if (regressionRadioButton.Checked) problem.LearningTask = LearningTask.Regression; 207 problem.FireChanged(); 206 208 } 207 209 … … 215 217 problem.MinTimeOffset = int.Parse(minTimeOffsetTextBox.Text); 216 218 problem.MaxTimeOffset = int.Parse(maxTimeOffsetTextBox.Text); 217 }218 #endregion219 problem.FireChanged(); 220 } 219 221 } 220 222 } -
trunk/sources/HeuristicLab.CEDMA.Server/3.3/SimpleDispatcher.cs
r2275 r2290 36 36 37 37 namespace HeuristicLab.CEDMA.Server { 38 public class SimpleDispatcher : DispatcherBase{38 public class SimpleDispatcher : IDispatcher { 39 39 private class AlgorithmConfiguration { 40 40 public string name; … … 43 43 } 44 44 45 private IModelingDatabase database; 46 public IModelingDatabase Database { 47 get { 48 return database; 49 } 50 } 51 52 private Problem problem; 53 public Problem Problem { 54 get { 55 return problem; 56 } 57 } 58 internal event EventHandler Changed; 59 60 public IEnumerable<string> TargetVariables { 61 get { 62 return Enumerable.Range(0, problem.Dataset.Columns).Select(x => problem.Dataset.GetVariableName(x)); 63 } 64 } 65 66 public IEnumerable<string> InputVariables { 67 get { 68 return TargetVariables; 69 } 70 } 71 72 private HeuristicLab.Modeling.IAlgorithm[] algorithms; 73 public IEnumerable<HeuristicLab.Modeling.IAlgorithm> Algorithms { 74 get { 75 switch (Problem.LearningTask) { 76 case LearningTask.Regression: { 77 return algorithms.Where(a => (a as IClassificationAlgorithm) == null && (a as ITimeSeriesAlgorithm) == null); 78 } 79 case LearningTask.Classification: { 80 return algorithms.Where(a => (a as IClassificationAlgorithm) != null); 81 } 82 case LearningTask.TimeSeries: { 83 return algorithms.Where(a => (a as ITimeSeriesAlgorithm) != null); 84 } 85 } 86 return new HeuristicLab.Modeling.IAlgorithm[] { }; 87 } 88 } 89 90 private List<HeuristicLab.Modeling.IAlgorithm> activeAlgorithms; 91 public IEnumerable<HeuristicLab.Modeling.IAlgorithm> ActiveAlgorithms { 92 get { return activeAlgorithms; } 93 } 94 45 95 private Random random; 96 private List<int> allowedTargetVariables; 97 private Dictionary<int, List<int>> activeInputVariables; 46 98 private Dictionary<int, List<AlgorithmConfiguration>> finishedAndDispatchedRuns; 47 48 public SimpleDispatcher(IModelingDatabase database, Problem problem) 49 : base(database, problem) { 50 random = new Random(); 51 finishedAndDispatchedRuns = new Dictionary<int, List<AlgorithmConfiguration>>(); 99 private object locker = new object(); 100 101 public SimpleDispatcher(IModelingDatabase database, Problem problem) { 102 this.problem = problem; 103 this.database = database; 104 this.random = new Random(); 105 106 this.finishedAndDispatchedRuns = new Dictionary<int, List<AlgorithmConfiguration>>(); 107 this.allowedTargetVariables = new List<int>(); 108 this.activeInputVariables = new Dictionary<int, List<int>>(); 109 this.activeAlgorithms = new List<HeuristicLab.Modeling.IAlgorithm>(); 110 DiscoveryService ds = new DiscoveryService(); 111 this.algorithms = ds.GetInstances<HeuristicLab.Modeling.IAlgorithm>(); 112 problem.Changed += (sender, args) => { 113 lock (locker) { 114 allowedTargetVariables.Clear(); 115 activeInputVariables.Clear(); 116 activeAlgorithms.Clear(); 117 } 118 OnChanged(); 119 }; 120 52 121 PopulateFinishedRuns(); 53 122 } 54 123 55 public override HeuristicLab.Modeling.IAlgorithm SelectAndConfigureAlgorithm(int targetVariable, int[] inputVariables, Problem problem) { 124 public HeuristicLab.Modeling.IAlgorithm GetNextJob() { 125 lock (locker) { 126 if (allowedTargetVariables.Count > 0) { 127 int[] targetVariables = allowedTargetVariables.ToArray(); 128 int targetVariable = SelectTargetVariable(targetVariables); 129 int[] inputVariables = activeInputVariables[targetVariable].ToArray(); 130 131 HeuristicLab.Modeling.IAlgorithm selectedAlgorithm = SelectAndConfigureAlgorithm(targetVariable, inputVariables, problem); 132 133 return selectedAlgorithm; 134 } else return null; 135 } 136 } 137 138 public virtual int SelectTargetVariable(int[] targetVariables) { 139 return targetVariables[random.Next(targetVariables.Length)]; 140 } 141 142 public HeuristicLab.Modeling.IAlgorithm SelectAndConfigureAlgorithm(int targetVariable, int[] inputVariables, Problem problem) { 143 HeuristicLab.Modeling.IAlgorithm selectedAlgorithm = null; 56 144 DiscoveryService ds = new DiscoveryService(); 57 HeuristicLab.Modeling.IAlgorithm[] algos = ds.GetInstances<HeuristicLab.Modeling.IAlgorithm>(); 58 HeuristicLab.Modeling.IAlgorithm selectedAlgorithm = null; 59 switch (problem.LearningTask) { 60 case LearningTask.Regression: { 61 var regressionAlgos = algos.Where(a => (a as IClassificationAlgorithm) == null && (a as ITimeSeriesAlgorithm) == null); 62 selectedAlgorithm = ChooseDeterministic(targetVariable, inputVariables, regressionAlgos) ?? ChooseStochastic(regressionAlgos); 63 break; 64 } 65 case LearningTask.Classification: { 66 var classificationAlgos = algos.Where(a => (a as IClassificationAlgorithm) != null); 67 selectedAlgorithm = ChooseDeterministic(targetVariable, inputVariables, classificationAlgos) ?? ChooseStochastic(classificationAlgos); 68 break; 69 } 70 case LearningTask.TimeSeries: { 71 var timeSeriesAlgos = algos.Where(a => (a as ITimeSeriesAlgorithm) != null); 72 selectedAlgorithm = ChooseDeterministic(targetVariable, inputVariables, timeSeriesAlgos) ?? ChooseStochastic(timeSeriesAlgos); 73 break; 74 } 75 } 76 77 145 var allAlgorithms = ds.GetInstances<HeuristicLab.Modeling.IAlgorithm>(); 146 var allowedAlgorithmTypes = activeAlgorithms.Select(x => x.GetType()); 147 var possibleAlgos = 148 allAlgorithms 149 .Where(x => allowedAlgorithmTypes.Contains(x.GetType()) && 150 ((x is IStochasticAlgorithm) || !AlgorithmFinishedOrDispatched(targetVariable, inputVariables, x.Name))); 151 if (possibleAlgos.Count() > 0) selectedAlgorithm = possibleAlgos.ElementAt(random.Next(possibleAlgos.Count())); 78 152 if (selectedAlgorithm != null) { 79 153 SetProblemParameters(selectedAlgorithm, problem, targetVariable, inputVariables); 80 AddDispatchedRun(targetVariable, inputVariables, selectedAlgorithm.Name); 154 if (!(selectedAlgorithm is IStochasticAlgorithm)) 155 AddDispatchedRun(targetVariable, inputVariables, selectedAlgorithm.Name); 81 156 } 82 157 return selectedAlgorithm; 83 }84 85 private HeuristicLab.Modeling.IAlgorithm ChooseDeterministic(int targetVariable, int[] inputVariables, IEnumerable<HeuristicLab.Modeling.IAlgorithm> algos) {86 var deterministicAlgos = algos87 .Where(a => (a as IStochasticAlgorithm) == null)88 .Where(a => AlgorithmFinishedOrDispatched(targetVariable, inputVariables, a.Name) == false);89 90 if (deterministicAlgos.Count() == 0) return null;91 return deterministicAlgos.ElementAt(random.Next(deterministicAlgos.Count()));92 }93 94 private HeuristicLab.Modeling.IAlgorithm ChooseStochastic(IEnumerable<HeuristicLab.Modeling.IAlgorithm> regressionAlgos) {95 var stochasticAlgos = regressionAlgos.Where(a => (a as IStochasticAlgorithm) != null);96 if (stochasticAlgos.Count() == 0) return null;97 return stochasticAlgos.ElementAt(random.Next(stochasticAlgos.Count()));98 158 } 99 159 … … 169 229 inputVariables.All(v => x.inputVariables.Contains(v))); 170 230 } 231 232 public void EnableAlgorithm(HeuristicLab.Modeling.IAlgorithm algo) { 233 lock (locker) { 234 if (!activeAlgorithms.Contains(algo)) activeAlgorithms.Add(algo); 235 } 236 } 237 238 public void DisableAlgorithm(HeuristicLab.Modeling.IAlgorithm algo) { 239 lock (locker) { 240 while (activeAlgorithms.Remove(algo)) ; 241 } 242 } 243 244 internal void EnableTargetVariable(string name) { 245 int varIndex = problem.Dataset.GetVariableIndex(name); 246 lock (locker) 247 if (!allowedTargetVariables.Contains(varIndex)) allowedTargetVariables.Add(varIndex); 248 } 249 250 internal void DisableTargetVariable(string name) { 251 int varIndex = problem.Dataset.GetVariableIndex(name); 252 lock (locker) 253 while (allowedTargetVariables.Remove(varIndex)) ; 254 } 255 256 internal void EnableInputVariable(string target, string name) { 257 int targetIndex = problem.Dataset.GetVariableIndex(target); 258 int inputIndex = problem.Dataset.GetVariableIndex(name); 259 lock (locker) { 260 if (!activeInputVariables.ContainsKey(targetIndex)) activeInputVariables[targetIndex] = new List<int>(); 261 if (!activeInputVariables[targetIndex].Contains(inputIndex)) { 262 activeInputVariables[targetIndex].Add(inputIndex); 263 } 264 } 265 } 266 267 internal void DisableInputVariable(string target, string name) { 268 int targetIndex = problem.Dataset.GetVariableIndex(target); 269 int inputIndex = problem.Dataset.GetVariableIndex(name); 270 lock (locker) { 271 if (!activeInputVariables.ContainsKey(targetIndex)) activeInputVariables[targetIndex] = new List<int>(); 272 while (activeInputVariables[targetIndex].Remove(inputIndex)) ; 273 } 274 } 275 276 public void OnChanged() { 277 if (Changed != null) Changed(this, new EventArgs()); 278 } 279 280 internal IEnumerable<string> GetInputVariables(string target) { 281 int targetIndex = problem.Dataset.GetVariableIndex(target); 282 lock (locker) { 283 if (!activeInputVariables.ContainsKey(targetIndex)) activeInputVariables[targetIndex] = new List<int>(); 284 return activeInputVariables[targetIndex] 285 .Select(i => problem.Dataset.GetVariableName(i)); 286 } 287 } 288 289 290 #region IViewable Members 291 292 public virtual IView CreateView() { 293 return new DispatcherView(this); 294 } 295 296 #endregion 297 298 171 299 } 172 300 } -
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/BaseClasses/AlgorithmBase.cs
r2285 r2290 412 412 Engine.GlobalScope.AddSubScope(bestModelScope); 413 413 IGeneticProgrammingModel tree = bestModelScope.GetVariableValue<IGeneticProgrammingModel>("FunctionTree", false); 414 ITreeEvaluator evaluator = bestModelScope.GetVariableValue<ITreeEvaluator>("TreeEvaluator", false);414 ITreeEvaluator evaluator = bestModelScope.GetVariableValue<ITreeEvaluator>("TreeEvaluator", true); 415 415 IAnalyzerModel model = new AnalyzerModel(); 416 416 model.Predictor = new Predictor(evaluator, tree); -
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/VariableEvaluationImpactCalculator.cs
r2285 r2290 32 32 AddVariableInfo(new VariableInfo("TreeEvaluator", "The evaluator that should be used to evaluate the expression tree", typeof(ITreeEvaluator), VariableKind.In)); 33 33 AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be evaluated", typeof(IGeneticProgrammingModel), VariableKind.In)); 34 AddVariableInfo(new VariableInfo("TreeSize", "Size (number of nodes) of the tree to evaluate", typeof(IntData), VariableKind.In));35 34 } 36 35 … … 39 38 ITreeEvaluator evaluator = GetVariableValue<ITreeEvaluator>("TreeEvaluator", scope, true); 40 39 IGeneticProgrammingModel gpModel = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope, true); 41 double punishmentFactor = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data;42 40 evaluator.PrepareForEvaluation(dataset, targetVariable, start, end, gpModel.FunctionTree); 43 41 -
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/VariableQualityImpactCalculator.cs
r2285 r2290 38 38 ITreeEvaluator evaluator = GetVariableValue<ITreeEvaluator>("TreeEvaluator", scope, true); 39 39 IGeneticProgrammingModel gpModel = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope, true); 40 double punishmentFactor = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data;41 40 evaluator.PrepareForEvaluation(dataset, targetVariable, start, end, gpModel.FunctionTree); 42 41 -
trunk/sources/HeuristicLab.LinearRegression/3.2/LinearRegression.cs
r2285 r2290 270 270 protected internal virtual IAnalyzerModel CreateLRModel(IScope bestModelScope) { 271 271 IGeneticProgrammingModel tree = bestModelScope.GetVariableValue<IGeneticProgrammingModel>("LinearRegressionModel", false); 272 ITreeEvaluator evaluator = bestModelScope.GetVariableValue<ITreeEvaluator>("TreeEvaluator", false);272 ITreeEvaluator evaluator = bestModelScope.GetVariableValue<ITreeEvaluator>("TreeEvaluator", true); 273 273 IAnalyzerModel model = new AnalyzerModel(); 274 274 model.Predictor = new Predictor(evaluator, tree); -
trunk/sources/HeuristicLab.SupportVectorMachines/3.2/Predictor.cs
r2285 r2290 34 34 public class Predictor : ItemBase, IPredictor { 35 35 private SVMModel svmModel; 36 private Dictionary<string, int> variableNames = new Dictionary<string, int>(); 36 37 private string targetVariable; 37 38 38 39 public Predictor() : base() { } // for persistence 39 40 40 public Predictor(SVMModel model, string targetVariable )41 public Predictor(SVMModel model, string targetVariable, Dictionary<string, int> variableNames) 41 42 : base() { 42 43 this.svmModel = model; 43 44 this.targetVariable = targetVariable; 45 this.variableNames = variableNames; 44 46 } 45 47 … … 49 51 RangeTransform transform = svmModel.RangeTransform; 50 52 Model model = svmModel.Model; 53 // maps columns of the current input dataset to the columns that were originally used in training 54 Dictionary<int, int> newIndex = new Dictionary<int, int>(); 55 foreach (var pair in variableNames) { 56 newIndex[input.GetVariableIndex(pair.Key)] = pair.Value; 57 } 51 58 52 Problem p = SVMHelper.CreateSVMProblem(input, input.GetVariableIndex(targetVariable), start, end);59 Problem p = SVMHelper.CreateSVMProblem(input, input.GetVariableIndex(targetVariable), newIndex, start, end); 53 60 Problem scaledProblem = SVM.Scaling.Scale(p, transform); 54 61 … … 70 77 clone.svmModel = (SVMModel)Auxiliary.Clone(svmModel, clonedObjects); 71 78 clone.targetVariable = targetVariable; 79 clone.variableNames = new Dictionary<string, int>(variableNames); 72 80 return clone; 73 81 } … … 79 87 node.Attributes.Append(targetVarAttr); 80 88 node.AppendChild(PersistenceManager.Persist(svmModel, document, persistedObjects)); 89 XmlNode variablesNode = document.CreateElement("Variables"); 90 foreach (var pair in variableNames) { 91 XmlNode pairNode = document.CreateElement("Variable"); 92 XmlAttribute nameAttr = document.CreateAttribute("Name"); 93 XmlAttribute indexAttr = document.CreateAttribute("Index"); 94 nameAttr.Value = pair.Key; 95 indexAttr.Value = XmlConvert.ToString(pair.Value); 96 pairNode.Attributes.Append(nameAttr); 97 pairNode.Attributes.Append(indexAttr); 98 variablesNode.AppendChild(pairNode); 99 } 100 node.AppendChild(variablesNode); 81 101 return node; 82 102 } … … 86 106 targetVariable = node.Attributes["TargetVariable"].Value; 87 107 svmModel = (SVMModel)PersistenceManager.Restore(node.ChildNodes[0], restoredObjects); 108 109 variableNames = new Dictionary<string, int>(); 110 XmlNode variablesNode = node.ChildNodes[1]; 111 foreach (XmlNode pairNode in variablesNode.ChildNodes) { 112 variableNames[pairNode.Attributes["Name"].Value] = XmlConvert.ToInt32(pairNode.Attributes["Index"].Value); 113 } 88 114 } 89 115 } -
trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SVMHelper.cs
r2285 r2290 9 9 namespace HeuristicLab.SupportVectorMachines { 10 10 public class SVMHelper { 11 11 12 public static SVM.Problem CreateSVMProblem(Dataset dataset, int targetVariable, int start, int end) { 13 return CreateSVMProblem(dataset, targetVariable, Enumerable.Range(0, dataset.Columns).ToDictionary<int, int>(x => x), start, end); 14 } 15 16 public static SVM.Problem CreateSVMProblem(Dataset dataset, int targetVariable, Dictionary<int, int> columnMapping, int start, int end) { 12 17 int rowCount = end - start; 13 18 List<int> skippedFeatures = new List<int>(); … … 24 29 for (int i = 0; i < rowCount; i++) { 25 30 double value = dataset.GetValue(start + i, targetVariable); 26 31 targetVector[i] = value; 27 32 } 28 targetVector = targetVector.Where(x => !double.IsNaN(x)).ToArray();33 targetVector = targetVector.Where(x => !double.IsNaN(x)).ToArray(); 29 34 30 35 SVM.Node[][] nodes = new SVM.Node[targetVector.Length][]; … … 34 39 tempRow = new List<SVM.Node>(); 35 40 for (int col = 0; col < dataset.Columns; col++) { 36 if (!skippedFeatures.Contains(col) && col !=targetVariable) {41 if (!skippedFeatures.Contains(col) && col != targetVariable && columnMapping.ContainsKey(col)) { 37 42 double value = dataset.GetValue(start + row, col); 38 43 if (!double.IsNaN(value)) 39 tempRow.Add(new SVM.Node(col + 1, value));44 tempRow.Add(new SVM.Node(columnMapping[col], value)); 40 45 } 41 46 } -
trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SupportVectorRegression.cs
r2285 r2290 448 448 model.TestSamplesStart = bestModelScope.GetVariableValue<IntData>("TestSamplesStart", true).Data; 449 449 model.TestSamplesEnd = bestModelScope.GetVariableValue<IntData>("TestSamplesEnd", true).Data; 450 model.Predictor = new Predictor(bestModelScope.GetVariableValue<SVMModel>("Model", false), model.TargetVariable); 450 Dictionary<string, int> variableNames = new Dictionary<string, int>(); 451 for (int i = 0; i < ds.Columns; i++) variableNames[ds.GetVariableName(i)] = i; 452 model.Predictor = new Predictor(bestModelScope.GetVariableValue<SVMModel>("Model", false), model.TargetVariable, variableNames); 451 453 452 454
Note: See TracChangeset
for help on using the changeset viewer.