#region License Information
/* HeuristicLab
* Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Core.Networks;
using HeuristicLab.Data;
using HeuristicLab.Encodings.BinaryVectorEncoding;
using HeuristicLab.Encodings.RealVectorEncoding;
using HeuristicLab.Optimization;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Problems.Knapsack;
using HeuristicLab.Problems.TravelingSalesman;
namespace HeuristicLab.Networks.IntegratedOptimization.TravelingThief {
[Item("TtpOrchestratorNode2", "Orchestrator for TTP optimization network version 2.")]
[StorableClass]
public sealed class TtpOrchestratorNode2 : TtpOrchestratorNode {
[StorableConstructor]
private TtpOrchestratorNode2(bool deserializing) : base(deserializing) { }
private TtpOrchestratorNode2(TtpOrchestratorNode2 original, Cloner cloner) : base(original, cloner) { }
public TtpOrchestratorNode2() : this("TtpOrchestratorNode2") { }
public TtpOrchestratorNode2(string name) : base(name) {
MetaSolverOrchestrationPort = CreateOrchestrationPort>(MetaSolverName + OrchestrationPortNameSuffix);
MetaSolverEvaluationPort = CreateEvaluationPort(MetaSolverName + EvaluationPortNameSuffix, "RealVector", "Quality");
TspSolverOrchestrationPort = CreateOrchestrationPort(TspSolverName + OrchestrationPortNameSuffix);
KspSolverOrchestrationPort = CreateOrchestrationPort(KspSolverName + OrchestrationPortNameSuffix);
}
public override IDeepCloneable Clone(Cloner cloner) {
return new TtpOrchestratorNode2(this, cloner);
}
public override void Prepare(bool clearRuns = false) {
base.Prepare(clearRuns);
var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
var msgFlags = OrchestrationMessage.Prepare | OrchestrationMessage.SetEvalHook;
if (clearRuns) msgFlags |= OrchestrationMessage.ClearRuns;
metaMsg["OrchestrationMessage"] = new EnumValue(msgFlags);
var problem = new MaximizationVariegationProblem();
problem.Encoding.Length = TspParameter.Value.Coordinates.Rows * 2;
problem.Encoding.Bounds = new DoubleMatrix(new[,] { { -1.0, 1.0 } });
metaMsg["Problem"] = problem;
MetaSolverOrchestrationPort.SendMessage(metaMsg);
}
#region MetaSolver Message Handling
protected override void MetaSolverEvaluationPortMessage(IMessage message) {
var factors = (RealVector)message["RealVector"];
var tsp = (TravelingSalesmanProblem)TspParameter.Value.Clone();
for (int i = 0; i < tsp.Coordinates.Rows; i++) {
tsp.Coordinates[i, 0] = (int)Math.Ceiling(tsp.Coordinates[i, 0] * factors[i * 2]);
tsp.Coordinates[i, 1] = (int)Math.Ceiling(tsp.Coordinates[i, 1] * factors[i * 2 + 1]);
}
tsp.DistanceMatrix = new DistanceMatrix(TtpUtils.GetDistances(tsp.Coordinates, distanceType));
var tspMsg = TspSolverOrchestrationPort.PrepareMessage();
tspMsg["OrchestrationMessage"] = new EnumValue(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
tspMsg["Problem"] = tsp;
TspSolverOrchestrationPort.SendMessage(tspMsg);
cts.Token.ThrowIfCancellationRequested();
var tspResults = (ResultCollection)tspMsg["Results"];
var bestTspSolution = (PathTSPTour)tspResults["Best TSP Solution"].Value.Clone();
var coordinates = (DoubleMatrix)TspParameter.Value.Coordinates.Clone();
var tour = new PathTSPTour(coordinates, bestTspSolution.Permutation, new DoubleValue(TSPCoordinatesPathEvaluator.Apply(new TSPEuclideanPathEvaluator(), coordinates, bestTspSolution.Permutation)));
var availability = TtpUtils.GetAvailability(AvailabilityParameter.Value.ToArray());
var kspMsg = KspSolverOrchestrationPort.PrepareMessage();
kspMsg["OrchestrationMessage"] = new EnumValue(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
var lpp = new LootProfitProblem {
Tsp = (TravelingSalesmanProblem)TspParameter.Value.Clone(),
Ksp = (BinaryKnapsackProblem)KspParameter.Value.Clone(),
FixedTspSolution = bestTspSolution.Permutation,
Availability = availability,
RentingRatio = RentingRatioParameter.Value.Value,
MinSpeed = MinSpeedParameter.Value.Value,
MaxSpeed = MaxSpeedParameter.Value.Value,
};
lpp.Encoding.Length = KspParameter.Value.Length;
kspMsg["Problem"] = lpp;
KspSolverOrchestrationPort.SendMessage(kspMsg);
cts.Token.ThrowIfCancellationRequested();
var kspResults = (ResultCollection)kspMsg["Results"];
var bestKspSolution = (BinaryVector)kspResults["Best Solution"].Value.Clone();
var kspCapacity = (IntValue)KspParameter.Value.KnapsackCapacity.Clone();
var kspPenalty = new DoubleValue(0.0);
var kspWeights = (IntArray)KspParameter.Value.Weights.Clone();
var kspValues = (IntArray)KspParameter.Value.Values.Clone();
var bestKspQuality = KnapsackEvaluator.Apply(bestKspSolution, kspCapacity, kspPenalty, kspWeights, kspValues).Quality;
var loot = new KnapsackSolution(bestKspSolution, bestKspQuality, kspCapacity, kspWeights, kspValues);
#region Analyze
double objectiveValue = TtpUtils.Evaluate(TspParameter.Value, tour.Permutation.ToArray(), KspParameter.Value, loot.BinaryVector.ToArray(),
availability, RentingRatioParameter.Value.Value, MinSpeedParameter.Value.Value, MaxSpeedParameter.Value.Value);
((DoubleValue)message["Quality"]).Value = objectiveValue;
IResult bestQuality;
if (!Results.TryGetValue("Best TTP Quality", out bestQuality)) {
Results.Add(new Result("Best TTP Quality", new DoubleValue(objectiveValue)));
Results.Add(new Result("Best Tour", tour));
Results.Add(new Result("Best Loot", loot));
} else if (objectiveValue > ((DoubleValue)bestQuality.Value).Value) {
((DoubleValue)bestQuality.Value).Value = objectiveValue;
Results["Best Tour"].Value = tour;
Results["Best Loot"].Value = loot;
}
#endregion
}
#endregion
}
}