[15553] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System.Linq;
|
---|
[16728] | 23 | using HEAL.Attic;
|
---|
[15553] | 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
[15564] | 26 | using HeuristicLab.Data;
|
---|
[15553] | 27 | using HeuristicLab.Parameters;
|
---|
| 28 |
|
---|
[15564] | 29 | namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.Evolutionary {
|
---|
| 30 | [Item("OSGA (GQAP) Context", "Context for OSGA (GQAP).")]
|
---|
[16728] | 31 | [StorableType("65ADDF53-91A6-4C8C-9DDE-B330E87AB32B")]
|
---|
[15572] | 32 | public sealed class OSGAContext : PopulationContext<ISingleObjectiveSolutionScope<GQAPSolution>> {
|
---|
[15553] | 33 |
|
---|
| 34 | [Storable]
|
---|
| 35 | private IValueParameter<GQAP> problem;
|
---|
| 36 | public GQAP Problem {
|
---|
| 37 | get { return problem.Value; }
|
---|
| 38 | set { problem.Value = value; }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | [Storable]
|
---|
| 42 | private IValueParameter<GQAPSolution> bestSolution;
|
---|
| 43 | public GQAPSolution BestSolution {
|
---|
| 44 | get { return bestSolution.Value; }
|
---|
| 45 | set { bestSolution.Value = value; }
|
---|
| 46 | }
|
---|
[15564] | 47 |
|
---|
| 48 | [Storable]
|
---|
| 49 | private IValueParameter<ItemList<ISingleObjectiveSolutionScope<GQAPSolution>>> nextGeneration;
|
---|
| 50 | public ItemList<ISingleObjectiveSolutionScope<GQAPSolution>> NextGeneration {
|
---|
| 51 | get { return nextGeneration.Value; }
|
---|
| 52 | set { nextGeneration.Value = value; }
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | [Storable]
|
---|
| 56 | private IValueParameter<DoubleValue> selectionPressure;
|
---|
| 57 | public double SelectionPressure {
|
---|
| 58 | get { return selectionPressure.Value.Value; }
|
---|
| 59 | set { selectionPressure.Value.Value = value; }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | [Storable]
|
---|
| 63 | private IValueParameter<IntValue> attempts;
|
---|
| 64 | public int Attempts {
|
---|
| 65 | get { return attempts.Value.Value; }
|
---|
| 66 | set { attempts.Value.Value = value; }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[15553] | 69 | public void SortPopulation() {
|
---|
[15562] | 70 | Scope.SubScopes.Replace(Scope.SubScopes.OfType<ISingleObjectiveSolutionScope<GQAPSolution>>().OrderBy(x => Problem.Maximization ? -x.Fitness : x.Fitness).ToList());
|
---|
[15553] | 71 | }
|
---|
| 72 |
|
---|
| 73 | [StorableConstructor]
|
---|
[16728] | 74 | private OSGAContext(StorableConstructorFlag _) : base(_) { }
|
---|
[15564] | 75 | private OSGAContext(OSGAContext original, Cloner cloner)
|
---|
[15553] | 76 | : base(original, cloner) {
|
---|
| 77 | problem = cloner.Clone(original.problem);
|
---|
| 78 | bestSolution = cloner.Clone(original.bestSolution);
|
---|
[15564] | 79 | nextGeneration = cloner.Clone(original.nextGeneration);
|
---|
| 80 | selectionPressure = cloner.Clone(original.selectionPressure);
|
---|
| 81 | attempts = cloner.Clone(original.attempts);
|
---|
[15553] | 82 | }
|
---|
[15564] | 83 | public OSGAContext() : this("OSGA (GQAP) Context") { }
|
---|
| 84 | public OSGAContext(string name) : base(name) {
|
---|
[15553] | 85 | Parameters.Add(problem = new ValueParameter<GQAP>("Problem"));
|
---|
| 86 | Parameters.Add(bestSolution = new ValueParameter<GQAPSolution>("BestFoundSolution"));
|
---|
[15564] | 87 | Parameters.Add(nextGeneration = new ValueParameter<ItemList<ISingleObjectiveSolutionScope<GQAPSolution>>>("NextGeneration"));
|
---|
| 88 | Parameters.Add(selectionPressure = new ValueParameter<DoubleValue>("SelectionPressure", new DoubleValue(0)));
|
---|
| 89 | Parameters.Add(attempts = new ValueParameter<IntValue>("Attempts", new IntValue(0)));
|
---|
[15553] | 90 | }
|
---|
| 91 |
|
---|
| 92 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[15564] | 93 | return new OSGAContext(this, cloner);
|
---|
[15553] | 94 | }
|
---|
| 95 |
|
---|
| 96 | public ISingleObjectiveSolutionScope<GQAPSolution> ToScope(GQAPSolution code, double fitness = double.NaN) {
|
---|
| 97 | var name = Problem.Encoding.Name;
|
---|
| 98 | var scope = new SingleObjectiveSolutionScope<GQAPSolution>(code,
|
---|
| 99 | name + "Solution", fitness, Problem.Evaluator.QualityParameter.ActualName) {
|
---|
| 100 | Parent = Scope
|
---|
| 101 | };
|
---|
| 102 | scope.Variables.Add(new Variable(name, code.Assignment));
|
---|
| 103 | scope.Variables.Add(new Variable("Evaluation", code.Evaluation));
|
---|
| 104 | return scope;
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | }
|
---|