Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpOrchestratorNode3.cs @ 14628

Last change on this file since 14628 was 14628, checked in by jkarder, 7 years ago

#2205: worked on optimization networks

  • added variegation problem for minimization and maximization
  • refactored some classes
File size: 7.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 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 System;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Core.Networks;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.BinaryVectorEncoding;
29using HeuristicLab.Encodings.PermutationEncoding;
30using HeuristicLab.Encodings.RealVectorEncoding;
31using HeuristicLab.Optimization;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.Problems.Knapsack;
34using HeuristicLab.Problems.TravelingSalesman;
35
36namespace HeuristicLab.Networks.IntegratedOptimization.TravelingThief {
37  [Item("TtpOrchestratorNode3", "Orchestrator for TTP optimization network version 3.")]
38  [StorableClass]
39  public sealed class TtpOrchestratorNode3 : TtpOrchestratorNode {
40    [StorableConstructor]
41    private TtpOrchestratorNode3(bool deserializing) : base(deserializing) { }
42    private TtpOrchestratorNode3(TtpOrchestratorNode3 original, Cloner cloner) : base(original, cloner) { }
43    public TtpOrchestratorNode3() : this("TtpOrchestratorNode3") { }
44    public TtpOrchestratorNode3(string name) : base(name) {
45      MetaSolverOrchestrationPort = CreateOrchestrationPort<MaximizationVariegationProblem<RealVectorEncoding>>(MetaSolverName + OrchestrationPortNameSuffix);
46      MetaSolverEvaluationPort = CreateEvaluationPort<RealVector>(MetaSolverName + EvaluationPortNameSuffix, "RealVector", "Quality");
47      TspSolverOrchestrationPort = CreateOrchestrationPort<TravelingSalesmanProblem>(TspSolverName + OrchestrationPortNameSuffix);
48      KspSolverOrchestrationPort = CreateOrchestrationPort<BinaryKnapsackProblem>(KspSolverName + OrchestrationPortNameSuffix);
49    }
50
51    public override IDeepCloneable Clone(Cloner cloner) {
52      return new TtpOrchestratorNode3(this, cloner);
53    }
54
55    public override void Prepare(bool clearRuns = false) {
56      base.Prepare(clearRuns);
57
58      var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
59      var msgFlags = OrchestrationMessage.Prepare | OrchestrationMessage.SetEvalHook;
60      if (clearRuns) msgFlags |= OrchestrationMessage.ClearRuns;
61      metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(msgFlags);
62      var problem = new MaximizationVariegationProblem<RealVectorEncoding>();
63      problem.Encoding.Length = KspParameter.Value.Length + TspParameter.Value.Coordinates.Rows * 2;
64      problem.Encoding.Bounds = new DoubleMatrix(new[,] { { -1.0, 1.0 } });
65      metaMsg["Problem"] = problem;
66      MetaSolverOrchestrationPort.SendMessage(metaMsg);
67    }
68
69    #region MetaSolver Message Handling
70    protected override void MetaSolverEvaluationPortMessage(IMessage message) {
71      var factors = (RealVector)message["RealVector"];
72      int fi = 0;
73
74      var ksp = (BinaryKnapsackProblem)KspParameter.Value.Clone();
75      while (fi < ksp.Values.Length) {
76        ksp.Values[fi] = (int)Math.Ceiling(ksp.Values[fi] * factors[fi]);
77        ++fi;
78      }
79
80      var kspMsg = KspSolverOrchestrationPort.PrepareMessage();
81      kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
82      kspMsg["Problem"] = ksp;
83      KspSolverOrchestrationPort.SendMessage(kspMsg);
84      cts.Token.ThrowIfCancellationRequested();
85
86      var kspResults = (ResultCollection)kspMsg["Results"];
87      var bestKspSolution = (BinaryVector)kspResults["Best Solution"].Value.Clone();
88      var kspCapacity = (IntValue)KspParameter.Value.KnapsackCapacity.Clone();
89      var kspPenalty = new DoubleValue(0.0);
90      var kspWeights = (IntArray)KspParameter.Value.Weights.Clone();
91      var kspValues = (IntArray)KspParameter.Value.Values.Clone();
92      var bestKspQuality = KnapsackEvaluator.Apply(bestKspSolution, kspCapacity, kspPenalty, kspWeights, kspValues).Quality;
93      var loot = new KnapsackSolution(bestKspSolution, bestKspQuality, kspCapacity, kspWeights, kspValues);
94
95      var tsp = (TravelingSalesmanProblem)TspParameter.Value.Clone();
96      for (int j = 0; j < tsp.Coordinates.Rows; j++) {
97        tsp.Coordinates[j, 0] = (int)Math.Ceiling(tsp.Coordinates[j, 0] * factors[fi + j * 2]);
98        tsp.Coordinates[j, 1] = (int)Math.Ceiling(tsp.Coordinates[j, 1] * factors[fi + j * 2 + 1]);
99      }
100
101      var tspMsg = TspSolverOrchestrationPort.PrepareMessage();
102      tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
103      var tpp = new TourProfitProblem {
104        Tsp = (TravelingSalesmanProblem)TspParameter.Value.Clone(),
105        Ksp = (BinaryKnapsackProblem)KspParameter.Value.Clone(),
106        FixedKspSolution = bestKspSolution,
107        Availability = AvailabilityParameter.Value.ToArray(),
108        RentingRatio = RentingRatioParameter.Value.Value,
109        MinSpeed = MinSpeedParameter.Value.Value,
110        MaxSpeed = MaxSpeedParameter.Value.Value,
111        DistanceType = distanceType
112      };
113      tpp.Encoding.Length = TspParameter.Value.Coordinates.Rows;
114      tspMsg["Problem"] = tpp;
115      TspSolverOrchestrationPort.SendMessage(tspMsg);
116      cts.Token.ThrowIfCancellationRequested();
117
118      var tspResults = (ResultCollection)tspMsg["Results"];
119      var bestTspSolution = (Permutation)tspResults["Best TSP Solution"].Value.Clone();
120      var coordinates = (DoubleMatrix)TspParameter.Value.Coordinates.Clone();
121      var tour = new PathTSPTour(coordinates, bestTspSolution, new DoubleValue(TSPCoordinatesPathEvaluator.Apply(new TSPEuclideanPathEvaluator(), coordinates, bestTspSolution)));
122
123      #region Analyze
124      double objectiveValue = TtpUtils.Evaluate(TspParameter.Value, tour.Permutation.ToArray(), KspParameter.Value, loot.BinaryVector.ToArray(),
125        AvailabilityParameter.Value.ToArray(), RentingRatioParameter.Value.Value, MinSpeedParameter.Value.Value, MaxSpeedParameter.Value.Value, distanceType);
126      ((DoubleValue)message["Quality"]).Value = objectiveValue;
127
128      IResult bestQuality;
129      if (!Results.TryGetValue("Best TTP Quality", out bestQuality)) {
130        Results.Add(new Result("Best TTP Quality", new DoubleValue(objectiveValue)));
131        Results.Add(new Result("Best Tour", tour));
132        Results.Add(new Result("Best Loot", loot));
133      } else if (objectiveValue > ((DoubleValue)bestQuality.Value).Value) {
134        ((DoubleValue)bestQuality.Value).Value = objectiveValue;
135        Results["Best Tour"].Value = tour;
136        Results["Best Loot"].Value = loot;
137      }
138      #endregion
139    }
140    #endregion
141  }
142}
Note: See TracBrowser for help on using the repository browser.