[11635] | 1 | #region License Information
|
---|
[11956] | 2 |
|
---|
[11635] | 3 | /* HeuristicLab
|
---|
[17181] | 4 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11635] | 5 | *
|
---|
| 6 | * This file is part of HeuristicLab.
|
---|
| 7 | *
|
---|
| 8 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 9 | * it under the terms of the GNU General Public License as published by
|
---|
| 10 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 11 | * (at your option) any later version.
|
---|
| 12 | *
|
---|
| 13 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 16 | * GNU General Public License for more details.
|
---|
| 17 | *
|
---|
| 18 | * You should have received a copy of the GNU General Public License
|
---|
| 19 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 20 | */
|
---|
[11956] | 21 |
|
---|
[11635] | 22 | #endregion
|
---|
| 23 |
|
---|
[12005] | 24 | using System;
|
---|
| 25 | using System.Linq;
|
---|
[15217] | 26 | using HeuristicLab.Analysis;
|
---|
[11635] | 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Data;
|
---|
[11987] | 30 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
[11635] | 31 | using HeuristicLab.Optimization;
|
---|
[15217] | 32 | using HeuristicLab.Optimization.Operators;
|
---|
[11635] | 33 | using HeuristicLab.Parameters;
|
---|
[17097] | 34 | using HEAL.Attic;
|
---|
[11635] | 35 |
|
---|
[11987] | 36 | namespace HeuristicLab.Problems.Binary {
|
---|
[17097] | 37 | [StorableType("2F6FEB34-BD19-47AF-9484-7F48565C0C43")]
|
---|
[11987] | 38 | public abstract class BinaryProblem : SingleObjectiveBasicProblem<BinaryVectorEncoding> {
|
---|
| 39 | public virtual int Length {
|
---|
| 40 | get { return Encoding.Length; }
|
---|
| 41 | set { Encoding.Length = value; }
|
---|
[11635] | 42 | }
|
---|
| 43 |
|
---|
[12005] | 44 | private IFixedValueParameter<IntValue> LengthParameter {
|
---|
| 45 | get { return (IFixedValueParameter<IntValue>)Parameters["Length"]; }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[11987] | 48 | [StorableConstructor]
|
---|
[17097] | 49 | protected BinaryProblem(StorableConstructorFlag _) : base(_) { }
|
---|
[12005] | 50 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 51 | private void AfterDeserialization() {
|
---|
| 52 | RegisterEventHandlers();
|
---|
| 53 | }
|
---|
[11635] | 54 |
|
---|
[12005] | 55 | protected BinaryProblem(BinaryProblem original, Cloner cloner)
|
---|
| 56 | : base(original, cloner) {
|
---|
| 57 | RegisterEventHandlers();
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | protected BinaryProblem()
|
---|
| 61 | : base() {
|
---|
| 62 | var lengthParameter = new FixedValueParameter<IntValue>("Length", "The length of the BinaryVector.", new IntValue(10));
|
---|
| 63 | Parameters.Add(lengthParameter);
|
---|
| 64 | Encoding.LengthParameter = lengthParameter;
|
---|
[15217] | 65 | Operators.Add(new HammingSimilarityCalculator());
|
---|
| 66 | Operators.Add(new QualitySimilarityCalculator());
|
---|
| 67 | Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
|
---|
| 68 | Parameterize();
|
---|
[12005] | 69 | RegisterEventHandlers();
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[11987] | 72 | public virtual bool IsBetter(double quality, double bestQuality) {
|
---|
[11640] | 73 | return (Maximization && quality > bestQuality || !Maximization && quality < bestQuality);
|
---|
| 74 | }
|
---|
[11635] | 75 |
|
---|
[12005] | 76 | public abstract double Evaluate(BinaryVector vector, IRandom random);
|
---|
[11987] | 77 | public sealed override double Evaluate(Individual individual, IRandom random) {
|
---|
| 78 | return Evaluate(individual.BinaryVector(), random);
|
---|
[11635] | 79 | }
|
---|
| 80 |
|
---|
[12005] | 81 | public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
|
---|
| 82 | base.Analyze(individuals, qualities, results, random);
|
---|
| 83 | var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality);
|
---|
| 84 | var best = Maximization ? orderedIndividuals.Last().Individual : orderedIndividuals.First().Individual;
|
---|
| 85 |
|
---|
| 86 | if (!results.ContainsKey("Best Solution")) {
|
---|
| 87 | results.Add(new Result("Best Solution", typeof(BinaryVector)));
|
---|
| 88 | }
|
---|
| 89 | results["Best Solution"].Value = (IItem)best.BinaryVector().Clone();
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | protected override void OnEncodingChanged() {
|
---|
| 93 | base.OnEncodingChanged();
|
---|
| 94 | Encoding.LengthParameter = LengthParameter;
|
---|
[15217] | 95 | Parameterize();
|
---|
[12005] | 96 | }
|
---|
| 97 |
|
---|
[15217] | 98 | private void Parameterize() {
|
---|
| 99 | foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
|
---|
| 100 | similarityCalculator.SolutionVariableName = Encoding.SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
| 101 | similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
[12005] | 104 |
|
---|
| 105 | private void RegisterEventHandlers() {
|
---|
| 106 | LengthParameter.Value.ValueChanged += LengthParameter_ValueChanged;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | protected virtual void LengthParameter_ValueChanged(object sender, EventArgs e) { }
|
---|
[11635] | 110 | }
|
---|
| 111 | }
|
---|