Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2870_AutoDiff-nuget/HeuristicLab.Optimization/3.3/RunCollectionModification/RunCollectionFormulaModifer.cs

Last change on this file was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Optimization {
33
34  [Item("RunCollection Formula Modifier", "Modifies a RunCollection by adding results using the given formula.")]
35  [StorableClass]
36  public class RunCollectionFormulaModifer : ParameterizedNamedItem, IRunCollectionModifier {
37
38    public override bool CanChangeName { get { return false; } }
39    public override bool CanChangeDescription { get { return false; } }
40
41    public ValueParameter<StringValue> ResultNameParameter {
42      get { return (ValueParameter<StringValue>)Parameters["ResultName"]; }
43    }
44
45    public ValueParameter<StringValue> FormulaParameter {
46      get { return (ValueParameter<StringValue>)Parameters["Formula"]; }
47    }
48
49    private string ResultName { get { return ResultNameParameter.Value.Value; } }
50    private string Formula { get { return FormulaParameter.Value.Value; } }
51
52    #region Construction & Cloning
53    [StorableConstructor]
54    protected RunCollectionFormulaModifer(bool deserializing) : base(deserializing) { }
55    protected RunCollectionFormulaModifer(RunCollectionFormulaModifer original, Cloner cloner)
56      : base(original, cloner) {
57      RegisterEvents();
58    }
59    public RunCollectionFormulaModifer() {
60      Parameters.Add(new ValueParameter<StringValue>("ResultName", "The name of the result to be generated by this formula.", new StringValue("Calc.Value")));
61      Parameters.Add(new ValueParameter<StringValue>("Formula",
62@"RPN formula for new value in postfix notation.
63
64This can contain the following elements:
65
66literals:
67  numbers, true, false, null and strings in single quotes
68variables (run parameters or results):
69  unquoted or in double quotes if they contain special characters or whitespace
70mathematical functions (resulting in double values):
71  +, -, *, /, ^ (power), log
72predicates:
73  ==, <, >, isnull, not
74conversions:
75  toint, todouble
76array indexing:
77  []
78stack manipulation:
79  drop swap dup
80string matching:
81  <string> <pattern> ismatch
82string replacing:
83  <string> <pattern> <replacement> rename
84conditionals:
85  <then> <else> <condition> if
86
87If the final value is null, the result variable is removed if it exists.",
88        new StringValue("1 1 +")));
89      UpdateName();
90      RegisterEvents();
91    }
92    public override IDeepCloneable Clone(Cloner cloner) {
93      return new RunCollectionFormulaModifer(this, cloner);
94    }
95    [StorableHook(HookType.AfterDeserialization)]
96    private void AfterDeserialization() {
97      RegisterEvents();
98    }
99    #endregion
100
101    private void RegisterEvents() {
102      ResultNameParameter.ToStringChanged += Parameter_NameChanged;
103      FormulaParameter.ToStringChanged += Parameter_NameChanged;
104    }
105
106    void Parameter_NameChanged(object sender, EventArgs e) {
107      UpdateName();
108    }
109
110    private void UpdateName() {
111      name = string.Format("{0} := {1}", ResultName, Formula);
112      OnNameChanged();
113    }
114
115    public void Modify(List<IRun> runs) {
116      var calc = new Calculator { Formula = Formula };
117      foreach (var run in runs) {
118        var variables = new Dictionary<string, IItem>();
119        foreach (var param in run.Parameters)
120          variables[param.Key] = param.Value;
121        foreach (var result in run.Results)
122          variables[result.Key] = result.Value;
123        try {
124          var value = calc.GetValue(variables);
125          if (value != null)
126            run.Results[ResultName] = value;
127          else
128            run.Results.Remove(ResultName);
129        } catch (Exception x) {
130          throw new Exception(string.Format("Calculation failed at Run {0}", run.Name), x);
131        }
132      }
133    }
134
135  }
136}
Note: See TracBrowser for help on using the repository browser.