[11484] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17226] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11484] | 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;
|
---|
[11739] | 23 | using System.Collections.Generic;
|
---|
[11484] | 24 | using System.Globalization;
|
---|
| 25 | using System.Linq;
|
---|
[16753] | 26 | using HEAL.Attic;
|
---|
[11484] | 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Data;
|
---|
| 30 | using HeuristicLab.Operators;
|
---|
| 31 | using HeuristicLab.Parameters;
|
---|
| 32 |
|
---|
[11949] | 33 | namespace HeuristicLab.Optimization {
|
---|
[11484] | 34 | [Item("Single-objective MoveGenerator", "Calls the GetNeighbors method of the problem definition to obtain the moves.")]
|
---|
[16723] | 35 | [StorableType("CB37E7D8-EAC3-4061-9D39-20538CD1064D")]
|
---|
[16753] | 36 | internal sealed class SingleObjectiveMoveGenerator<TEncodedSolution> : SingleSuccessorOperator, INeighborBasedOperator<TEncodedSolution>, IMultiMoveGenerator, IStochasticOperator, ISingleObjectiveMoveOperator
|
---|
[16751] | 37 | where TEncodedSolution : class, IEncodedSolution {
|
---|
[11484] | 38 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 39 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | public IValueLookupParameter<IntValue> SampleSizeParameter {
|
---|
| 43 | get { return (IValueLookupParameter<IntValue>)Parameters["SampleSize"]; }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[16751] | 46 | public ILookupParameter<IEncoding<TEncodedSolution>> EncodingParameter {
|
---|
| 47 | get { return (ILookupParameter<IEncoding<TEncodedSolution>>)Parameters["Encoding"]; }
|
---|
[11484] | 48 | }
|
---|
| 49 |
|
---|
[17363] | 50 | public Func<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom, IEnumerable<ISingleObjectiveSolutionContext<TEncodedSolution>>> GetNeighbors { get; set; }
|
---|
[11739] | 51 |
|
---|
[11484] | 52 | [StorableConstructor]
|
---|
[16753] | 53 | private SingleObjectiveMoveGenerator(StorableConstructorFlag _) : base(_) { }
|
---|
| 54 | private SingleObjectiveMoveGenerator(SingleObjectiveMoveGenerator<TEncodedSolution> original, Cloner cloner)
|
---|
[11484] | 55 | : base(original, cloner) { }
|
---|
| 56 | public SingleObjectiveMoveGenerator() {
|
---|
| 57 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
|
---|
| 58 | Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of moves to sample."));
|
---|
[16751] | 59 | Parameters.Add(new LookupParameter<IEncoding<TEncodedSolution>>("Encoding", "An item that holds the problem's encoding."));
|
---|
[11484] | 60 | }
|
---|
| 61 |
|
---|
| 62 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[16751] | 63 | return new SingleObjectiveMoveGenerator<TEncodedSolution>(this, cloner);
|
---|
[11484] | 64 | }
|
---|
| 65 |
|
---|
[11739] | 66 | public override IOperation Apply() {
|
---|
[11484] | 67 | var random = RandomParameter.ActualValue;
|
---|
| 68 | var sampleSize = SampleSizeParameter.ActualValue.Value;
|
---|
| 69 | var encoding = EncodingParameter.ActualValue;
|
---|
[16751] | 70 | var solution = ScopeUtil.GetEncodedSolution(ExecutionContext.Scope, encoding);
|
---|
[17363] | 71 | var solutionContext = new SingleObjectiveSolutionContextScope<TEncodedSolution>(ExecutionContext.Scope, solution);
|
---|
[11484] | 72 |
|
---|
[17363] | 73 | var nbhood = GetNeighbors(solutionContext, random).Take(sampleSize).ToList();
|
---|
[11484] | 74 | var moveScopes = new Scope[nbhood.Count];
|
---|
| 75 | for (int i = 0; i < moveScopes.Length; i++) {
|
---|
| 76 | moveScopes[i] = new Scope(i.ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
[17363] | 77 | ScopeUtil.CopyEncodedSolutionToScope(moveScopes[i], encoding, nbhood[i].EncodedSolution);
|
---|
[11484] | 78 | }
|
---|
| 79 | ExecutionContext.Scope.SubScopes.AddRange(moveScopes);
|
---|
| 80 |
|
---|
[11739] | 81 | return base.Apply();
|
---|
[11484] | 82 | }
|
---|
| 83 | }
|
---|
| 84 | }
|
---|