Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveMoveGenerator.cs

Last change on this file was 17699, checked in by abeham, 4 years ago

#2521: Made encodings non-generic classes (the TEncodedSolution type parameter is not actually used), this will make it considerably easier to port the VRP to the new architecture

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Collections.Generic;
24using System.Globalization;
25using System.Linq;
26using HEAL.Attic;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Operators;
31using HeuristicLab.Parameters;
32
33namespace HeuristicLab.Optimization {
34  [Item("Single-objective MoveGenerator", "Calls the GetNeighbors method of the problem definition to obtain the moves.")]
35  [StorableType("CB37E7D8-EAC3-4061-9D39-20538CD1064D")]
36  internal sealed class SingleObjectiveMoveGenerator<TEncodedSolution> : SingleSuccessorOperator, INeighborBasedOperator<TEncodedSolution>, IMultiMoveGenerator, IStochasticOperator, ISingleObjectiveMoveOperator
37  where TEncodedSolution : class, IEncodedSolution {
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<IEncoding> EncodingParameter {
47      get { return (ILookupParameter<IEncoding>)Parameters["Encoding"]; }
48    }
49
50    public Func<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom, IEnumerable<ISingleObjectiveSolutionContext<TEncodedSolution>>> GetNeighbors { get; set; }
51
52    [StorableConstructor]
53    private SingleObjectiveMoveGenerator(StorableConstructorFlag _) : base(_) { }
54    private SingleObjectiveMoveGenerator(SingleObjectiveMoveGenerator<TEncodedSolution> original, Cloner cloner)
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."));
59      Parameters.Add(new LookupParameter<IEncoding>("Encoding", "An item that holds the problem's encoding."));
60    }
61
62    public override IDeepCloneable Clone(Cloner cloner) {
63      return new SingleObjectiveMoveGenerator<TEncodedSolution>(this, cloner);
64    }
65
66    public override IOperation Apply() {
67      var random = RandomParameter.ActualValue;
68      var sampleSize = SampleSizeParameter.ActualValue.Value;
69      var encoding = EncodingParameter.ActualValue;
70      var solutionContext = ScopeUtil.CreateSolutionContext<TEncodedSolution>(ExecutionContext.Scope, encoding);
71
72      var nbhood = GetNeighbors(solutionContext, random).Take(sampleSize).ToList();
73      var moveScopes = new Scope[nbhood.Count];
74      for (int i = 0; i < moveScopes.Length; i++) {
75        moveScopes[i] = new Scope(i.ToString(CultureInfo.InvariantCulture.NumberFormat));
76        ScopeUtil.CopyEncodedSolutionToScope(moveScopes[i], encoding, nbhood[i].EncodedSolution);
77        ScopeUtil.CopyToScope(moveScopes[i], nbhood[i]);
78      }
79      ExecutionContext.Scope.SubScopes.AddRange(moveScopes);
80
81      return base.Apply();
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.