Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/Evaluators/BioBoostEvaluator.cs @ 17777

Last change on this file since 17777 was 16575, checked in by gkronber, 5 years ago

#2520: changed HeuristicLab.BioBoost addon to compile with new HL.Persistence

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 HeuristicLab.BioBoost.Operators;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using System;
30using HEAL.Attic;
31
32namespace HeuristicLab.BioBoost.Evaluators {
33
34  [StorableType("836CF83B-0123-428F-B953-5B9411D6BB97")]
35  public abstract class BioBoostEvaluator : BioBoostOperator  {
36
37    #region Parameter
38    public ILookupParameter<ResultCollection> CostsParameter {
39      get { return (ILookupParameter<ResultCollection>) Parameters["Costs"]; }
40    }
41    public ILookupParameter<ResultCollection> IntermediateResultsParameter {
42      get { return (ILookupParameter<ResultCollection>) Parameters["IntermediateResults"]; }
43    }
44    #endregion
45
46    #region Parameter Values
47    public ResultCollection Costs {
48      get { return CostsParameter.ActualValue; }
49    }
50    public ResultCollection IntermediateResults {
51      get { return IntermediateResultsParameter.ActualValue; }
52    }
53    #endregion
54
55    #region Construction & Cloning
56    [StorableConstructor]
57    protected BioBoostEvaluator(StorableConstructorFlag _) : base(_) { }
58    protected BioBoostEvaluator(BioBoostEvaluator original, Cloner cloner) : base(original, cloner) {}
59    protected BioBoostEvaluator() {
60      Parameters.Add(new LookupParameter<ResultCollection>("Costs", "Collection of all costs."));
61      Parameters.Add(new LookupParameter<ResultCollection>("Emissions", "Collection of all emissions."));
62      Parameters.Add(new LookupParameter<ResultCollection>("IntermediateResults", "Collection of all intermeditate results."));
63    }
64    #endregion
65
66    protected void PutInScope(string label, IItem value, bool isIntermediateVariable) {
67      var vars = ExecutionContext.Scope.Variables;
68      var var = new Variable(label, value);
69      if (vars.ContainsKey(label))
70        vars.Remove(label);
71      vars.Add(var);
72      if (isIntermediateVariable)
73        IntermediateResults.Add(new Result(var.Name, value.GetType()));
74    }
75
76    protected void AddInScope(string label, DoubleArray values, bool isIntermediateValue) {
77      var oldValues = GetFromScope<DoubleArray>(label);
78      if (oldValues != null) {
79        if (oldValues.Length != values.Length)
80          throw new ArgumentException("DoubleArray lentgh differs from new length, cannot add " + label);
81        for (int i = 0; i<oldValues.Length; i++)
82          oldValues[i] += values[i];
83      } else {
84        PutInScope(label, values, isIntermediateValue);
85      }
86    }
87
88    protected void AddCost(string label, double value) {
89      if (Equals(value, 0d)) return;
90      if (Costs.ContainsKey(label)) {
91        ((DoubleValue)Costs[label].Value).Value += value;
92      } else {
93        Costs.Add(new Result(label, new DoubleValue(value)));
94      }
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.