Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/Evaluators/BioBoostEvaluator.cs @ 13071

Last change on this file since 13071 was 13071, checked in by gkronber, 8 years ago

#2499: added license headers and removed unused usings

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;
30
31namespace HeuristicLab.BioBoost.Evaluators {
32
33  [StorableClass]
34  public abstract class BioBoostEvaluator : BioBoostOperator  {
35
36    #region Parameter
37    public ILookupParameter<ResultCollection> CostsParameter {
38      get { return (ILookupParameter<ResultCollection>) Parameters["Costs"]; }
39    }
40    public ILookupParameter<ResultCollection> IntermediateResultsParameter {
41      get { return (ILookupParameter<ResultCollection>) Parameters["IntermediateResults"]; }
42    }
43    #endregion
44
45    #region Parameter Values
46    public ResultCollection Costs {
47      get { return CostsParameter.ActualValue; }
48    }
49    public ResultCollection IntermediateResults {
50      get { return IntermediateResultsParameter.ActualValue; }
51    }
52    #endregion
53
54    #region Construction & Cloning
55    [StorableConstructor]
56    protected BioBoostEvaluator(bool isDeserializing) : base(isDeserializing) {}
57    protected BioBoostEvaluator(BioBoostEvaluator original, Cloner cloner) : base(original, cloner) {}
58    protected BioBoostEvaluator() {
59      Parameters.Add(new LookupParameter<ResultCollection>("Costs", "Collection of all costs."));
60      Parameters.Add(new LookupParameter<ResultCollection>("Emissions", "Collection of all emissions."));
61      Parameters.Add(new LookupParameter<ResultCollection>("IntermediateResults", "Collection of all intermeditate results."));
62    }
63    #endregion
64
65    protected void PutInScope(string label, IItem value, bool isIntermediateVariable) {
66      var vars = ExecutionContext.Scope.Variables;
67      var var = new Variable(label, value);
68      if (vars.ContainsKey(label))
69        vars.Remove(label);
70      vars.Add(var);
71      if (isIntermediateVariable)
72        IntermediateResults.Add(new Result(var.Name, value.GetType()));
73    }
74
75    protected void AddInScope(string label, DoubleArray values, bool isIntermediateValue) {
76      var oldValues = GetFromScope<DoubleArray>(label);
77      if (oldValues != null) {
78        if (oldValues.Length != values.Length)
79          throw new ArgumentException("DoubleArray lentgh differs from new length, cannot add " + label);
80        for (int i = 0; i<oldValues.Length; i++)
81          oldValues[i] += values[i];
82      } else {
83        PutInScope(label, values, isIntermediateValue);
84      }
85    }
86
87    protected void AddCost(string label, double value) {
88      if (Equals(value, 0d)) return;
89      if (Costs.ContainsKey(label)) {
90        ((DoubleValue)Costs[label].Value).Value += value;
91      } else {
92        Costs.Add(new Result(label, new DoubleValue(value)));
93      }
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.