Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/27/10 16:25:51 (15 years ago)
Author:
gkronber
Message:

Fixed bugs in wiring of data analysis and symbolic regression problems. #938 (Data types and operators for regression problems)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionProblem.cs

    r3539 r3545  
    4444  [Creatable("Problems")]
    4545  [StorableClass]
    46   public sealed class SymbolicRegressionProblem : DataAnalysisProblem, ISingleObjectiveProblem {
     46  public class SymbolicRegressionProblem : DataAnalysisProblem, ISingleObjectiveProblem {
    4747
    4848    #region Parameter Properties
     
    169169    public DoubleValue PunishmentFactor {
    170170      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); }
    171186    }
    172187    #endregion
     
    180195      var visualizer = new BestValidationSymbolicRegressionSolutionVisualizer();
    181196      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()));
    183198      Parameters.Add(new ValueParameter<SymbolicExpressionTreeCreator>("SolutionCreator", "The operator which should be used to create new symbolic regression solutions.", creator));
    184199      Parameters.Add(new ValueParameter<ISymbolicExpressionTreeInterpreter>("SymbolicExpressionTreeInterpreter", "The interpreter that should be used to evaluate the symbolic expression tree.", interpreter));
     
    190205      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionLength", "Maximal length of the symbolic expression.", new IntValue(100)));
    191206      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()));
    194209      Parameters.Add(new ValueParameter<ISingleObjectiveSolutionsVisualizer>("Visualizer", "The operator which should be used to visualize symbolic regression solutions.", visualizer));
    195210
    196211      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
    205214      ParameterizeSolutionCreator();
    206215      ParameterizeEvaluator();
    207216      ParameterizeVisualizer();
    208217
     218      UpdateGrammar();
     219      UpdateEstimationLimits();
    209220      Initialize();
    210221    }
    211 
    212222
    213223    [StorableConstructor]
    214224    private SymbolicRegressionProblem(bool deserializing) : base() { }
     225
     226    [StorableHook(HookType.AfterDeserialization)]
     227    private void AfterDeserializationHook() {
     228      Initialize();
     229    }
    215230
    216231    public override IDeepCloneable Clone(Cloner cloner) {
     
    220235    }
    221236
    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
    228259      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) {
    233264      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();
    236354    }
    237355
     
    242360    }
    243361
    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);
    259366        var mean = targetValues.Average();
    260367        var range = targetValues.Max() - targetValues.Min();
     
    264371    }
    265372
    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     #endregion
    322 
    323     #region Helpers
    324     [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 
    334373    private void InitializeOperators() {
    335374      operators = new List<ISymbolicExpressionTreeOperator>();
    336375      operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
    337376      ParameterizeOperators();
    338       UpdateGrammar();
    339       UpdatePartitioningParameters();
    340377    }
    341378
     
    344381      SolutionCreator.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
    345382      SolutionCreator.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
    346     }
     383      SolutionCreator.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
     384      SolutionCreator.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionDefiningBranchesParameter.Name;
     385    }
     386
    347387    private void ParameterizeEvaluator() {
    348388      Evaluator.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
    349389      Evaluator.RegressionProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
    350       Evaluator.QualityParameter.ActualName = "TrainingMeanSquaredError";
    351       Evaluator.SamplesStartParameter.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
    354394    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;
    363405      }
    364406    }
     
    370412        op.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
    371413      }
    372       foreach (ISymbolicRegressionEvaluator op in Operators.OfType<ISymbolicRegressionEvaluator>()) {
    373         op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
    374         op.RegressionProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
    375       }
    376414      foreach (ISymbolicExpressionTreeCrossover op in Operators.OfType<ISymbolicExpressionTreeCrossover>()) {
    377415        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
Note: See TracChangeset for help on using the changeset viewer.