#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 Nodes public TtpOrchestratorNode1 Orchestrator { get { return (TtpOrchestratorNode1)Nodes["Orchestrator"]; } } public OrchestratedAlgorithmNode MetaSolver { get { return (OrchestratedAlgorithmNode)Nodes["MetaSolver"]; } } public OrchestratedAlgorithmNode TspSolver { get { return (OrchestratedAlgorithmNode)Nodes["TspSolver"]; } } public OrchestratedAlgorithmNode KspSolver { get { return (OrchestratedAlgorithmNode)Nodes["KspSolver"]; } } #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) { Nodes.Add(new TtpOrchestratorNode1("Orchestrator")); Nodes.Add(new OrchestratedAlgorithmNode("MetaSolver")); Nodes.Add(new OrchestratedAlgorithmNode("TspSolver")); Nodes.Add(new OrchestratedAlgorithmNode("KspSolver")); RegisterEvents(); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { RegisterEvents(); } protected virtual void RegisterEvents() { MetaSolver.AlgorithmChanged += MetaSolver_AlgorithmChanged; RegisterAlgorithmEvents(); } protected virtual void RegisterAlgorithmEvents() { var algorithm = MetaSolver.Algorithm; if (algorithm == null) return; algorithm.ExecutionStateChanged += OnExecutionStateChanged; algorithm.ExecutionTimeChanged += OnExecutionTimeChanged; algorithm.Prepared += OnPrepared; algorithm.Started += OnStarted; algorithm.Paused += OnPaused; algorithm.Stopped += OnStopped; algorithm.ExceptionOccurred += OnExceptionOccurred; } protected virtual void MetaSolver_AlgorithmChanged(object sender, EventArgs e) { RegisterAlgorithmEvents(); } #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(); } #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)); } } }