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

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

#2499: imported source code for HeuristicLab.BioBoost from private repository with some changes

File size: 2.8 KB
Line 
1using HeuristicLab.BioBoost.Operators;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Data;
5using HeuristicLab.Optimization;
6using HeuristicLab.Parameters;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8using System;
9
10namespace HeuristicLab.BioBoost.Evaluators {
11
12  [StorableClass]
13  public abstract class BioBoostEvaluator : BioBoostOperator  {
14
15    #region Parameter
16    public ILookupParameter<ResultCollection> CostsParameter {
17      get { return (ILookupParameter<ResultCollection>) Parameters["Costs"]; }
18    }
19    public ILookupParameter<ResultCollection> IntermediateResultsParameter {
20      get { return (ILookupParameter<ResultCollection>) Parameters["IntermediateResults"]; }
21    }
22    #endregion
23
24    #region Parameter Values
25    public ResultCollection Costs {
26      get { return CostsParameter.ActualValue; }
27    }
28    public ResultCollection IntermediateResults {
29      get { return IntermediateResultsParameter.ActualValue; }
30    }
31    #endregion
32
33    #region Construction & Cloning
34    [StorableConstructor]
35    protected BioBoostEvaluator(bool isDeserializing) : base(isDeserializing) {}
36    protected BioBoostEvaluator(BioBoostEvaluator original, Cloner cloner) : base(original, cloner) {}
37    protected BioBoostEvaluator() {
38      Parameters.Add(new LookupParameter<ResultCollection>("Costs", "Collection of all costs."));
39      Parameters.Add(new LookupParameter<ResultCollection>("Emissions", "Collection of all emissions."));
40      Parameters.Add(new LookupParameter<ResultCollection>("IntermediateResults", "Collection of all intermeditate results."));
41    }
42    #endregion
43
44    protected void PutInScope(string label, IItem value, bool isIntermediateVariable) {
45      var vars = ExecutionContext.Scope.Variables;
46      var var = new Variable(label, value);
47      if (vars.ContainsKey(label))
48        vars.Remove(label);
49      vars.Add(var);
50      if (isIntermediateVariable)
51        IntermediateResults.Add(new Result(var.Name, value.GetType()));
52    }
53
54    protected void AddInScope(string label, DoubleArray values, bool isIntermediateValue) {
55      var oldValues = GetFromScope<DoubleArray>(label);
56      if (oldValues != null) {
57        if (oldValues.Length != values.Length)
58          throw new ArgumentException("DoubleArray lentgh differs from new length, cannot add " + label);
59        for (int i = 0; i<oldValues.Length; i++)
60          oldValues[i] += values[i];
61      } else {
62        PutInScope(label, values, isIntermediateValue);
63      }
64    }
65
66    protected void AddCost(string label, double value) {
67      if (Equals(value, 0d)) return;
68      if (Costs.ContainsKey(label)) {
69        ((DoubleValue)Costs[label].Value).Value += value;
70      } else {
71        Costs.Add(new Result(label, new DoubleValue(value)));
72      }
73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.