[13069] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using System.Threading.Tasks;
|
---|
| 6 | using HeuristicLab.BioBoost.ProblemDescription;
|
---|
| 7 | using HeuristicLab.Common;
|
---|
| 8 | using HeuristicLab.Core;
|
---|
| 9 | using HeuristicLab.Data;
|
---|
| 10 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
| 11 | using HeuristicLab.Parameters;
|
---|
| 12 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 13 | using HeuristicLab.Random;
|
---|
| 14 |
|
---|
| 15 | namespace HeuristicLab.BioBoost.Operators {
|
---|
| 16 |
|
---|
| 17 | [StorableClass]
|
---|
| 18 | public class LimitedDistanceTransportTargetCreator : BioBoostOperator, IIntegerVectorCreator {
|
---|
| 19 |
|
---|
| 20 | #region IntegerVectorCreator
|
---|
| 21 | public IValueLookupParameter<IRandom> RandomParameter { get { return (IValueLookupParameter<IRandom>) Parameters["Random"]; } }
|
---|
| 22 | public IValueLookupParameter<IntMatrix> BoundsParameter { get { return (IValueLookupParameter<IntMatrix>) Parameters["Bounds"]; } }
|
---|
| 23 | public IValueLookupParameter<IntValue> LengthParameter { get { return (IValueLookupParameter<IntValue>) Parameters["Length"]; } }
|
---|
| 24 | public IValueLookupParameter<IntValue> TournamentSizeParameter { get { return (IValueLookupParameter<IntValue>) Parameters["TournamentSize"]; } }
|
---|
| 25 | public ILookupParameter<IntegerVector> IntegerVectorParameter { get { return (ILookupParameter<IntegerVector>) Parameters["IntegerVector"]; } }
|
---|
| 26 | public LookupParameter<DistanceMatrix> DistanceMatrixParameter { get { return (LookupParameter<DistanceMatrix>) Parameters["DistanceMatrix"]; } }
|
---|
| 27 | public ValueParameter<BoolValue> DistanceMatrixInProblemDataParameter { get { return (ValueParameter<BoolValue>) Parameters["DistanceMatrixInProblemData"]; } }
|
---|
| 28 | #endregion
|
---|
| 29 |
|
---|
| 30 | public bool DistanceMatrixInProblemData { get { return DistanceMatrixInProblemDataParameter.Value.Value; } }
|
---|
| 31 | public DistanceMatrix DistanceMatrix {
|
---|
| 32 | get {
|
---|
| 33 | if (DistanceMatrixInProblemData) {
|
---|
| 34 | return ((IValueParameter<DistanceMatrix>) ProblemData.Parameters[DistanceMatrixParameter.ActualName]).Value;
|
---|
| 35 | } else {
|
---|
| 36 | return DistanceMatrixParameter.ActualValue;
|
---|
| 37 | }
|
---|
| 38 | }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | #region Construction & Cloning
|
---|
| 42 | [StorableConstructor]
|
---|
| 43 | protected LimitedDistanceTransportTargetCreator(bool isDeserializing) : base(isDeserializing) { }
|
---|
| 44 |
|
---|
| 45 | protected LimitedDistanceTransportTargetCreator(LimitedDistanceTransportTargetCreator orig, Cloner cloner) : base(orig, cloner) {}
|
---|
| 46 |
|
---|
| 47 | public LimitedDistanceTransportTargetCreator() {
|
---|
| 48 | Parameters.Add(new ValueLookupParameter<IntMatrix>("Bounds", "The list of limits for each dimension (wrapped around if too short)"));
|
---|
| 49 | Parameters.Add(new ValueLookupParameter<IntValue>("Length", "The length of the integer vector to be created"));
|
---|
| 50 | Parameters.Add(new LookupParameter<IntegerVector>("IntegerVector", "The newly created integer vector"));
|
---|
| 51 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "The random number generator to use."));
|
---|
| 52 | Parameters.Add(new ValueLookupParameter<IntValue>("TournamentSize", "The tournament size as a fraction of the length. (higher values yield closer targets, 0 is random)", new IntValue(5)));
|
---|
| 53 | Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The distance matrix to use", "StreetDistanceMatrix")); // TODO: check this
|
---|
| 54 | Parameters.Add(new ValueParameter<BoolValue>("DistanceMatrixInProblemData", "Whether to look for the distance matrix in problem data or in scope.", new BoolValue(true)));
|
---|
| 55 | DistanceMatrixParameter.Hidden = false;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public override IDeepCloneable Clone(Cloner cloner) { return new LimitedDistanceTransportTargetCreator(this, cloner); }
|
---|
| 59 | #endregion
|
---|
| 60 |
|
---|
| 61 | public override IOperation Apply() {
|
---|
| 62 | var random = RandomParameter.ActualValue;
|
---|
| 63 | var length = LengthParameter.ActualValue.Value;
|
---|
| 64 | var v = new IntegerVector(length);
|
---|
| 65 | var tournamentSize = Math.Min(length, Math.Max(1, TournamentSizeParameter.ActualValue.Value));
|
---|
| 66 | var bounds = BoundsParameter.ActualValue;
|
---|
| 67 | for (int i = 0; i < length; i++) {
|
---|
| 68 | var min = bounds[i%bounds.Rows, 0];
|
---|
| 69 | var max = bounds[i%bounds.Rows, 1];
|
---|
| 70 | var range = max - min;
|
---|
| 71 | var sample = tournamentSize > length/3
|
---|
| 72 | ? Enumerable.Range(min, range).SampleRandom(random, tournamentSize).ToList()
|
---|
| 73 | : Enumerable.Range(0, tournamentSize).Select(j => random.Next(range) + min).ToList();
|
---|
| 74 | v[i] = sample
|
---|
| 75 | .Select(j => new {j, dist = DistanceMatrix[i, j]})
|
---|
| 76 | .OrderBy(x => x.dist)
|
---|
| 77 | .First().j;
|
---|
| 78 | }
|
---|
| 79 | IntegerVectorParameter.ActualValue = v;
|
---|
| 80 | return base.Apply();
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | }
|
---|