Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveMoveGenerator.cs @ 11587

Last change on this file since 11587 was 11559, checked in by mkommend, 10 years ago

#2174: Adapted IEncoding and Encoding base class.

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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
22using System;
23using System.Globalization;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.Programmable {
34  [Item("Single-objective MoveGenerator", "Calls the GetNeighbors method of the problem definition to obtain the moves.")]
35  [StorableClass]
36  public class SingleObjectiveMoveGenerator : InstrumentedOperator, ISingleObjectiveMoveOperator, IMultiMoveGenerator, IStochasticOperator {
37
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
46    public ILookupParameter<ISingleObjectiveProblemDefinition> ProblemDefinitionParameter {
47      get { return (ILookupParameter<ISingleObjectiveProblemDefinition>)Parameters["ProblemDefinition"]; }
48    }
49
50    public ILookupParameter<IEncoding> EncodingParameter {
51      get { return (ILookupParameter<IEncoding>)Parameters["Encoding"]; }
52    }
53
54    [StorableConstructor]
55    protected SingleObjectiveMoveGenerator(bool deserializing) : base(deserializing) { }
56    protected SingleObjectiveMoveGenerator(SingleObjectiveMoveGenerator original, Cloner cloner)
57      : base(original, cloner) { }
58    public SingleObjectiveMoveGenerator() {
59      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
60      Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of moves to sample."));
61      Parameters.Add(new LookupParameter<ISingleObjectiveProblemDefinition>("ProblemDefinition", "The host that holds the problem definition."));
62      Parameters.Add(new LookupParameter<IEncoding>("Encoding", "An item that holds the problem's encoding."));
63    }
64
65    public override IDeepCloneable Clone(Cloner cloner) {
66      return new SingleObjectiveMoveGenerator(this, cloner);
67    }
68
69    public override IOperation InstrumentedApply() {
70      var random = RandomParameter.ActualValue;
71      var definition = ProblemDefinitionParameter.ActualValue;
72      if (definition == null) throw new InvalidOperationException("Problem definition is null.");
73      var sampleSize = SampleSizeParameter.ActualValue.Value;
74      var encoding = EncodingParameter.ActualValue;
75      var vector = Helper.Extract(ExecutionContext.Scope, encoding);
76      var nbhood = definition.GetNeighbors(random, vector).Take(sampleSize).ToList();
77
78      var moveScopes = new Scope[nbhood.Count];
79      for (int i = 0; i < moveScopes.Length; i++) {
80        moveScopes[i] = new Scope(i.ToString(CultureInfo.InvariantCulture.NumberFormat));
81        Helper.Write(moveScopes[i], nbhood[i]);
82      }
83      ExecutionContext.Scope.SubScopes.AddRange(moveScopes);
84
85      return base.InstrumentedApply();
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.