Changeset 3545 for trunk/sources
- Timestamp:
- 04/27/10 16:25:51 (15 years ago)
- Location:
- trunk/sources
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionProblem.cs
r3539 r3545 44 44 [Creatable("Problems")] 45 45 [StorableClass] 46 public sealedclass SymbolicRegressionProblem : DataAnalysisProblem, ISingleObjectiveProblem {46 public class SymbolicRegressionProblem : DataAnalysisProblem, ISingleObjectiveProblem { 47 47 48 48 #region Parameter Properties … … 169 169 public DoubleValue PunishmentFactor { 170 170 get { return new DoubleValue(10.0); } 171 } 172 public IntValue TrainingSamplesStart { 173 get { return new IntValue(DataAnalysisProblemData.TrainingSamplesStart.Value); } 174 } 175 public IntValue TrainingSamplesEnd { 176 get { 177 return new IntValue((DataAnalysisProblemData.TrainingSamplesStart.Value + 178 DataAnalysisProblemData.TrainingSamplesEnd.Value) / 2); 179 } 180 } 181 public IntValue ValidationSamplesStart { 182 get { return TrainingSamplesEnd; } 183 } 184 public IntValue ValidationSamplesEnd { 185 get { return new IntValue(DataAnalysisProblemData.TrainingSamplesEnd.Value); } 171 186 } 172 187 #endregion … … 180 195 var visualizer = new BestValidationSymbolicRegressionSolutionVisualizer(); 181 196 var interpreter = new SimpleArithmeticExpressionInterpreter(); 182 Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the error of the regression model should be minimized.", new BoolValue(false)));197 Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the error of the regression model should be minimized.", (BoolValue)new BoolValue(false).AsReadOnly())); 183 198 Parameters.Add(new ValueParameter<SymbolicExpressionTreeCreator>("SolutionCreator", "The operator which should be used to create new symbolic regression solutions.", creator)); 184 199 Parameters.Add(new ValueParameter<ISymbolicExpressionTreeInterpreter>("SymbolicExpressionTreeInterpreter", "The interpreter that should be used to evaluate the symbolic expression tree.", interpreter)); … … 190 205 Parameters.Add(new ValueParameter<IntValue>("MaxExpressionLength", "Maximal length of the symbolic expression.", new IntValue(100))); 191 206 Parameters.Add(new ValueParameter<IntValue>("MaxExpressionDepth", "Maximal depth of the symbolic expression.", new IntValue(10))); 192 Parameters.Add(new ValueParameter<IntValue>("MaxFunctionDefiningBranches", "Maximal number of automatically defined functions.", new IntValue(3)));193 Parameters.Add(new ValueParameter<IntValue>("MaxFunctionArguments", "Maximal number of arguments of automatically defined functions.", new IntValue(3)));207 Parameters.Add(new ValueParameter<IntValue>("MaxFunctionDefiningBranches", "Maximal number of automatically defined functions.", (IntValue)new IntValue(0).AsReadOnly())); 208 Parameters.Add(new ValueParameter<IntValue>("MaxFunctionArguments", "Maximal number of arguments of automatically defined functions.", (IntValue)new IntValue(0).AsReadOnly())); 194 209 Parameters.Add(new ValueParameter<ISingleObjectiveSolutionsVisualizer>("Visualizer", "The operator which should be used to visualize symbolic regression solutions.", visualizer)); 195 210 196 211 creator.SymbolicExpressionTreeParameter.ActualName = "SymbolicRegressionModel"; 197 creator.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name; 198 creator.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionDefiningBranchesParameter.Name; 199 DataAnalysisProblemDataParameter.ValueChanged += new EventHandler(DataAnalysisProblemDataParameter_ValueChanged); 200 DataAnalysisProblemData.ProblemDataChanged += new EventHandler(DataAnalysisProblemData_Changed); 201 MaxFunctionArgumentsParameter.ValueChanged += new EventHandler(ArchitectureParameter_Changed); 202 MaxFunctionArgumentsParameter.Value.ValueChanged += new EventHandler(ArchitectureParameter_Changed); 203 MaxFunctionDefiningBranchesParameter.ValueChanged += new EventHandler(ArchitectureParameter_Changed); 204 MaxFunctionDefiningBranchesParameter.Value.ValueChanged += new EventHandler(ArchitectureParameter_Changed); 212 evaluator.QualityParameter.ActualName = "TrainingMeanSquaredError"; 213 205 214 ParameterizeSolutionCreator(); 206 215 ParameterizeEvaluator(); 207 216 ParameterizeVisualizer(); 208 217 218 UpdateGrammar(); 219 UpdateEstimationLimits(); 209 220 Initialize(); 210 221 } 211 212 222 213 223 [StorableConstructor] 214 224 private SymbolicRegressionProblem(bool deserializing) : base() { } 225 226 [StorableHook(HookType.AfterDeserialization)] 227 private void AfterDeserializationHook() { 228 Initialize(); 229 } 215 230 216 231 public override IDeepCloneable Clone(Cloner cloner) { … … 220 235 } 221 236 222 #region Events 223 void DataAnalysisProblemDataParameter_ValueChanged(object sender, EventArgs e) { 224 DataAnalysisProblemData.ProblemDataChanged += new EventHandler(DataAnalysisProblemData_Changed); 225 } 226 227 void DataAnalysisProblemData_Changed(object sender, EventArgs e) { 237 private void RegisterParameterValueEvents() { 238 MaxFunctionArgumentsParameter.ValueChanged += new EventHandler(ArchitectureParameter_ValueChanged); 239 MaxFunctionDefiningBranchesParameter.ValueChanged += new EventHandler(ArchitectureParameter_ValueChanged); 240 SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged); 241 EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged); 242 VisualizerParameter.ValueChanged += new EventHandler(VisualizerParameter_ValueChanged); 243 } 244 245 private void RegisterParameterEvents() { 246 MaxFunctionArgumentsParameter.Value.ValueChanged += new EventHandler(ArchitectureParameter_ValueChanged); 247 MaxFunctionDefiningBranchesParameter.Value.ValueChanged += new EventHandler(ArchitectureParameter_ValueChanged); 248 SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged); 249 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged); 250 } 251 252 #region event handling 253 protected override void OnDataAnalysisProblemChanged(EventArgs e) { 254 base.OnDataAnalysisProblemChanged(e); 255 // paritions could be changed 256 ParameterizeEvaluator(); 257 ParameterizeVisualizer(); 258 // input variables could have been changed 228 259 UpdateGrammar(); 229 UpdatePartitioningParameters();230 }231 232 void ArchitectureParameter_Changed(object sender,EventArgs e) {260 // estimation limits have to be recalculated 261 UpdateEstimationLimits(); 262 } 263 protected virtual void OnArchitectureParameterChanged(EventArgs e) { 233 264 var globalGrammar = FunctionTreeGrammar as GlobalSymbolicExpressionGrammar; 234 globalGrammar.MaxFunctionArguments = MaxFunctionArguments.Value; 235 globalGrammar.MaxFunctionDefinitions = MaxFunctionDefiningBranches.Value; 265 if (globalGrammar != null) { 266 globalGrammar.MaxFunctionArguments = MaxFunctionArguments.Value; 267 globalGrammar.MaxFunctionDefinitions = MaxFunctionDefiningBranches.Value; 268 } 269 } 270 protected virtual void OnGrammarChanged(EventArgs e) { } 271 protected virtual void OnOperatorsChanged(EventArgs e) { RaiseOperatorsChanged(e); } 272 protected virtual void OnSolutionCreatorChanged(EventArgs e) { 273 SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged); 274 ParameterizeSolutionCreator(); 275 OnSolutionParameterNameChanged(e); 276 RaiseSolutionCreatorChanged(e); 277 } 278 279 protected virtual void OnSolutionParameterNameChanged(EventArgs e) { 280 ParameterizeEvaluator(); 281 ParameterizeVisualizer(); 282 ParameterizeOperators(); 283 } 284 285 protected virtual void OnEvaluatorChanged(EventArgs e) { 286 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged); 287 ParameterizeEvaluator(); 288 ParameterizeVisualizer(); 289 RaiseEvaluatorChanged(e); 290 } 291 protected virtual void OnQualityParameterNameChanged(EventArgs e) { 292 ParameterizeVisualizer(); 293 } 294 protected virtual void OnVisualizerChanged(EventArgs e) { 295 ParameterizeVisualizer(); 296 RaiseVisualizerChanged(e); 297 } 298 #endregion 299 300 #region event handlers 301 private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) { 302 OnSolutionCreatorChanged(e); 303 } 304 private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) { 305 OnSolutionParameterNameChanged(e); 306 } 307 private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) { 308 OnEvaluatorChanged(e); 309 } 310 private void VisualizerParameter_ValueChanged(object sender, EventArgs e) { 311 OnVisualizerChanged(e); 312 } 313 private void ArchitectureParameter_ValueChanged(object sender, EventArgs e) { 314 OnArchitectureParameterChanged(e); 315 } 316 private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) { 317 OnQualityParameterNameChanged(e); 318 } 319 #endregion 320 321 #region events 322 public event EventHandler SolutionCreatorChanged; 323 private void RaiseSolutionCreatorChanged(EventArgs e) { 324 var changed = SolutionCreatorChanged; 325 if (changed != null) 326 changed(this, e); 327 } 328 public event EventHandler EvaluatorChanged; 329 private void RaiseEvaluatorChanged(EventArgs e) { 330 var changed = EvaluatorChanged; 331 if (changed != null) 332 changed(this, e); 333 } 334 public event EventHandler VisualizerChanged; 335 private void RaiseVisualizerChanged(EventArgs e) { 336 var changed = VisualizerChanged; 337 if (changed != null) 338 changed(this, e); 339 } 340 341 public event EventHandler OperatorsChanged; 342 private void RaiseOperatorsChanged(EventArgs e) { 343 var changed = OperatorsChanged; 344 if (changed != null) 345 changed(this, e); 346 } 347 #endregion 348 349 #region Helpers 350 private void Initialize() { 351 InitializeOperators(); 352 RegisterParameterEvents(); 353 RegisterParameterValueEvents(); 236 354 } 237 355 … … 242 360 } 243 361 244 private void UpdatePartitioningParameters() { 245 int trainingStart = DataAnalysisProblemData.TrainingSamplesStart.Value; 246 int validationEnd = DataAnalysisProblemData.TrainingSamplesEnd.Value; 247 int trainingEnd = trainingStart + (validationEnd - trainingStart) / 2; 248 int validationStart = trainingEnd; 249 var solutionVisualizer = Visualizer as BestValidationSymbolicRegressionSolutionVisualizer; 250 if (solutionVisualizer != null) { 251 solutionVisualizer.ValidationSamplesStartParameter.Value = new IntValue(validationStart); 252 solutionVisualizer.ValidationSamplesEndParameter.Value = new IntValue(validationEnd); 253 } 254 Evaluator.SamplesStartParameter.Value = new IntValue(trainingStart); 255 Evaluator.SamplesEndParameter.Value = new IntValue(trainingEnd); 256 257 if (trainingEnd - trainingStart > 0 && DataAnalysisProblemData.TargetVariable.Value != string.Empty) { 258 var targetValues = DataAnalysisProblemData.Dataset.GetVariableValues(DataAnalysisProblemData.TargetVariable.Value, trainingStart, trainingEnd); 362 private void UpdateEstimationLimits() { 363 if (TrainingSamplesStart.Value < TrainingSamplesEnd.Value && 364 DataAnalysisProblemData.Dataset.VariableNames.Contains(DataAnalysisProblemData.TargetVariable.Value)) { 365 var targetValues = DataAnalysisProblemData.Dataset.GetVariableValues(DataAnalysisProblemData.TargetVariable.Value, TrainingSamplesStart.Value, TrainingSamplesEnd.Value); 259 366 var mean = targetValues.Average(); 260 367 var range = targetValues.Max() - targetValues.Min(); … … 264 371 } 265 372 266 public event EventHandler SolutionCreatorChanged;267 private void OnSolutionCreatorChanged() {268 var changed = SolutionCreatorChanged;269 if (changed != null)270 changed(this, EventArgs.Empty);271 }272 public event EventHandler EvaluatorChanged;273 private void OnEvaluatorChanged() {274 var changed = EvaluatorChanged;275 if (changed != null)276 changed(this, EventArgs.Empty);277 }278 public event EventHandler VisualizerChanged;279 private void OnVisualizerChanged() {280 var changed = VisualizerChanged;281 if (changed != null)282 changed(this, EventArgs.Empty);283 }284 285 public event EventHandler OperatorsChanged;286 private void OnOperatorsChanged() {287 var changed = OperatorsChanged;288 if (changed != null)289 changed(this, EventArgs.Empty);290 }291 292 private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {293 SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);294 ParameterizeSolutionCreator();295 ParameterizeEvaluator();296 ParameterizeVisualizer();297 ParameterizeOperators();298 OnSolutionCreatorChanged();299 }300 private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {301 ParameterizeEvaluator();302 ParameterizeVisualizer();303 ParameterizeOperators();304 }305 private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {306 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);307 ParameterizeEvaluator();308 ParameterizeVisualizer();309 OnEvaluatorChanged();310 }311 312 private void VisualizerParameter_ValueChanged(object sender, EventArgs e) {313 ParameterizeVisualizer();314 OnVisualizerChanged();315 }316 317 private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {318 ParameterizeVisualizer();319 }320 321 #endregion322 323 #region Helpers324 [StorableHook(HookType.AfterDeserialization)]325 private void Initialize() {326 InitializeOperators();327 SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);328 SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);329 EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);330 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);331 VisualizerParameter.ValueChanged += new EventHandler(VisualizerParameter_ValueChanged);332 }333 334 373 private void InitializeOperators() { 335 374 operators = new List<ISymbolicExpressionTreeOperator>(); 336 375 operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>()); 337 376 ParameterizeOperators(); 338 UpdateGrammar();339 UpdatePartitioningParameters();340 377 } 341 378 … … 344 381 SolutionCreator.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name; 345 382 SolutionCreator.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name; 346 } 383 SolutionCreator.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name; 384 SolutionCreator.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionDefiningBranchesParameter.Name; 385 } 386 347 387 private void ParameterizeEvaluator() { 348 388 Evaluator.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName; 349 389 Evaluator.RegressionProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name; 350 Evaluator. QualityParameter.ActualName = "TrainingMeanSquaredError";351 Evaluator.Samples StartParameter.Value = new IntValue(DataAnalysisProblemData.TrainingSamplesStart.Value);352 Evaluator.SamplesEndParameter.Value = new IntValue((DataAnalysisProblemData.TrainingSamplesStart.Value + DataAnalysisProblemData.TrainingSamplesEnd.Value) / 2);353 } 390 Evaluator.SamplesStartParameter.Value = TrainingSamplesStart; 391 Evaluator.SamplesEndParameter.Value = TrainingSamplesEnd; 392 } 393 354 394 private void ParameterizeVisualizer() { 355 if (Visualizer != null) { 356 var solutionVisualizer = Visualizer as BestValidationSymbolicRegressionSolutionVisualizer; 357 if (solutionVisualizer != null) { 358 solutionVisualizer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName; 359 solutionVisualizer.DataAnalysisProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name; 360 solutionVisualizer.ValidationSamplesStartParameter.Value = new IntValue((DataAnalysisProblemData.TrainingSamplesStart.Value + DataAnalysisProblemData.TrainingSamplesEnd.Value) / 2); 361 solutionVisualizer.ValidationSamplesEndParameter.Value = new IntValue(DataAnalysisProblemData.TrainingSamplesEnd.Value); 362 } 395 var solutionVisualizer = Visualizer as BestValidationSymbolicRegressionSolutionVisualizer; 396 if (solutionVisualizer != null) { 397 solutionVisualizer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName; 398 solutionVisualizer.DataAnalysisProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name; 399 solutionVisualizer.UpperEstimationLimitParameter.ActualName = UpperEstimationLimitParameter.Name; 400 solutionVisualizer.LowerEstimationLimitParameter.ActualName = LowerEstimationLimitParameter.Name; 401 solutionVisualizer.QualityParameter.ActualName = Evaluator.QualityParameter.Name; 402 solutionVisualizer.SymbolicExpressionTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name; 403 solutionVisualizer.ValidationSamplesStartParameter.Value = ValidationSamplesStart; 404 solutionVisualizer.ValidationSamplesEndParameter.Value = ValidationSamplesEnd; 363 405 } 364 406 } … … 370 412 op.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name; 371 413 } 372 foreach (ISymbolicRegressionEvaluator op in Operators.OfType<ISymbolicRegressionEvaluator>()) {373 op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;374 op.RegressionProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;375 }376 414 foreach (ISymbolicExpressionTreeCrossover op in Operators.OfType<ISymbolicExpressionTreeCrossover>()) { 377 415 op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName; -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.3/DataAnalysisProblemDataView.Designer.cs
r3496 r3545 24 24 /// </summary> 25 25 private void InitializeComponent() { 26 this.variableCollectionView = new HeuristicLab.Core.Views.VariableCollectionView();27 26 this.importButton = new System.Windows.Forms.Button(); 28 27 ((System.ComponentModel.ISupportInitialize)(this.errorProvider)).BeginInit(); 29 28 this.SuspendLayout(); 29 // 30 // parameterCollectionView 31 // 32 this.parameterCollectionView.Size = new System.Drawing.Size(585, 366); 30 33 // 31 34 // nameTextBox … … 38 41 // 39 42 this.descriptionTextBox.Size = new System.Drawing.Size(513, 20); 40 //41 // variableCollectionView42 //43 this.variableCollectionView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)44 | System.Windows.Forms.AnchorStyles.Left)45 | System.Windows.Forms.AnchorStyles.Right)));46 this.variableCollectionView.Caption = "VariableCollection";47 this.variableCollectionView.Content = null;48 this.variableCollectionView.Location = new System.Drawing.Point(0, 52);49 this.variableCollectionView.Name = "variableCollectionView";50 this.variableCollectionView.ReadOnly = false;51 this.variableCollectionView.Size = new System.Drawing.Size(588, 366);52 this.variableCollectionView.TabIndex = 0;53 43 // 54 44 // importButton … … 68 58 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 69 59 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 70 this.Controls.Add(this.variableCollectionView);71 60 this.Controls.Add(this.importButton); 72 61 this.Name = "DataAnalysisProblemDataView"; 73 62 this.Size = new System.Drawing.Size(588, 450); 63 this.Controls.SetChildIndex(this.parameterCollectionView, 0); 74 64 this.Controls.SetChildIndex(this.importButton, 0); 75 this.Controls.SetChildIndex(this.variableCollectionView, 0);76 65 this.Controls.SetChildIndex(this.nameLabel, 0); 77 66 this.Controls.SetChildIndex(this.descriptionLabel, 0); … … 86 75 #endregion 87 76 88 private HeuristicLab.Core.Views.VariableCollectionView variableCollectionView;89 77 private System.Windows.Forms.Button importButton; 90 78 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.3/DataAnalysisProblemDataView.cs
r3462 r3545 12 12 namespace HeuristicLab.Problems.DataAnalysis.Views { 13 13 [View("Data-Analysis Problem View")] 14 [Content(typeof(DataAnalysisProblemData) )]15 public partial class DataAnalysisProblemDataView : NamedItemView {14 [Content(typeof(DataAnalysisProblemData), true)] 15 public partial class DataAnalysisProblemDataView : ParameterizedNamedItemView { 16 16 private OpenFileDialog openFileDialog; 17 17 public new DataAnalysisProblemData Content { … … 31 31 } 32 32 33 protected override void OnContentChanged() {34 base.OnContentChanged();35 variableCollectionView.Content = Content.Variables.AsReadOnly();36 }37 38 33 private void importButton_Click(object sender, EventArgs e) { 39 34 if (openFileDialog == null) openFileDialog = new OpenFileDialog(); -
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/DataAnalysisProblem.cs
r3528 r3545 50 50 public DataAnalysisProblemData DataAnalysisProblemData { 51 51 get { return DataAnalysisProblemDataParameter.Value; } 52 set { DataAnalysisProblemDataParameter.Value = value; }53 52 } 54 53 #endregion … … 57 56 : base() { 58 57 Parameters.Add(new ValueParameter<DataAnalysisProblemData>(DataAnalysisProblemDataParameterName, "The data set, target variable and input variables of the data analysis problem.", new DataAnalysisProblemData())); 58 RegisterParameterEvents(); 59 RegisterParameterValueEvents(); 59 60 } 60 61 61 62 [StorableConstructor] 62 63 private DataAnalysisProblem(bool deserializing) : base() { } 64 65 [StorableHook(HookType.AfterDeserialization)] 66 private void AfterDeserializationHook() { 67 RegisterParameterEvents(); 68 RegisterParameterValueEvents(); 69 } 70 71 #region events 72 protected virtual void OnDataAnalysisProblemChanged(EventArgs e) { } 73 74 private void RegisterParameterEvents() { 75 DataAnalysisProblemDataParameter.ValueChanged += new EventHandler(DataAnalysisProblemDataParameter_ValueChanged); 76 } 77 78 private void RegisterParameterValueEvents() { 79 DataAnalysisProblemData.ProblemDataChanged += new EventHandler(DataAnalysisProblemData_ProblemDataChanged); 80 } 81 82 private void DataAnalysisProblemDataParameter_ValueChanged(object sender, EventArgs e) { 83 OnDataAnalysisProblemChanged(e); 84 } 85 86 private void DataAnalysisProblemData_ProblemDataChanged(object sender, EventArgs e) { 87 OnDataAnalysisProblemChanged(e); 88 } 89 #endregion 90 91 public override IDeepCloneable Clone(Cloner cloner) { 92 DataAnalysisProblem clone = (DataAnalysisProblem) base.Clone(cloner); 93 clone.RegisterParameterEvents(); 94 clone.RegisterParameterValueEvents(); 95 return clone; 96 } 63 97 } 64 98 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/DataAnalysisProblemData.cs
r3442 r3545 35 35 [Item("DataAnalysisProblemData", "Represents an item containing all data defining a data analysis problem.")] 36 36 [StorableClass] 37 public class DataAnalysisProblemData : NamedItem { 38 [Storable] 39 private VariableCollection variables; 40 public VariableCollection Variables { 41 get { return variables; } 42 } 43 44 #region variable properties 45 public IVariable DatasetVariable { 46 get { return variables["Dataset"]; } 47 } 48 49 public IVariable TargetVariableVariable { 50 get { return variables["TargetVariable"]; } 51 } 52 53 public IVariable InputVariablesVariable { 54 get { return variables["InputVariables"]; } 55 } 56 57 public IVariable TrainingSamplesStartVariable { 58 get { return variables["TrainingSamplesStart"]; } 59 } 60 public IVariable TrainingSamplesEndVariable { 61 get { return variables["TrainingSamplesEnd"]; } 62 } 63 64 public IVariable TestSamplesStartVariable { 65 get { return variables["TestSamplesStart"]; } 66 } 67 public IVariable TestSamplesEndVariable { 68 get { return variables["TestSamplesEnd"]; } 37 public class DataAnalysisProblemData : ParameterizedNamedItem { 38 private bool suppressEvents = false; 39 #region parameter properties 40 public IValueParameter<Dataset> DatasetParameter { 41 get { return (IValueParameter<Dataset>)Parameters["Dataset"]; } 42 } 43 44 public IValueParameter<StringValue> TargetVariableParameter { 45 get { return (IValueParameter<StringValue>)Parameters["TargetVariable"]; } 46 } 47 48 public IValueParameter<ItemSet<StringValue>> InputVariablesParameter { 49 get { return (IValueParameter<ItemSet<StringValue>>)Parameters["InputVariables"]; } 50 } 51 52 public IValueParameter<IntValue> TrainingSamplesStartParameter { 53 get { return (IValueParameter<IntValue>)Parameters["TrainingSamplesStart"]; } 54 } 55 public IValueParameter<IntValue> TrainingSamplesEndParameter { 56 get { return (IValueParameter<IntValue>)Parameters["TrainingSamplesEnd"]; } 57 } 58 public IValueParameter<IntValue> TestSamplesStartParameter { 59 get { return (IValueParameter<IntValue>)Parameters["TestSamplesStart"]; } 60 } 61 public IValueParameter<IntValue> TestSamplesEndParameter { 62 get { return (IValueParameter<IntValue>)Parameters["TestSamplesEnd"]; } 69 63 } 70 64 #endregion … … 72 66 #region properties 73 67 public Dataset Dataset { 74 get { return (Dataset)Dataset Variable.Value; }68 get { return (Dataset)DatasetParameter.Value; } 75 69 set { 76 70 if (value != Dataset) { 77 71 if (value == null) throw new ArgumentNullException(); 78 72 if (Dataset != null) DeregisterDatasetEventHandlers(); 79 DatasetVariable.Value = value; 80 RegisterDatasetEventHandlers(); 81 OnProblemDataChanged(EventArgs.Empty); 73 DatasetParameter.Value = value; 82 74 } 83 75 } 84 76 } 85 77 public StringValue TargetVariable { 86 get { return (StringValue)TargetVariable Variable.Value; }87 set { 88 if (value != TargetVariable Variable) {78 get { return (StringValue)TargetVariableParameter.Value; } 79 set { 80 if (value != TargetVariableParameter.Value) { 89 81 if (value == null) throw new ArgumentNullException(); 90 82 if (TargetVariable != null) DeregisterStringValueEventHandlers(TargetVariable); 91 TargetVariableVariable.Value = value; 92 RegisterStringValueEventHandlers(TargetVariable); 93 OnProblemDataChanged(EventArgs.Empty); 94 } 95 } 96 } 97 public ItemList<StringValue> InputVariables { 98 get { return (ItemList<StringValue>)InputVariablesVariable.Value; } 83 TargetVariableParameter.Value = value; 84 } 85 } 86 } 87 public ItemSet<StringValue> InputVariables { 88 get { return (ItemSet<StringValue>)InputVariablesParameter.Value; } 99 89 set { 100 90 if (value != InputVariables) { 101 91 if (value == null) throw new ArgumentNullException(); 102 92 if (InputVariables != null) DeregisterInputVariablesEventHandlers(); 103 InputVariablesVariable.Value = value; 104 RegisterInputVariablesEventHandlers(); 105 OnProblemDataChanged(EventArgs.Empty); 93 InputVariablesParameter.Value = value; 106 94 } 107 95 } 108 96 } 109 97 public IntValue TrainingSamplesStart { 110 get { return (IntValue)TrainingSamplesStart Variable.Value; }98 get { return (IntValue)TrainingSamplesStartParameter.Value; } 111 99 set { 112 100 if (value != TrainingSamplesStart) { 113 101 if (value == null) throw new ArgumentNullException(); 114 102 if (TrainingSamplesStart != null) DeregisterValueTypeEventHandlers(TrainingSamplesStart); 115 TrainingSamplesStartVariable.Value = value; 116 RegisterValueTypeEventHandlers(TrainingSamplesStart); 117 OnProblemDataChanged(EventArgs.Empty); 103 TrainingSamplesStartParameter.Value = value; 118 104 } 119 105 } 120 106 } 121 107 public IntValue TrainingSamplesEnd { 122 get { return (IntValue)TrainingSamplesEnd Variable.Value; }108 get { return (IntValue)TrainingSamplesEndParameter.Value; } 123 109 set { 124 110 if (value != TrainingSamplesEnd) { 125 111 if (value == null) throw new ArgumentNullException(); 126 112 if (TrainingSamplesEnd != null) DeregisterValueTypeEventHandlers(TrainingSamplesEnd); 127 TrainingSamplesEndVariable.Value = value; 128 RegisterValueTypeEventHandlers(TrainingSamplesEnd); 129 OnProblemDataChanged(EventArgs.Empty); 113 TrainingSamplesEndParameter.Value = value; 130 114 } 131 115 } 132 116 } 133 117 public IntValue TestSamplesStart { 134 get { return (IntValue)TestSamplesStart Variable.Value; }118 get { return (IntValue)TestSamplesStartParameter.Value; } 135 119 set { 136 120 if (value != TestSamplesStart) { 137 121 if (value == null) throw new ArgumentNullException(); 138 122 if (TestSamplesStart != null) DeregisterValueTypeEventHandlers(TestSamplesStart); 139 TestSamplesStartVariable.Value = value; 140 RegisterValueTypeEventHandlers(TestSamplesStart); 141 OnProblemDataChanged(EventArgs.Empty); 123 TestSamplesStartParameter.Value = value; 142 124 } 143 125 } 144 126 } 145 127 public IntValue TestSamplesEnd { 146 get { return (IntValue)TestSamplesEnd Variable.Value; }128 get { return (IntValue)TestSamplesEndParameter.Value; } 147 129 set { 148 130 if (value != TestSamplesEnd) { 149 131 if (value == null) throw new ArgumentNullException(); 150 132 if (TestSamplesEnd != null) DeregisterValueTypeEventHandlers(TestSamplesEnd); 151 TestSamplesEndVariable.Value = value; 152 RegisterValueTypeEventHandlers(TestSamplesEnd); 153 OnProblemDataChanged(EventArgs.Empty); 133 TestSamplesEndParameter.Value = value; 154 134 } 155 135 } … … 159 139 public DataAnalysisProblemData() 160 140 : base() { 161 variables = new VariableCollection(); 162 variables.Add(new Variable("Dataset", new Dataset())); 163 variables.Add(new Variable("InputVariables", new ItemList<StringValue>())); 164 variables.Add(new Variable("TargetVariable", new StringValue())); 165 variables.Add(new Variable("TrainingSamplesStart", new IntValue())); 166 variables.Add(new Variable("TrainingSamplesEnd", new IntValue())); 167 variables.Add(new Variable("TestSamplesStart", new IntValue())); 168 variables.Add(new Variable("TestSamplesEnd", new IntValue())); 169 RegisterEventHandlers(); 170 } 141 Parameters.Add(new ValueParameter<Dataset>("Dataset", new Dataset())); 142 Parameters.Add(new ValueParameter<ItemSet<StringValue>>("InputVariables", new ItemSet<StringValue>())); 143 Parameters.Add(new ConstrainedValueParameter<StringValue>("TargetVariable")); 144 Parameters.Add(new ValueParameter<IntValue>("TrainingSamplesStart", new IntValue())); 145 Parameters.Add(new ValueParameter<IntValue>("TrainingSamplesEnd", new IntValue())); 146 Parameters.Add(new ValueParameter<IntValue>("TestSamplesStart", new IntValue())); 147 Parameters.Add(new ValueParameter<IntValue>("TestSamplesEnd", new IntValue())); 148 RegisterParameterEventHandlers(); 149 RegisterParameterValueEventHandlers(); 150 } 151 171 152 172 153 [StorableConstructor] … … 175 156 [StorableHook(HookType.AfterDeserialization)] 176 157 private void AfterDeserializationHook() { 177 RegisterEventHandlers(); 158 RegisterParameterEventHandlers(); 159 RegisterParameterValueEventHandlers(); 178 160 } 179 161 180 162 #region events 181 private void RegisterEventHandlers() { 163 public event EventHandler ProblemDataChanged; 164 protected virtual void OnProblemDataChanged(EventArgs e) { 165 if (!suppressEvents) { 166 var listeners = ProblemDataChanged; 167 if (listeners != null) listeners(this, e); 168 } 169 } 170 171 private void RegisterParameterEventHandlers() { 172 DatasetParameter.ValueChanged += new EventHandler(DatasetParameter_ValueChanged); 173 InputVariablesParameter.ValueChanged += new EventHandler(InputVariablesParameter_ValueChanged); 174 TargetVariableParameter.ValueChanged += new EventHandler(TargetVariableParameter_ValueChanged); 175 TrainingSamplesStartParameter.ValueChanged += new EventHandler(TrainingSamplesStartParameter_ValueChanged); 176 TrainingSamplesEndParameter.ValueChanged += new EventHandler(TrainingSamplesEndParameter_ValueChanged); 177 TestSamplesStartParameter.ValueChanged += new EventHandler(TestSamplesStartParameter_ValueChanged); 178 TestSamplesEndParameter.ValueChanged += new EventHandler(TestSamplesEndParameter_ValueChanged); 179 } 180 181 private void RegisterParameterValueEventHandlers() { 182 182 RegisterDatasetEventHandlers(); 183 183 RegisterInputVariablesEventHandlers(); 184 RegisterStringValueEventHandlers(TargetVariable);184 if (TargetVariable != null) RegisterStringValueEventHandlers(TargetVariable); 185 185 RegisterValueTypeEventHandlers(TrainingSamplesStart); 186 186 RegisterValueTypeEventHandlers(TrainingSamplesEnd); … … 189 189 } 190 190 191 public event EventHandler ProblemDataChanged; 192 protected virtual void OnProblemDataChanged(EventArgs e) { 193 var listeners = ProblemDataChanged; 194 if (listeners != null) listeners(this, e); 195 } 196 197 198 private void RegisterValueTypeEventHandlers<T>(ValueTypeValue<T> value) where T : struct { 199 value.ValueChanged += new EventHandler(value_ValueChanged); 200 } 201 202 private void DeregisterValueTypeEventHandlers<T>(ValueTypeValue<T> value) where T : struct { 203 value.ValueChanged -= new EventHandler(value_ValueChanged); 204 } 205 206 void value_ValueChanged(object sender, EventArgs e) { 207 OnProblemDataChanged(e); 208 } 209 210 private void RegisterStringValueEventHandlers(StringValue value) { 211 value.ValueChanged += new EventHandler(value_ValueChanged); 212 } 213 214 private void DeregisterStringValueEventHandlers(StringValue value) { 215 value.ValueChanged -= new EventHandler(value_ValueChanged); 216 } 191 192 #region parameter value changed event handlers 193 void DatasetParameter_ValueChanged(object sender, EventArgs e) { 194 RegisterDatasetEventHandlers(); 195 OnProblemDataChanged(EventArgs.Empty); 196 } 197 void InputVariablesParameter_ValueChanged(object sender, EventArgs e) { 198 RegisterInputVariablesEventHandlers(); 199 OnProblemDataChanged(EventArgs.Empty); 200 } 201 void TargetVariableParameter_ValueChanged(object sender, EventArgs e) { 202 if (TargetVariable != null) { 203 RegisterStringValueEventHandlers(TargetVariable); 204 OnProblemDataChanged(EventArgs.Empty); 205 } 206 } 207 void TrainingSamplesStartParameter_ValueChanged(object sender, EventArgs e) { 208 RegisterValueTypeEventHandlers(TrainingSamplesStart); 209 OnProblemDataChanged(EventArgs.Empty); 210 } 211 void TrainingSamplesEndParameter_ValueChanged(object sender, EventArgs e) { 212 RegisterValueTypeEventHandlers(TrainingSamplesEnd); 213 OnProblemDataChanged(EventArgs.Empty); 214 } 215 void TestSamplesStartParameter_ValueChanged(object sender, EventArgs e) { 216 RegisterValueTypeEventHandlers(TestSamplesStart); 217 OnProblemDataChanged(EventArgs.Empty); 218 } 219 void TestSamplesEndParameter_ValueChanged(object sender, EventArgs e) { 220 RegisterValueTypeEventHandlers(TestSamplesEnd); 221 OnProblemDataChanged(EventArgs.Empty); 222 } 223 #endregion 224 217 225 218 226 private void RegisterDatasetEventHandlers() { … … 241 249 242 250 private void RegisterInputVariablesEventHandlers() { 243 InputVariables.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CollectionReset); 244 InputVariables.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsAdded); 245 InputVariables.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsRemoved); 246 InputVariables.ItemsReplaced += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsReplaced); 251 InputVariables.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<StringValue>(InputVariables_CollectionReset); 252 InputVariables.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<StringValue>(InputVariables_ItemsAdded); 253 InputVariables.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<StringValue>(InputVariables_ItemsRemoved); 247 254 foreach (var item in InputVariables) 248 item.ValueChanged += new EventHandler(InputVariables_Value_ValueChanged); 255 item.ValueChanged += new EventHandler(InputVariable_ValueChanged); 256 } 257 258 void InputVariables_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<StringValue> e) { 259 foreach (var item in e.Items) 260 item.ValueChanged -= new EventHandler(InputVariable_ValueChanged); 261 OnProblemDataChanged(e); 262 } 263 264 void InputVariables_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<StringValue> e) { 265 foreach (var item in e.OldItems) 266 item.ValueChanged -= new EventHandler(InputVariable_ValueChanged); 267 OnProblemDataChanged(e); 268 } 269 270 void InputVariables_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<StringValue> e) { 271 foreach (var item in e.Items) 272 item.ValueChanged += new EventHandler(InputVariable_ValueChanged); 273 OnProblemDataChanged(e); 249 274 } 250 275 251 276 private void DeregisterInputVariablesEventHandlers() { 252 InputVariables.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CollectionReset); 253 InputVariables.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsAdded); 254 InputVariables.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsRemoved); 255 InputVariables.ItemsReplaced -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsReplaced); 277 InputVariables.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<StringValue>(InputVariables_CollectionReset); 278 InputVariables.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<StringValue>(InputVariables_ItemsAdded); 279 InputVariables.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<StringValue>(InputVariables_ItemsRemoved); 256 280 foreach (var item in InputVariables) { 257 item.ValueChanged -= new EventHandler(InputVariables_Value_ValueChanged); 258 } 259 } 260 261 void InputVariables_ItemsReplaced(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) { 262 foreach (var indexedItem in e.OldItems) 263 indexedItem.Value.ValueChanged -= new EventHandler(InputVariables_Value_ValueChanged); 264 foreach (var indexedItem in e.Items) 265 indexedItem.Value.ValueChanged += new EventHandler(InputVariables_Value_ValueChanged); 266 OnProblemDataChanged(e); 267 } 268 269 void InputVariables_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) { 270 foreach (var indexedItem in e.Items) 271 indexedItem.Value.ValueChanged -= new EventHandler(InputVariables_Value_ValueChanged); 272 OnProblemDataChanged(e); 273 } 274 275 void InputVariables_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) { 276 foreach (var indexedItem in e.Items) 277 indexedItem.Value.ValueChanged += new EventHandler(InputVariables_Value_ValueChanged); 278 OnProblemDataChanged(e); 279 } 280 281 void InputVariables_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) { 282 foreach (var indexedItem in e.OldItems) 283 indexedItem.Value.ValueChanged -= new EventHandler(InputVariables_Value_ValueChanged); 284 285 OnProblemDataChanged(e); 286 } 287 void InputVariables_Value_ValueChanged(object sender, EventArgs e) { 288 OnProblemDataChanged(e); 289 } 290 281 item.ValueChanged -= new EventHandler(InputVariable_ValueChanged); 282 } 283 } 284 285 void InputVariable_ValueChanged(object sender, EventArgs e) { 286 OnProblemDataChanged(e); 287 } 288 #region helper 289 290 private void RegisterValueTypeEventHandlers<T>(ValueTypeValue<T> value) where T : struct { 291 value.ValueChanged += new EventHandler(value_ValueChanged); 292 } 293 294 private void DeregisterValueTypeEventHandlers<T>(ValueTypeValue<T> value) where T : struct { 295 value.ValueChanged -= new EventHandler(value_ValueChanged); 296 } 297 298 void value_ValueChanged(object sender, EventArgs e) { 299 OnProblemDataChanged(e); 300 } 301 302 private void RegisterStringValueEventHandlers(StringValue value) { 303 value.ValueChanged += new EventHandler(value_ValueChanged); 304 } 305 306 private void DeregisterStringValueEventHandlers(StringValue value) { 307 value.ValueChanged -= new EventHandler(value_ValueChanged); 308 } 309 310 #endregion 291 311 #endregion 292 312 … … 294 314 var csvFileParser = new CsvFileParser(); 295 315 csvFileParser.Parse(fileName); 316 suppressEvents = true; 296 317 Name = "Data imported from " + Path.GetFileName(fileName); 297 318 Dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values); 298 319 Dataset.Name = Path.GetFileName(fileName); 299 TargetVariable = new StringValue(Dataset.VariableNames.First()); 300 InputVariables = new ItemList<StringValue>(Dataset.VariableNames.Skip(1).Select(s => new StringValue(s))); 320 var variableNames = Dataset.VariableNames.Select(x => new StringValue(x).AsReadOnly()).ToList(); 321 ((ConstrainedValueParameter<StringValue>)TargetVariableParameter).ValidValues.Clear(); 322 foreach (var variableName in variableNames) 323 ((ConstrainedValueParameter<StringValue>)TargetVariableParameter).ValidValues.Add(variableName); 324 TargetVariable = variableNames.First(); 325 InputVariables = new ItemSet<StringValue>(variableNames.Skip(1)); 301 326 int middle = (int)(csvFileParser.Rows * 0.5); 302 327 TrainingSamplesStart = new IntValue(0); … … 304 329 TestSamplesStart = new IntValue(middle); 305 330 TestSamplesEnd = new IntValue(csvFileParser.Rows); 331 suppressEvents = false; 332 OnProblemDataChanged(EventArgs.Empty); 306 333 } 307 334 308 335 public override IDeepCloneable Clone(Cloner cloner) { 309 336 DataAnalysisProblemData clone = (DataAnalysisProblemData)base.Clone(cloner); 310 clone.variables = (VariableCollection)variables.Clone(cloner); 311 312 clone.RegisterEventHandlers(); 337 clone.RegisterParameterEventHandlers(); 338 clone.RegisterParameterValueEventHandlers(); 313 339 return clone; 314 340 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/SimpleArithmeticExpressionInterpreter.cs
r3513 r3545 52 52 private Instruction[] code; 53 53 private int pc; 54 55 public override bool CanChangeName { 56 get { return false; } 57 } 58 public override bool CanChangeDescription { 59 get { return false; } 60 } 54 61 55 62 public SimpleArithmeticExpressionInterpreter()
Note: See TracChangeset
for help on using the changeset viewer.