Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Optimization/3.3/RunCollectionModification/RunCollectionFormulaModifer.cs @ 16713

Last change on this file since 16713 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 4.7 KB
RevLine 
[8924]1#region License Information
2/* HeuristicLab
[16565]3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[8924]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;
[7228]23using System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
[16565]28using HEAL.Attic;
[7228]29
30namespace HeuristicLab.Optimization {
31
32  [Item("RunCollection Formula Modifier", "Modifies a RunCollection by adding results using the given formula.")]
[16565]33  [StorableType("EEE365F2-8BEB-41C6-9DE6-94CA8FAC5108")]
[7228]34  public class RunCollectionFormulaModifer : ParameterizedNamedItem, IRunCollectionModifier {
35
36    public override bool CanChangeName { get { return false; } }
37    public override bool CanChangeDescription { get { return false; } }
38
39    public ValueParameter<StringValue> ResultNameParameter {
40      get { return (ValueParameter<StringValue>)Parameters["ResultName"]; }
41    }
42
43    public ValueParameter<StringValue> FormulaParameter {
44      get { return (ValueParameter<StringValue>)Parameters["Formula"]; }
45    }
46
47    private string ResultName { get { return ResultNameParameter.Value.Value; } }
48    private string Formula { get { return FormulaParameter.Value.Value; } }
49
50    #region Construction & Cloning
51    [StorableConstructor]
[16565]52    protected RunCollectionFormulaModifer(StorableConstructorFlag _) : base(_) { }
[7228]53    protected RunCollectionFormulaModifer(RunCollectionFormulaModifer original, Cloner cloner)
54      : base(original, cloner) {
55      RegisterEvents();
56    }
57    public RunCollectionFormulaModifer() {
58      Parameters.Add(new ValueParameter<StringValue>("ResultName", "The name of the result to be generated by this formula.", new StringValue("Calc.Value")));
59      Parameters.Add(new ValueParameter<StringValue>("Formula",
60@"RPN formula for new value in postfix notation.
61
62This can contain the following elements:
63
64literals:
65  numbers, true, false, null and strings in single quotes
66variables (run parameters or results):
67  unquoted or in double quotes if they contain special characters or whitespace
[12114]68mathematical functions (resulting in double values):
69  +, -, *, /, ^ (power), log
[7228]70predicates:
71  ==, <, >, isnull, not
[12114]72conversions:
73  toint, todouble
74array indexing:
75  []
[7228]76stack manipulation:
77  drop swap dup
78string matching:
79  <string> <pattern> ismatch
80string replacing:
81  <string> <pattern> <replacement> rename
82conditionals:
83  <then> <else> <condition> if
84
85If the final value is null, the result variable is removed if it exists.",
86        new StringValue("1 1 +")));
87      UpdateName();
88      RegisterEvents();
89    }
90    public override IDeepCloneable Clone(Cloner cloner) {
91      return new RunCollectionFormulaModifer(this, cloner);
92    }
93    [StorableHook(HookType.AfterDeserialization)]
94    private void AfterDeserialization() {
95      RegisterEvents();
96    }
97    #endregion
98
99    private void RegisterEvents() {
100      ResultNameParameter.ToStringChanged += Parameter_NameChanged;
101      FormulaParameter.ToStringChanged += Parameter_NameChanged;
102    }
103
104    void Parameter_NameChanged(object sender, EventArgs e) {
105      UpdateName();
106    }
107
108    private void UpdateName() {
109      name = string.Format("{0} := {1}", ResultName, Formula);
110      OnNameChanged();
111    }
112
113    public void Modify(List<IRun> runs) {
114      var calc = new Calculator { Formula = Formula };
115      foreach (var run in runs) {
116        var variables = new Dictionary<string, IItem>();
117        foreach (var param in run.Parameters)
118          variables[param.Key] = param.Value;
119        foreach (var result in run.Results)
120          variables[result.Key] = result.Value;
121        try {
122          var value = calc.GetValue(variables);
123          if (value != null)
124            run.Results[ResultName] = value;
125          else
126            run.Results.Remove(ResultName);
127        } catch (Exception x) {
128          throw new Exception(string.Format("Calculation failed at Run {0}", run.Name), x);
129        }
130      }
131    }
132
133  }
134}
Note: See TracBrowser for help on using the repository browser.