Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpOrchestratorNode1.cs @ 14613

Last change on this file since 14613 was 14610, checked in by jkarder, 8 years ago

#2205: worked on optimization networks

  • added abstract base classes for ttp networks/orchestrators
  • removed ttp networks/orchestrators from HeuristicLab.Networks.IntegratedOptimization
  • runs can now be cleared when preparing OrchestratedAlgorithmNodes
File size: 5.5 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("TtpOrchestratorNode1", "Orchestrator for TTP optimization network version 1.")]
38  [StorableClass]
39  public sealed class TtpOrchestratorNode1 : TtpOrchestratorNode {
40    [StorableConstructor]
41    private TtpOrchestratorNode1(bool deserializing) : base(deserializing) { }
42    private TtpOrchestratorNode1(TtpOrchestratorNode1 original, Cloner cloner) : base(original, cloner) { }
43    public TtpOrchestratorNode1() : this("TtpOrchestratorNode1") { }
44    public TtpOrchestratorNode1(string name) : base(name) { }
45
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new TtpOrchestratorNode1(this, cloner);
48    }
49
50    #region MetaSolver Message Handling
51    protected override void MetaSolverEvaluationPortMessage(IMessage message) {
52      var factors = (RealVector)message["RealVector"];
53
54      var ksp = (BinaryKnapsackProblem)KspParameter.Value.Clone();
55      for (int i = 0; i < ksp.Values.Length; i++)
56        ksp.Values[i] = (int)Math.Ceiling(ksp.Values[i] * factors[i]);
57
58      var kspMsg = KspSolverOrchestrationPort.PrepareMessage();
59      kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
60      kspMsg["Problem"] = ksp;
61      KspSolverOrchestrationPort.SendMessage(kspMsg);
62      cts.Token.ThrowIfCancellationRequested();
63
64      var bestKspSolution = (BinaryVector)kspResults["Best Solution"].Value.Clone();
65      var kspCapacity = (IntValue)KspParameter.Value.KnapsackCapacity.Clone();
66      var kspPenalty = new DoubleValue(0.0);
67      var kspWeights = (IntArray)KspParameter.Value.Weights.Clone();
68      var kspValues = (IntArray)KspParameter.Value.Values.Clone();
69      var bestKspQuality = KnapsackEvaluator.Apply(bestKspSolution, kspCapacity, kspPenalty, kspWeights, kspValues).Quality;
70      var loot = new KnapsackSolution(bestKspSolution, bestKspQuality, kspCapacity, kspWeights, kspValues);
71
72      var tspMsg = TspSolverOrchestrationPort.PrepareMessage();
73      tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
74      var tpp = new TourProfitProblem {
75        Tsp = (TravelingSalesmanProblem)TspParameter.Value.Clone(),
76        Ksp = (BinaryKnapsackProblem)KspParameter.Value.Clone(),
77        FixedKspSolution = bestKspSolution,
78        Availability = AvailabilityParameter.Value.ToArray(),
79        RentingRatio = RentingRatioParameter.Value.Value,
80        MinSpeed = MinSpeedParameter.Value.Value,
81        MaxSpeed = MaxSpeedParameter.Value.Value,
82        DistanceType = distanceType
83      };
84      tpp.Encoding.Length = TspParameter.Value.Coordinates.Rows;
85      tspMsg["Problem"] = tpp;
86      TspSolverOrchestrationPort.SendMessage(tspMsg);
87      cts.Token.ThrowIfCancellationRequested();
88
89      var bestTspSolution = (Permutation)tspResults["Best TSP Solution"].Value.Clone();
90      var coordinates = (DoubleMatrix)TspParameter.Value.Coordinates.Clone();
91      var tour = new PathTSPTour(coordinates, bestTspSolution, new DoubleValue(TSPCoordinatesPathEvaluator.Apply(new TSPEuclideanPathEvaluator(), coordinates, bestTspSolution)));
92
93      #region Analyze
94      double objectiveValue = TtpUtils.Evaluate(TspParameter.Value, tour.Permutation.ToArray(), KspParameter.Value, loot.BinaryVector.ToArray(),
95        AvailabilityParameter.Value.ToArray(), RentingRatioParameter.Value.Value, MinSpeedParameter.Value.Value, MaxSpeedParameter.Value.Value, distanceType);
96      ((DoubleValue)message["Quality"]).Value = objectiveValue;
97
98      IResult bestQuality;
99      if (!Results.TryGetValue("Best TTP Quality", out bestQuality)) {
100        Results.Add(new Result("Best TTP Quality", new DoubleValue(objectiveValue)));
101        Results.Add(new Result("Best Tour", tour));
102        Results.Add(new Result("Best Loot", loot));
103      } else if (objectiveValue > ((DoubleValue)bestQuality.Value).Value) {
104        ((DoubleValue)bestQuality.Value).Value = objectiveValue;
105        Results["Best Tour"].Value = tour;
106        Results["Best Loot"].Value = loot;
107      }
108      #endregion
109    }
110    #endregion
111  }
112}
Note: See TracBrowser for help on using the repository browser.