[8924] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15583] | 3 | * Copyright (C) 2002-2018 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 |
|
---|
| 22 | using System;
|
---|
[7228] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace 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 |
|
---|
| 64 | This can contain the following elements:
|
---|
| 65 |
|
---|
| 66 | literals:
|
---|
| 67 | numbers, true, false, null and strings in single quotes
|
---|
| 68 | variables (run parameters or results):
|
---|
| 69 | unquoted or in double quotes if they contain special characters or whitespace
|
---|
[12114] | 70 | mathematical functions (resulting in double values):
|
---|
| 71 | +, -, *, /, ^ (power), log
|
---|
[7228] | 72 | predicates:
|
---|
| 73 | ==, <, >, isnull, not
|
---|
[12114] | 74 | conversions:
|
---|
| 75 | toint, todouble
|
---|
| 76 | array indexing:
|
---|
| 77 | []
|
---|
[7228] | 78 | stack manipulation:
|
---|
| 79 | drop swap dup
|
---|
| 80 | string matching:
|
---|
| 81 | <string> <pattern> ismatch
|
---|
| 82 | string replacing:
|
---|
| 83 | <string> <pattern> <replacement> rename
|
---|
| 84 | conditionals:
|
---|
| 85 | <then> <else> <condition> if
|
---|
| 86 |
|
---|
| 87 | If 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 | }
|
---|