#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.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.Evolutionary { [Item("OSGA (GQAP) Context", "Context for OSGA (GQAP).")] [StorableClass] public sealed class OSGAContext : SingleObjectivePopulationContext> { [Storable] private IValueParameter problem; public GQAP Problem { get { return problem.Value; } set { problem.Value = value; } } [Storable] private IValueParameter bestSolution; public GQAPSolution BestSolution { get { return bestSolution.Value; } set { bestSolution.Value = value; } } [Storable] private IValueParameter>> nextGeneration; public ItemList> NextGeneration { get { return nextGeneration.Value; } set { nextGeneration.Value = value; } } [Storable] private IValueParameter selectionPressure; public double SelectionPressure { get { return selectionPressure.Value.Value; } set { selectionPressure.Value.Value = value; } } [Storable] private IValueParameter attempts; public int Attempts { get { return attempts.Value.Value; } set { attempts.Value.Value = value; } } public void SortPopulation() { Scope.SubScopes.Replace(Scope.SubScopes.OfType>().OrderBy(x => Problem.Maximization ? -x.Fitness : x.Fitness).ToList()); } [StorableConstructor] private OSGAContext(bool deserializing) : base(deserializing) { } private OSGAContext(OSGAContext original, Cloner cloner) : base(original, cloner) { problem = cloner.Clone(original.problem); bestSolution = cloner.Clone(original.bestSolution); nextGeneration = cloner.Clone(original.nextGeneration); selectionPressure = cloner.Clone(original.selectionPressure); attempts = cloner.Clone(original.attempts); } public OSGAContext() : this("OSGA (GQAP) Context") { } public OSGAContext(string name) : base(name) { Parameters.Add(problem = new ValueParameter("Problem")); Parameters.Add(bestSolution = new ValueParameter("BestFoundSolution")); Parameters.Add(nextGeneration = new ValueParameter>>("NextGeneration")); Parameters.Add(selectionPressure = new ValueParameter("SelectionPressure", new DoubleValue(0))); Parameters.Add(attempts = new ValueParameter("Attempts", new IntValue(0))); } public override IDeepCloneable Clone(Cloner cloner) { return new OSGAContext(this, cloner); } public ISingleObjectiveSolutionScope ToScope(GQAPSolution code, double fitness = double.NaN) { var name = Problem.Encoding.Name; var scope = new SingleObjectiveSolutionScope(code, name + "Solution", fitness, Problem.Evaluator.QualityParameter.ActualName) { Parent = Scope }; scope.Variables.Add(new Variable(name, code.Assignment)); scope.Variables.Add(new Variable("Evaluation", code.Evaluation)); return scope; } } }