#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.Collections.Generic;
using System.Linq;
using HeuristicLab.Algorithms.CMAEvolutionStrategy;
using HeuristicLab.Algorithms.GeneticAlgorithm;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Core.Networks;
using HeuristicLab.Optimization;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Problems.FacilityLocation;
using HeuristicLab.Problems.FacilityLocation.CplexSolver;
using HeuristicLab.Problems.VehicleRouting;
using HeuristicLab.Problems.VehicleRouting.Encodings.General;
using HeuristicLab.Selection;
namespace HeuristicLab.Networks.IntegratedOptimization.LocationRouting {
[Item("LrpNetwork1", "Version 1 of a TTP optimization network.")]
[Creatable("Optimization Networks")]
[StorableClass]
public sealed class LrpNetwork1 : Network, IOptimizer {
#region Nodes
public LrpOrchestratorNode1 Orchestrator {
get { return (LrpOrchestratorNode1)Nodes["Orchestrator"]; }
}
public OrchestratedAlgorithmNode MetaSolver {
get { return (OrchestratedAlgorithmNode)Nodes["MetaSolver"]; }
}
public OrchestratedAlgorithmNode LrpSolver {
get { return (OrchestratedAlgorithmNode)Nodes["LrpSolver"]; }
}
public OrchestratedAlgorithmNode VrpSolver {
get { return (OrchestratedAlgorithmNode)Nodes["VrpSolver"]; }
}
#endregion
[StorableConstructor]
private LrpNetwork1(bool deserializing) : base(deserializing) { }
private LrpNetwork1(LrpNetwork1 original, Cloner cloner) : base(original, cloner) {
RegisterEvents();
}
private void RegisterEvents() {
MetaSolver.Algorithm.ExecutionStateChanged += OnExecutionStateChanged;
MetaSolver.Algorithm.ExecutionTimeChanged += OnExecutionTimeChanged;
MetaSolver.Algorithm.Prepared += OnPrepared;
MetaSolver.Algorithm.Started += OnStarted;
MetaSolver.Algorithm.Paused += OnPaused;
MetaSolver.Algorithm.Stopped += OnStopped;
MetaSolver.Algorithm.ExceptionOccurred += OnExceptionOccurred;
}
public LrpNetwork1() : base("LrpNetwork1") {
var orchestratorNode = new LrpOrchestratorNode1("Orchestrator");
Nodes.Add(orchestratorNode);
var metaSolverNode = new OrchestratedAlgorithmNode("MetaSolver");
var cmaes = new CMAEvolutionStrategy();
var vp = new VariegationProblem();
vp.SetMaximization(false);
cmaes.Problem = vp;
cmaes.MaximumGenerations = 80;
metaSolverNode.Algorithm = cmaes;
orchestratorNode.MetaSolverOrchestrationPort.ConnectedPort = metaSolverNode.OrchestrationPort;
Nodes.Add(metaSolverNode);
var flpSolverNode = new OrchestratedAlgorithmNode("FlpSolver");
var cplexSolver = new FLPCplexSolver();
cplexSolver.Problem = new FacilityLocationProblem();
cplexSolver.MaximumRuntimeParameter.Value.Value = TimeSpan.FromSeconds(3.0);
flpSolverNode.Algorithm = cplexSolver;
orchestratorNode.FlpSolverOrchestrationPort.ConnectedPort = flpSolverNode.OrchestrationPort;
Nodes.Add(flpSolverNode);
var vrpSolverNode = new OrchestratedAlgorithmNode("VrpSolver");
var ga = new GeneticAlgorithm();
ga.Problem = new VehicleRoutingProblem();
ga.PopulationSize.Value = 100;
var crossover = ga.CrossoverParameter.ValidValues.OfType().Single(x => x.Name == "MultiVRPSolutionCrossover");
ga.CrossoverParameter.Value = crossover;
ga.MaximumGenerations.Value = 100;
var mutator = ga.MutatorParameter.ValidValues.OfType().Single(x => x.Name == "MultiVRPSolutionManipulator");
ga.MutatorParameter.Value = mutator;
var selector = ga.SelectorParameter.ValidValues.OfType().Single();
ga.SelectorParameter.Value = selector;
vrpSolverNode.Algorithm = ga;
orchestratorNode.VrpSolverOrchestrationPort.ConnectedPort = vrpSolverNode.OrchestrationPort;
Nodes.Add(vrpSolverNode);
RegisterEvents();
}
public override IDeepCloneable Clone(Cloner cloner) {
return new LrpNetwork1(this, cloner);
}
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
RegisterEvents();
}
#region IOptimizer Members
public RunCollection Runs { get { return MetaSolver.Algorithm.Runs; } }
public IEnumerable NestedOptimizers { get { yield break; } }
public ExecutionState ExecutionState { get { return MetaSolver.Algorithm.ExecutionState; } }
public TimeSpan ExecutionTime { get { return MetaSolver.Algorithm.ExecutionTime; } }
public void Prepare(bool clearRuns) { Prepare(); }
public void Prepare() { Orchestrator.Prepare(); }
public void Start() {
if (MetaSolver.Algorithm.ExecutionState == ExecutionState.Prepared)
Orchestrator.Prepare();
Orchestrator.StartAsync();
}
public void Pause() { Orchestrator.Pause(); }
public void Stop() { Orchestrator.Stop(); }
#endregion
private void OnExecutionStateChanged(object sender, EventArgs e) { OnExecutionStateChanged(); }
private void OnExecutionTimeChanged(object sender, EventArgs e) { OnExecutionTimeChanged(); }
private void OnPrepared(object sender, EventArgs e) { OnPrepared(); }
private void OnStarted(object sender, EventArgs e) { OnStarted(); }
private void OnPaused(object sender, EventArgs e) { OnPaused(); }
private void OnStopped(object sender, EventArgs e) { OnStopped(); }
private void OnExceptionOccurred(object sender, EventArgs e) { OnExceptionOccurred(e.Value); }
public event EventHandler ExecutionStateChanged;
private void OnExecutionStateChanged() {
var handler = ExecutionStateChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler ExecutionTimeChanged;
private void OnExecutionTimeChanged() {
var handler = ExecutionTimeChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler Prepared;
private void OnPrepared() {
var handler = Prepared;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler Started;
private void OnStarted() {
var handler = Started;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler Paused;
private void OnPaused() {
var handler = Paused;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler Stopped;
private void OnStopped() {
var handler = Stopped;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler> ExceptionOccurred;
private void OnExceptionOccurred(Exception exception) {
var handler = ExceptionOccurred;
if (handler != null) handler(this, new EventArgs(exception));
}
}
}