source: branches/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/Operators/Crossover/CompoundCrossover.cs @ 13069

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

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

File size: 6.0 KB
Line 
1using HeuristicLab.BioBoost.ProblemDescription;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Data;
5using HeuristicLab.Encodings.IntegerVectorEncoding;
6using HeuristicLab.Encodings.RealVectorEncoding;
7using HeuristicLab.Operators;
8using HeuristicLab.Optimization;
9using HeuristicLab.Parameters;
10using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
11using HeuristicLab.PluginInfrastructure;
12using System.Linq;
13using HeuristicLab.BioBoost.Representation;
14using DiscreteIntegerVectorCrossover = HeuristicLab.Encodings.IntegerVectorEncoding.DiscreteCrossover;
15using DiscreteRealVectorCrossover = HeuristicLab.Encodings.RealVectorEncoding.DiscreteCrossover;
16using SinglePointRealVectorCrossover = HeuristicLab.Encodings.RealVectorEncoding.SinglePointCrossover;
17using SinglePointInteverVectorCrossover = HeuristicLab.Encodings.IntegerVectorEncoding.SinglePointCrossover;
18
19namespace HeuristicLab.BioBoost.Operators.Crossover {
20  [StorableClass]
21  public class CompoundCrossover : SingleSuccessorOperator, ICrossover {
22
23    #region Parameters
24    public ValueLookupParameter<IntMatrix> RegionBoundsParameter {
25      get { return (ValueLookupParameter<IntMatrix>) Parameters["RegionBounds"]; }
26    }
27    public LookupParameter<BioBoostProblemData> ProblemDataParameter {
28      get { return (LookupParameter<BioBoostProblemData>) Parameters["ProblemData"]; }
29    }
30    public ConstrainedValueParameter<IIntegerVectorCrossover> TransportTargetCrossoverParameter {
31      get { return (ConstrainedValueParameter<IIntegerVectorCrossover>) Parameters["TransportTargetCrossover"]; }
32    }
33    public ConstrainedValueParameter<IRealVectorCrossover> FeedstockUtilizationCrossoverParameter {
34      get { return (ConstrainedValueParameter<IRealVectorCrossover>) Parameters["FeedstockUtilizationCrossover"]; }
35    }
36    #endregion
37
38    #region Parameter Values
39    public BioBoostProblemData ProblemData {
40      get { return ProblemDataParameter.ActualValue; }
41    }
42    #endregion
43
44    #region Construction & Cloning
45    [StorableConstructor]
46    protected CompoundCrossover(bool isDeserializing) : base(isDeserializing) {}
47    protected CompoundCrossover(CompoundCrossover orig, Cloner cloner) : base(orig, cloner) { }
48    public CompoundCrossover() {
49      Parameters.Add(new LookupParameter<BioBoostProblemData>("ProblemData", "The data store of the detailed problem description."));
50      Parameters.Add(new ValueLookupParameter<IntMatrix>("RegionBounds", "The limits of valid region ids."));
51      Parameters.Add(new ConstrainedValueParameter<IIntegerVectorCrossover>("TransportTargetCrossover", "The manipulator to apply to the transport target vectors."));
52      Parameters.Add(new ConstrainedValueParameter<IRealVectorCrossover>("FeedstockUtilizationCrossover", "The manipulator for the feedstock utilization vectors."));
53      foreach (var op in ApplicationManager.Manager.GetInstances<IIntegerVectorCrossover>()) {
54        var bop = op as IBoundedIntegerVectorOperator;
55        if (bop != null)
56          bop.BoundsParameter.ActualName = RegionBoundsParameter.ActualName;
57        TransportTargetCrossoverParameter.ValidValues.Add(op);
58      }
59      foreach (var op in ApplicationManager.Manager.GetInstances<IRealVectorCrossover>()) {
60        op.BoundsParameter.Value = new DoubleMatrix(new double[,]{{0, 1}});
61        FeedstockUtilizationCrossoverParameter.ValidValues.Add(op);
62      }
63      TransportTargetCrossoverParameter.Value =
64        TransportTargetCrossoverParameter.ValidValues.FirstOrDefault(c => c.GetType() == typeof (MultiIntegerVectorCrossover));
65      FeedstockUtilizationCrossoverParameter.Value =
66        FeedstockUtilizationCrossoverParameter.ValidValues.FirstOrDefault(
67          c => c.GetType() == typeof (MultiRealVectorCrossover));
68      var tc = (MultiIntegerVectorCrossover) TransportTargetCrossoverParameter.Value;
69      foreach (var op in tc.Operators) {
70        if (op is DiscreteIntegerVectorCrossover ||
71            op is SinglePointInteverVectorCrossover) {
72          // accept
73        } else {
74          tc.Operators.SetItemCheckedState(op, false);
75        }
76      }
77      var uc = (MultiRealVectorCrossover) FeedstockUtilizationCrossoverParameter.Value;
78      foreach (var op in uc.Operators) {
79        if (op is BlendAlphaCrossover ||
80            op is DiscreteRealVectorCrossover ||
81            op is HeuristicCrossover ||
82            op is LocalCrossover ||
83            op is RandomConvexCrossover ||
84            op is SinglePointRealVectorCrossover ||
85            op is UniformAllPositionsArithmeticCrossover ||
86            op is UniformSomePositionsArithmeticCrossover) {
87          // accept
88        } else {
89          uc.Operators.SetItemCheckedState(op, false);
90        }
91      }
92    }
93    public override IDeepCloneable Clone(Cloner cloner) {
94      return new CompoundCrossover(this, cloner);
95    }
96    #endregion
97
98    public override IOperation Apply() {
99      var ops = new OperationCollection(base.Apply());
100      foreach (var n in ProblemData.TransportTargets.CheckedItems) {
101        if (TransportTargetCrossoverParameter.ActualValue != null) {
102          var t = (IIntegerVectorCrossover) TransportTargetCrossoverParameter.ActualValue.Clone();
103          t.ParentsParameter.ActualName = LayerDescriptor.TransportTargets.NameWithPrefix(n.Value.Value);
104          t.ChildParameter.ActualName = LayerDescriptor.TransportTargets.NameWithPrefix(n.Value.Value);
105          ops.Add(ExecutionContext.CreateChildOperation(t));
106        }
107      }
108      foreach (var n in ProblemData.Utilizations.CheckedItems) {
109        if (FeedstockUtilizationCrossoverParameter.ActualValue != null) {
110          var u = (IRealVectorCrossover) FeedstockUtilizationCrossoverParameter.ActualValue.Clone();
111          u.ParentsParameter.ActualName = LayerDescriptor.Utilizations.NameWithPrefix(n.Value.Value);
112          u.ChildParameter.ActualName = LayerDescriptor.Utilizations.NameWithPrefix(n.Value.Value);
113          ops.Add(ExecutionContext.CreateChildOperation(u));
114        }
115      }
116      return ops;
117    }
118
119  }
120}
Note: See TracBrowser for help on using the repository browser.