Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Alba/Creators/AlbaPermutationCreator.cs @ 4177

Last change on this file since 4177 was 4177, checked in by svonolfe, 12 years ago

Refactored VRP in preparation for the code review (#1039)

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
32  [Item("AlbaPermutationCreator", "An operator which creates a new Alba VRP representation.")]
33  [StorableClass]
34  public sealed class AlbaPermutationCreator : VRPCreator, IStochasticOperator {
35    public IValueLookupParameter<IPermutationCreator> PermutationCreatorParameter {
36      get { return (IValueLookupParameter<IPermutationCreator>)Parameters["PermutationCreatorParameter"]; }
37    }
38
39    #region IStochasticOperator Members
40    public ILookupParameter<IRandom> RandomParameter {
41      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
42    }
43    #endregion
44
45    public AlbaPermutationCreator()
46      : base() {
47      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator."));
48      Parameters.Add(new ValueLookupParameter<IPermutationCreator>("PermutationCreatorParameter", "The permutation creator.", new RandomPermutationCreator()));
49
50      Initialize();
51      ParameterizeSolutionCreator();
52    }
53
54    [StorableConstructor]
55    private AlbaPermutationCreator(bool deserializing) : base() { }
56
57    [StorableHook(HookType.AfterDeserialization)]
58    private void Initialize() {
59      CitiesParameter.ValueChanged += new EventHandler(CitiesParameter_ValueChanged);
60      if (CitiesParameter.Value != null)
61        CitiesParameter.Value.ValueChanged += new EventHandler(CitiesValue_ValueChanged);
62    }
63
64    public override IDeepCloneable Clone(Cloner cloner) {
65      AlbaPermutationCreator clone = (AlbaPermutationCreator)base.Clone(cloner);
66      clone.Initialize();
67      return clone;
68    }
69
70    void CitiesParameter_ValueChanged(object sender, EventArgs e) {
71      CitiesParameter.Value.ValueChanged += new EventHandler(CitiesValue_ValueChanged);
72      ParameterizeSolutionCreator();
73    }
74
75    void CitiesValue_ValueChanged(object sender, EventArgs e) {
76      ParameterizeSolutionCreator();
77    }
78
79    private void ParameterizeSolutionCreator() {
80      PermutationCreatorParameter.Value.PermutationTypeParameter.Value = new PermutationType(PermutationTypes.RelativeUndirected);
81    }
82
83    public override IOperation Apply() {
84      PermutationCreatorParameter.Value.LengthParameter.Value = new IntValue(CitiesParameter.Value.Value + VehiclesParameter.ActualValue.Value - 1);
85
86      IPermutationCreator creator = PermutationCreatorParameter.ActualValue;
87      IAtomicOperation op = this.ExecutionContext.CreateOperation(creator);
88      op.Operator.Execute((IExecutionContext)op);
89
90      Permutation permutation = ExecutionContext.Scope.Variables["Permutation"].Value as Permutation;
91      ExecutionContext.Scope.Variables.Remove("Permutation");
92
93      VRPSolutionParameter.ActualValue = new AlbaEncoding(permutation, CitiesParameter.ActualValue.Value);
94
95      return base.Apply();
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.