Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/MultiDepotVRP/MDCVRP/MDCVRPProblemInstance.cs @ 7852

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

Moved parameters from the ProblemInstance to the Problem (#1177)

File size: 4.8 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.Problems.VehicleRouting.Interfaces;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Core;
29using HeuristicLab.Parameters;
30using HeuristicLab.Data;
31using HeuristicLab.Optimization;
32using HeuristicLab.PluginInfrastructure;
33using HeuristicLab.Problems.VehicleRouting.Variants;
34using HeuristicLab.Common;
35
36namespace HeuristicLab.Problems.VehicleRouting.ProblemInstances {
37  [Item("MDCVRPProblemInstance", "Represents a multi depot CVRP instance.")]
38  [StorableClass]
39  public class MDCVRPProblemInstance: MultiDepotVRPProblemInstance, IHeterogenousCapacitatedProblemInstance {
40    protected IValueParameter<DoubleArray> CapacityParameter {
41      get { return (IValueParameter<DoubleArray>)Parameters["Capacity"]; }
42    }
43    protected IValueParameter<DoubleValue> OverloadPenaltyParameter {
44      get { return (IValueParameter<DoubleValue>)Parameters["EvalOverloadPenalty"]; }
45    }
46
47    public DoubleArray Capacity {
48      get { return CapacityParameter.Value; }
49      set { CapacityParameter.Value = value; }
50    }
51
52    [Storable]
53    private DoubleValue currentOverloadPenalty;
54
55    public DoubleValue OverloadPenalty {
56      get {
57        if (currentOverloadPenalty != null)
58          return currentOverloadPenalty;
59        else
60          return OverloadPenaltyParameter.Value; }
61      set { currentOverloadPenalty = value; }
62    }
63
64    protected override IEnumerable<IOperator> GetOperators() {
65        return base.GetOperators()
66          .Where(o => o is IHeterogenousCapacitatedOperator).Cast<IOperator>();
67    }
68
69    protected override IEnumerable<IOperator> GetAnalyzers() {
70      return ApplicationManager.Manager.GetInstances<ICapacitatedOperator>()
71        .Where(o => o is IAnalyzer)
72        .Cast<IOperator>().Union(base.GetAnalyzers());
73    }
74
75    protected override IVRPEvaluator Evaluator {
76      get {
77        return new MDCVRPEvaluator();
78      }
79    }
80   
81    [StorableConstructor]
82    protected MDCVRPProblemInstance(bool deserializing) : base(deserializing) { }
83
84    public MDCVRPProblemInstance() {
85      Parameters.Add(new ValueParameter<DoubleArray>("Capacity", "The capacity of each vehicle.", new DoubleArray()));
86      Parameters.Add(new ValueParameter<DoubleValue>("EvalOverloadPenalty", "The overload penalty considered in the evaluation.", new DoubleValue(100)));
87
88      AttachEventHandlers();
89    }
90
91    public override IDeepCloneable Clone(Cloner cloner) {
92      return new MDCVRPProblemInstance(this, cloner);
93    }
94
95    protected MDCVRPProblemInstance(MDCVRPProblemInstance original, Cloner cloner)
96      : base(original, cloner) {
97        currentOverloadPenalty = cloner.Clone(original.currentOverloadPenalty) as DoubleValue;
98
99        AttachEventHandlers();
100    }
101
102    [StorableHook(HookType.AfterDeserialization)]
103    private void AfterDeserializationHook() {
104      AttachEventHandlers();
105    }
106
107    private void AttachEventHandlers() {
108      CapacityParameter.ValueChanged += new EventHandler(CapacityParameter_ValueChanged);
109      OverloadPenaltyParameter.ValueChanged += new EventHandler(OverloadPenaltyParameter_ValueChanged);
110      OverloadPenaltyParameter.Value.ValueChanged += new EventHandler(OverloadPenalty_ValueChanged);
111    }
112
113    public override void InitializeState() {
114      base.InitializeState();
115
116      currentOverloadPenalty = null;
117    }
118
119    #region Event handlers
120    void CapacityParameter_ValueChanged(object sender, EventArgs e) {
121      EvalBestKnownSolution();
122    }
123    void OverloadPenaltyParameter_ValueChanged(object sender, EventArgs e) {
124      OverloadPenaltyParameter.Value.ValueChanged += new EventHandler(OverloadPenalty_ValueChanged);
125      EvalBestKnownSolution();
126    }
127    void OverloadPenalty_ValueChanged(object sender, EventArgs e) {
128      EvalBestKnownSolution();
129    }
130    #endregion
131  }
132}
Note: See TracBrowser for help on using the repository browser.