#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.LocationRouting { [Item("LrpNetwork", "An abstract base class for LRP optimization networks.")] [StorableClass] public abstract class LrpNetwork : Network { #region protected const string OrchestratorNodeName = "Orchestrator"; protected const string MetaSolverNodeName = "MetaSolver"; protected const string FlpSolverNodeName = "FlpSolver"; protected const string VrpSolverNodeName = "VrpSolver"; #endregion #region Nodes public LrpOrchestratorNode Orchestrator { get { return (LrpOrchestratorNode)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 FlpSolver { get { return (OrchestratedAlgorithmNode)Nodes[FlpSolverNodeName]; } protected set { if (Nodes.ContainsKey(FlpSolverNodeName)) throw new InvalidOperationException("FlpSolver already set."); Nodes.Add(value); } } public OrchestratedAlgorithmNode VrpSolver { get { return (OrchestratedAlgorithmNode)Nodes[VrpSolverNodeName]; } protected set { if (Nodes.ContainsKey(VrpSolverNodeName)) throw new InvalidOperationException("VrpSolver already set."); Nodes.Add(value); } } #endregion [StorableConstructor] protected LrpNetwork(bool deserializing) : base(deserializing) { } protected LrpNetwork(LrpNetwork original, Cloner cloner) : base(original, cloner) { RegisterEvents(); } protected LrpNetwork() : this("LrpNetwork") { } protected LrpNetwork(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 } }