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

Last change on this file since 3938 was 3938, checked in by svonolfe, 10 years ago

Added CVRP implementation using the Alba encoding (#1039)

File size: 4.1 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      ParameterizeSolutionCreator();
55
56      Initialize();
57    }
58
59    [StorableConstructor]
60    private AlbaPermutationCreator(bool deserializing) : base() { }
61
62    [StorableHook(HookType.AfterDeserialization)]
63    private void Initialize() {
64      CitiesParameter.ValueChanged += new EventHandler(CitiesParameter_ValueChanged);
65      if(CitiesParameter.Value != null)
66        CitiesParameter.Value.ValueChanged += new EventHandler(CitiesValue_ValueChanged);
67    }
68
69    public override IDeepCloneable Clone(Cloner cloner) {
70      AlbaPermutationCreator clone = (AlbaPermutationCreator)base.Clone(cloner);
71      clone.Initialize();
72      return clone;
73    }
74
75    void CitiesParameter_ValueChanged(object sender, EventArgs e) {
76      CitiesParameter.Value.ValueChanged += new EventHandler(CitiesValue_ValueChanged);
77     
78      ParameterizeSolutionCreator();
79    }
80
81    void CitiesValue_ValueChanged(object sender, EventArgs e) {
82      ParameterizeSolutionCreator();
83    }
84
85    private void ParameterizeSolutionCreator() {
86      PermutationCreatorParameter.Value.LengthParameter.Value = CitiesParameter.Value;
87      PermutationCreatorParameter.Value.PermutationTypeParameter.Value = new PermutationType(PermutationTypes.RelativeUndirected);
88    }
89
90    public override IOperation Apply() {
91      PermutationCreatorParameter.Value.LengthParameter.Value = new IntValue(CitiesParameter.Value.Value + VehiclesParameter.ActualValue.Value - 1);
92
93      IPermutationCreator creator = PermutationCreatorParameter.ActualValue;
94      IAtomicOperation op = this.ExecutionContext.CreateOperation(creator);
95      op.Operator.Execute((IExecutionContext)op);
96
97      Permutation permutation = ExecutionContext.Scope.Variables["Permutation"].Value as Permutation;
98      ExecutionContext.Scope.Variables.Remove("Permutation");
99
100      VRPSolutionParameter.ActualValue = new AlbaEncoding(permutation, CitiesParameter.ActualValue.Value);
101
102      return base.Apply();
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.