[11484] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15584] | 3 | * Copyright (C) 2002-2018 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;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.Operators;
|
---|
| 30 | using HeuristicLab.Parameters;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 32 |
|
---|
[11949] | 33 | namespace HeuristicLab.Optimization {
|
---|
[11484] | 34 | [Item("Single-objective MoveGenerator", "Calls the GetNeighbors method of the problem definition to obtain the moves.")]
|
---|
| 35 | [StorableClass]
|
---|
[11753] | 36 | public class SingleObjectiveMoveGenerator : SingleSuccessorOperator, INeighborBasedOperator, IMultiMoveGenerator, IStochasticOperator, ISingleObjectiveMoveOperator {
|
---|
[11484] | 37 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 38 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | public IValueLookupParameter<IntValue> SampleSizeParameter {
|
---|
| 42 | get { return (IValueLookupParameter<IntValue>)Parameters["SampleSize"]; }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[11559] | 45 | public ILookupParameter<IEncoding> EncodingParameter {
|
---|
| 46 | get { return (ILookupParameter<IEncoding>)Parameters["Encoding"]; }
|
---|
[11484] | 47 | }
|
---|
| 48 |
|
---|
[11739] | 49 | public Func<Individual, IRandom, IEnumerable<Individual>> GetNeighborsFunc { get; set; }
|
---|
| 50 |
|
---|
[11484] | 51 | [StorableConstructor]
|
---|
| 52 | protected SingleObjectiveMoveGenerator(bool deserializing) : base(deserializing) { }
|
---|
| 53 | protected SingleObjectiveMoveGenerator(SingleObjectiveMoveGenerator original, Cloner cloner)
|
---|
| 54 | : base(original, cloner) { }
|
---|
| 55 | public SingleObjectiveMoveGenerator() {
|
---|
| 56 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
|
---|
| 57 | Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of moves to sample."));
|
---|
[11559] | 58 | Parameters.Add(new LookupParameter<IEncoding>("Encoding", "An item that holds the problem's encoding."));
|
---|
[11484] | 59 | }
|
---|
| 60 |
|
---|
| 61 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 62 | return new SingleObjectiveMoveGenerator(this, cloner);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[11739] | 65 | public override IOperation Apply() {
|
---|
[11484] | 66 | var random = RandomParameter.ActualValue;
|
---|
| 67 | var sampleSize = SampleSizeParameter.ActualValue.Value;
|
---|
| 68 | var encoding = EncodingParameter.ActualValue;
|
---|
[11619] | 69 | var individual = encoding.GetIndividual(ExecutionContext.Scope);
|
---|
[11739] | 70 | var nbhood = GetNeighborsFunc(individual, random).Take(sampleSize).ToList();
|
---|
[11484] | 71 |
|
---|
| 72 | var moveScopes = new Scope[nbhood.Count];
|
---|
| 73 | for (int i = 0; i < moveScopes.Length; i++) {
|
---|
| 74 | moveScopes[i] = new Scope(i.ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
[11885] | 75 | nbhood[i].CopyToScope(moveScopes[i]);
|
---|
[11484] | 76 | }
|
---|
| 77 | ExecutionContext.Scope.SubScopes.AddRange(moveScopes);
|
---|
| 78 |
|
---|
[11739] | 79 | return base.Apply();
|
---|
[11484] | 80 | }
|
---|
| 81 | }
|
---|
| 82 | }
|
---|