[8924] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 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
|
---|
| 70 | mathematical functions:
|
---|
| 71 | +, -, /, ^ (power), log
|
---|
| 72 | predicates:
|
---|
| 73 | ==, <, >, isnull, not
|
---|
| 74 | stack manipulation:
|
---|
| 75 | drop swap dup
|
---|
| 76 | string matching:
|
---|
| 77 | <string> <pattern> ismatch
|
---|
| 78 | string replacing:
|
---|
| 79 | <string> <pattern> <replacement> rename
|
---|
| 80 | conditionals:
|
---|
| 81 | <then> <else> <condition> if
|
---|
| 82 |
|
---|
| 83 | If the final value is null, the result variable is removed if it exists.",
|
---|
| 84 | new StringValue("1 1 +")));
|
---|
| 85 | UpdateName();
|
---|
| 86 | RegisterEvents();
|
---|
| 87 | }
|
---|
| 88 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 89 | return new RunCollectionFormulaModifer(this, cloner);
|
---|
| 90 | }
|
---|
| 91 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 92 | private void AfterDeserialization() {
|
---|
| 93 | RegisterEvents();
|
---|
| 94 | }
|
---|
| 95 | #endregion
|
---|
| 96 |
|
---|
| 97 | private void RegisterEvents() {
|
---|
| 98 | ResultNameParameter.ToStringChanged += Parameter_NameChanged;
|
---|
| 99 | FormulaParameter.ToStringChanged += Parameter_NameChanged;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | void Parameter_NameChanged(object sender, EventArgs e) {
|
---|
| 103 | UpdateName();
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | private void UpdateName() {
|
---|
| 107 | name = string.Format("{0} := {1}", ResultName, Formula);
|
---|
| 108 | OnNameChanged();
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | public void Modify(List<IRun> runs) {
|
---|
| 112 | var calc = new Calculator { Formula = Formula };
|
---|
| 113 | foreach (var run in runs) {
|
---|
| 114 | var variables = new Dictionary<string, IItem>();
|
---|
| 115 | foreach (var param in run.Parameters)
|
---|
| 116 | variables[param.Key] = param.Value;
|
---|
| 117 | foreach (var result in run.Results)
|
---|
| 118 | variables[result.Key] = result.Value;
|
---|
| 119 | try {
|
---|
| 120 | var value = calc.GetValue(variables);
|
---|
| 121 | if (value != null)
|
---|
| 122 | run.Results[ResultName] = value;
|
---|
| 123 | else
|
---|
| 124 | run.Results.Remove(ResultName);
|
---|
| 125 | } catch (Exception x) {
|
---|
| 126 | throw new Exception(string.Format("Calculation failed at Run {0}", run.Name), x);
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | }
|
---|
| 132 | }
|
---|