#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 HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Core.Networks;
using HeuristicLab.Optimization;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Networks.IntegratedOptimization.TravelingThief {
[Item("TtpNetwork", "An abstract base class for TTP optimization networks.")]
[StorableClass]
public abstract class TtpNetwork : Network, IOptimizer {
#region Constants
protected const string OrchestratorNodeName = "Orchestrator";
protected const string MetaSolverNodeName = "MetaSolver";
protected const string TspSolverNodeName = "TspSolver";
protected const string KspSolverNodeName = "KspSolver";
#endregion
#region Nodes
public TtpOrchestratorNode Orchestrator {
get { return (TtpOrchestratorNode)Nodes[OrchestratorNodeName]; }
protected set {
if (Nodes.ContainsKey(OrchestratorNodeName))
throw new InvalidOperationException("Orchestrator already set.");
Nodes.Add(value);
}
}
public OrchestratedAlgorithmNode MetaSolver {
get { return (OrchestratedAlgorithmNode)Nodes[MetaSolverNodeName]; }
protected set {
if (Nodes.ContainsKey(MetaSolverNodeName))
throw new InvalidOperationException("MetaSolver already set.");
Nodes.Add(value);
RegisterEvents();
}
}
public OrchestratedAlgorithmNode TspSolver {
get { return (OrchestratedAlgorithmNode)Nodes[TspSolverNodeName]; }
protected set {
if (Nodes.ContainsKey(TspSolverNodeName))
throw new InvalidOperationException("TspSolver already set.");
Nodes.Add(value);
}
}
public OrchestratedAlgorithmNode KspSolver {
get { return (OrchestratedAlgorithmNode)Nodes[KspSolverNodeName]; }
protected set {
if (Nodes.ContainsKey(KspSolverNodeName))
throw new InvalidOperationException("KspSolver already set.");
Nodes.Add(value);
}
}
#endregion
[StorableConstructor]
protected TtpNetwork(bool deserializing) : base(deserializing) { }
protected TtpNetwork(TtpNetwork original, Cloner cloner) : base(original, cloner) {
RegisterEvents();
}
protected TtpNetwork() : this("TtpNetwork") { }
protected TtpNetwork(string name) : base(name) { }
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
RegisterEvents();
}
private void RegisterEvents() {
MetaSolver.AlgorithmChanged += MetaSolver_AlgorithmChanged;
RegisterAlgorithmEvents();
}
private void MetaSolver_AlgorithmChanged(object sender, EventArgs e) {
RegisterAlgorithmEvents();
}
private void RegisterAlgorithmEvents() {
var algorithm = MetaSolver.Algorithm;
if (algorithm == null) return;
algorithm.ExecutionStateChanged += (s, e) => OnExecutionStateChanged();
algorithm.ExecutionTimeChanged += (s, e) => OnExecutionTimeChanged();
algorithm.Prepared += (s, e) => OnPrepared();
algorithm.Started += (s, e) => OnStarted();
algorithm.Paused += (s, e) => OnPaused();
algorithm.Stopped += (s, e) => OnStopped();
algorithm.ExceptionOccurred += (s, e) => OnExceptionOccurred(e.Value);
}
#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() { Prepare(false); }
public void Prepare(bool clearRuns) { Orchestrator.Prepare(clearRuns); }
public void Start() { Orchestrator.StartAsync(); }
public void Pause() { Orchestrator.Pause(); }
public void Stop() { Orchestrator.Stop(); }
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));
}
#endregion
}
}