Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3991 was 3991, checked in by svonolfe, 14 years ago

Fixed small issues in the VRP implementation (#1039)

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