Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/SingleDepotVRP/CVRP/CVRPProblemInstance.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: 5.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.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("CVRPProblemInstance", "Represents a single depot CVRP instance.")]
38  [StorableClass]
39  public class CVRPProblemInstance: SingleDepotVRPProblemInstance, IHomogenousCapacitatedProblemInstance {
40    protected IValueParameter<DoubleValue> CapacityParameter {
41      get { return (IValueParameter<DoubleValue>)Parameters["Capacity"]; }
42    }
43    protected IValueParameter<DoubleValue> OverloadPenaltyParameter {
44      get { return (IValueParameter<DoubleValue>)Parameters["EvalOverloadPenalty"]; }
45    }
46
47    public DoubleValue 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 IHomogenousCapacitatedOperator).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 CVRPEvaluator();
78      }
79    }
80   
81    [StorableConstructor]
82    protected CVRPProblemInstance(bool deserializing) : base(deserializing) { }
83
84    public CVRPProblemInstance() {
85      Parameters.Add(new ValueParameter<DoubleValue>("Capacity", "The capacity of each vehicle.", new DoubleValue(0)));
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 CVRPProblemInstance(this, cloner);
93    }
94
95    protected CVRPProblemInstance(CVRPProblemInstance 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      CapacityParameter.Value.ValueChanged += new EventHandler(Capacity_ValueChanged);
110      OverloadPenaltyParameter.ValueChanged += new EventHandler(OverloadPenaltyParameter_ValueChanged);
111      OverloadPenaltyParameter.Value.ValueChanged += new EventHandler(OverloadPenalty_ValueChanged);
112    }
113
114    public override void InitializeState() {
115      base.InitializeState();
116
117      currentOverloadPenalty = null;
118    }
119
120    #region Event handlers
121    void CapacityParameter_ValueChanged(object sender, EventArgs e) {
122      CapacityParameter.Value.ValueChanged += new EventHandler(Capacity_ValueChanged);
123      EvalBestKnownSolution();
124    }
125    void Capacity_ValueChanged(object sender, EventArgs e) {
126      EvalBestKnownSolution();
127    }
128    void OverloadPenaltyParameter_ValueChanged(object sender, EventArgs e) {
129      OverloadPenaltyParameter.Value.ValueChanged += new EventHandler(OverloadPenalty_ValueChanged);
130      EvalBestKnownSolution();
131    }
132    void OverloadPenalty_ValueChanged(object sender, EventArgs e) {
133      EvalBestKnownSolution();
134    }
135    #endregion
136  }
137}
Note: See TracBrowser for help on using the repository browser.