#region License Information /* HeuristicLab * Copyright (C) 2002-2016 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.MemPR.Interfaces; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Random; namespace HeuristicLab.Algorithms.MemPR { [Item("MemPRContext", "Abstract base class for MemPR contexts.")] [StorableClass] public abstract class MemPRPopulationContext : ParameterizedNamedItem, IPopulationBasedHeuristicAlgorithmContext, ISolutionModelContext where TProblem : class, IItem, ISingleObjectiveProblemDefinition where TSolution : class, IItem where TPopulationContext : MemPRPopulationContext where TSolutionContext : MemPRSolutionContext { private IExecutionContext parent; public IExecutionContext Parent { get { return parent; } set { parent = value; } } [Storable] private IScope scope; public IScope Scope { get { return scope; } private set { scope = value; } } IKeyedItemCollection IExecutionContext.Parameters { get { return Parameters; } } [Storable] private IValueParameter problem; public TProblem Problem { get { return problem.Value; } set { problem.Value = value; } } [Storable] private IValueParameter initialized; public bool Initialized { get { return initialized.Value.Value; } set { initialized.Value.Value = value; } } [Storable] private IValueParameter iterations; public int Iterations { get { return iterations.Value.Value; } set { iterations.Value.Value = value; } } [Storable] private IValueParameter evaluatedSolutions; public int EvaluatedSolutions { get { return evaluatedSolutions.Value.Value; } set { evaluatedSolutions.Value.Value = value; } } [Storable] private IValueParameter bestQuality; public double BestQuality { get { return bestQuality.Value.Value; } set { bestQuality.Value.Value = value; } } [Storable] private IValueParameter bestSolution; public TSolution BestSolution { get { return bestSolution.Value; } set { bestSolution.Value = value; } } [Storable] private IValueParameter hcSteps; public int HcSteps { get { return hcSteps.Value.Value; } set { hcSteps.Value.Value = value; } } [Storable] private IValueParameter byBreeding; public int ByBreeding { get { return byBreeding.Value.Value; } set { byBreeding.Value.Value = value; } } [Storable] private IValueParameter byRelinking; public int ByRelinking { get { return byRelinking.Value.Value; } set { byRelinking.Value.Value = value; } } [Storable] private IValueParameter bySampling; public int BySampling { get { return bySampling.Value.Value; } set { bySampling.Value.Value = value; } } [Storable] private IValueParameter byHillclimbing; public int ByHillclimbing { get { return byHillclimbing.Value.Value; } set { byHillclimbing.Value.Value = value; } } [Storable] private IValueParameter byTabuwalking; public int ByTabuwalking { get { return byTabuwalking.Value.Value; } set { byTabuwalking.Value.Value = value; } } [Storable] private IValueParameter random; public IRandom Random { get { return random.Value; } set { random.Value = value; } } public IEnumerable> Population { get { return scope.SubScopes.OfType>(); } } public void AddToPopulation(ISingleObjectiveSolutionScope solScope) { scope.SubScopes.Add(solScope); } public void ReplaceAtPopulation(int index, ISingleObjectiveSolutionScope solScope) { scope.SubScopes[index] = solScope; } public ISingleObjectiveSolutionScope AtPopulation(int index) { return scope.SubScopes[index] as ISingleObjectiveSolutionScope; } public int PopulationCount { get { return scope.SubScopes.Count; } } [Storable] private List> breedingStat; public List> BreedingStat { get { return breedingStat; } } [Storable] private List> hillclimbingStat; public List> HillclimbingStat { get { return hillclimbingStat; } } [Storable] private List> tabuwalkingStat; public List> TabuwalkingStat { get { return tabuwalkingStat; } } [Storable] public ISolutionModel Model { get; set; } [StorableConstructor] protected MemPRPopulationContext(bool deserializing) : base(deserializing) { } protected MemPRPopulationContext(MemPRPopulationContext original, Cloner cloner) : base(original, cloner) { scope = cloner.Clone(original.scope); problem = cloner.Clone(original.problem); initialized = cloner.Clone(original.initialized); iterations = cloner.Clone(original.iterations); evaluatedSolutions = cloner.Clone(original.evaluatedSolutions); bestQuality = cloner.Clone(original.bestQuality); bestSolution = cloner.Clone(original.bestSolution); hcSteps = cloner.Clone(original.hcSteps); byBreeding = cloner.Clone(original.byBreeding); byRelinking = cloner.Clone(original.byRelinking); bySampling = cloner.Clone(original.bySampling); byHillclimbing = cloner.Clone(original.byHillclimbing); byTabuwalking = cloner.Clone(original.byTabuwalking); random = cloner.Clone(original.random); breedingStat = original.breedingStat.Select(x => Tuple.Create(x.Item1, x.Item2, x.Item3)).ToList(); hillclimbingStat = original.hillclimbingStat.Select(x => Tuple.Create(x.Item1, x.Item2)).ToList(); tabuwalkingStat = original.tabuwalkingStat.Select(x => Tuple.Create(x.Item1, x.Item2)).ToList(); Model = cloner.Clone(original.Model); } public MemPRPopulationContext() : this("MemPRContext") { } public MemPRPopulationContext(string name) : base(name) { scope = new Scope("Global"); Parameters.Add(problem = new ValueParameter("Problem")); Parameters.Add(initialized = new ValueParameter("Initialized", new BoolValue(false))); Parameters.Add(iterations = new ValueParameter("Iterations", new IntValue(0))); Parameters.Add(evaluatedSolutions = new ValueParameter("EvaluatedSolutions", new IntValue(0))); Parameters.Add(bestQuality = new ValueParameter("BestQuality", new DoubleValue(double.NaN))); Parameters.Add(bestSolution = new ValueParameter("BestSolution")); Parameters.Add(hcSteps = new ValueParameter("HcSteps", new IntValue(0))); Parameters.Add(byBreeding = new ValueParameter("ByBreeding", new IntValue(0))); Parameters.Add(byRelinking = new ValueParameter("ByRelinking", new IntValue(0))); Parameters.Add(bySampling = new ValueParameter("BySampling", new IntValue(0))); Parameters.Add(byHillclimbing = new ValueParameter("ByHillclimbing", new IntValue(0))); Parameters.Add(byTabuwalking = new ValueParameter("ByTabuwalking", new IntValue(0))); Parameters.Add(random = new ValueParameter("Random", new MersenneTwister())); breedingStat = new List>(); hillclimbingStat = new List>(); tabuwalkingStat = new List>(); } public abstract TSolutionContext CreateSingleSolutionContext(ISingleObjectiveSolutionScope solution); public void IncrementEvaluatedSolutions(int byEvaluations) { if (byEvaluations < 0) throw new ArgumentException("Can only increment and not decrement evaluated solutions."); EvaluatedSolutions += byEvaluations; } #region IExecutionContext members public IAtomicOperation CreateOperation(IOperator op) { return new ExecutionContext(this, op, Scope); } public IAtomicOperation CreateOperation(IOperator op, IScope s) { return new ExecutionContext(this, op, s); } public IAtomicOperation CreateChildOperation(IOperator op) { return new ExecutionContext(this, op, Scope); } public IAtomicOperation CreateChildOperation(IOperator op, IScope s) { return new ExecutionContext(this, op, s); } #endregion } [Item("SingleSolutionMemPRContext", "Abstract base class for single solution MemPR contexts.")] [StorableClass] public abstract class MemPRSolutionContext : ParameterizedNamedItem, ISingleSolutionHeuristicAlgorithmContext where TProblem : class, IItem, ISingleObjectiveProblemDefinition where TSolution : class, IItem where TContext : MemPRPopulationContext where TSolutionContext : MemPRSolutionContext { private TContext parent; public IExecutionContext Parent { get { return parent; } set { throw new InvalidOperationException("Cannot set the parent of a single-solution context."); } } [Storable] private ISingleObjectiveSolutionScope scope; public IScope Scope { get { return scope; } } IKeyedItemCollection IExecutionContext.Parameters { get { return Parameters; } } public TProblem Problem { get { return parent.Problem; } } public double BestQuality { get { return parent.BestQuality; } set { parent.BestQuality = value; } } public TSolution BestSolution { get { return parent.BestSolution; } set { parent.BestSolution = value; } } public IRandom Random { get { return parent.Random; } } [Storable] private IValueParameter evaluatedSolutions; public int EvaluatedSolutions { get { return evaluatedSolutions.Value.Value; } set { evaluatedSolutions.Value.Value = value; } } [Storable] private IValueParameter iterations; public int Iterations { get { return iterations.Value.Value; } set { iterations.Value.Value = value; } } ISingleObjectiveSolutionScope ISingleSolutionHeuristicAlgorithmContext.Solution { get { return scope; } } [StorableConstructor] protected MemPRSolutionContext(bool deserializing) : base(deserializing) { } protected MemPRSolutionContext(MemPRSolutionContext original, Cloner cloner) : base(original, cloner) { scope = cloner.Clone(original.scope); evaluatedSolutions = cloner.Clone(original.evaluatedSolutions); iterations = cloner.Clone(original.iterations); } public MemPRSolutionContext(TContext baseContext, ISingleObjectiveSolutionScope solution) { parent = baseContext; scope = solution; Parameters.Add(evaluatedSolutions = new ValueParameter("EvaluatedSolutions", new IntValue(0))); Parameters.Add(iterations = new ValueParameter("Iterations", new IntValue(0))); } public void IncrementEvaluatedSolutions(int byEvaluations) { if (byEvaluations < 0) throw new ArgumentException("Can only increment and not decrement evaluated solutions."); EvaluatedSolutions += byEvaluations; } #region IExecutionContext members public IAtomicOperation CreateOperation(IOperator op) { return new ExecutionContext(this, op, Scope); } public IAtomicOperation CreateOperation(IOperator op, IScope s) { return new ExecutionContext(this, op, s); } public IAtomicOperation CreateChildOperation(IOperator op) { return new ExecutionContext(this, op, Scope); } public IAtomicOperation CreateChildOperation(IOperator op, IScope s) { return new ExecutionContext(this, op, s); } #endregion } }