Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting.Views/3.3/VehicleRoutingProblemView.cs @ 4708

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

Add best known solution import for the VRP (TSPLib format) #1236

File size: 5.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.Windows.Forms;
24using HeuristicLab.MainForm;
25using HeuristicLab.Optimization.Views;
26using HeuristicLab.Core.Views;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
30
31namespace HeuristicLab.Problems.VehicleRouting.Views {
32  [View("VehicleRouting Problem View")]
33  [Content(typeof(VehicleRoutingProblem), true)]
34  public partial class VehicleRoutingProblemView : NamedItemView {
35    public new VehicleRoutingProblem Content {
36      get { return (VehicleRoutingProblem)base.Content; }
37      set { base.Content = value; }
38    }
39
40    public VehicleRoutingProblemView() {
41      InitializeComponent();
42    }
43
44    protected override void DeregisterContentEvents() {
45      Content.CoordinatesParameter.ValueChanged -= new EventHandler(CoordinatesParameter_ValueChanged);
46      Content.BestKnownQualityParameter.ValueChanged -= new EventHandler(BestKnownQualityParameter_ValueChanged);
47      base.DeregisterContentEvents();
48    }
49    protected override void RegisterContentEvents() {
50      base.RegisterContentEvents();
51      Content.CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged);
52      Content.BestKnownQualityParameter.ValueChanged += new EventHandler(BestKnownQualityParameter_ValueChanged);
53    }
54
55    protected override void OnContentChanged() {
56      base.OnContentChanged();
57      if (Content == null) {
58        parameterCollectionView.Content = null;
59        vrpSolutionView.Content = null;
60      } else {
61        parameterCollectionView.Content = ((IParameterizedNamedItem)Content).Parameters;
62        UpdateSolution();
63      }
64    }
65
66    protected override void SetEnabledStateOfControls() {
67      base.SetEnabledStateOfControls();
68      parameterCollectionView.Enabled = Content != null;
69      vrpSolutionView.Enabled = Content != null;
70      importBestButton.Enabled = importButton.Enabled = importButton2.Enabled = importButton3.Enabled = Content != null && !ReadOnly;
71    }
72
73    private void importButton_Click(object sender, EventArgs e) {
74      OpenFileDialog dialog = new OpenFileDialog();
75      dialog.Filter = "Solomon files (*.txt)|*.txt";
76
77      if (dialog.ShowDialog() == DialogResult.OK) {
78        Content.ImportFromSolomon(dialog.FileName);
79      }
80    }
81
82    private void importButton2_Click(object sender, EventArgs e) {
83      OpenFileDialog dialog = new OpenFileDialog();
84      dialog.Filter = "TSPLib files (*.vrp)|*.vrp";
85
86      if (dialog.ShowDialog() == DialogResult.OK) {
87        Content.ImportFromTSPLib(dialog.FileName);
88      }
89    }
90
91    private void importBestButton_Click(object sender, EventArgs e) {
92      OpenFileDialog dialog = new OpenFileDialog();
93      dialog.Filter = "VRP solution files (*.opt)|*.opt";
94
95      if (dialog.ShowDialog() == DialogResult.OK) {
96        Content.ImportSolution(dialog.FileName);
97      }
98    }
99
100    private void importButton3_Click(object sender, EventArgs e) {
101      OpenFileDialog dialog = new OpenFileDialog();
102      dialog.Filter = "ORLib files (*.txt)|*.txt";
103
104      if (dialog.ShowDialog() == DialogResult.OK) {
105        Content.ImportFromORLib(dialog.FileName);
106      }
107    } 
108
109    private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) {
110      vrpSolutionView.Content.Coordinates = Content.Coordinates;
111    }
112
113    private void UpdateSolution() {
114      if (Content.BestKnownSolution == null)
115        vrpSolutionView.Content = new VRPSolution(Content.Coordinates);
116      else {
117        //call evaluator
118        IValueLookupParameter<DoubleMatrix> distMatrix = new ValueLookupParameter<DoubleMatrix>("DistMatrix",
119          Content.DistanceMatrix);
120
121        TourEvaluation eval = VRPEvaluator.Evaluate(
122          Content.BestKnownSolution,
123          Content.Vehicles,
124          Content.DueTime,
125          Content.ServiceTime,
126          Content.ReadyTime,
127          Content.Demand,
128          Content.Capacity,
129          Content.FleetUsageFactorParameter.Value,
130          Content.TimeFactorParameter.Value,
131          Content.DistanceFactorParameter.Value,
132          Content.OverloadPenaltyParameter.Value,
133          Content.TardinessPenaltyParameter.Value,
134          Content.Coordinates,
135          distMatrix,
136          Content.UseDistanceMatrix);
137
138        Content.DistanceMatrix = distMatrix.Value;
139
140        vrpSolutionView.Content = new VRPSolution(Content.Coordinates,
141          Content.BestKnownSolution,
142          new DoubleValue(eval.Quality),
143          new DoubleValue(eval.Distance),
144          new DoubleValue(eval.Overload),
145          new DoubleValue(eval.Tardiness),
146          new DoubleValue(eval.TravelTime),
147          new DoubleValue(eval.VehcilesUtilized),
148          Content.DistanceMatrix,
149          Content.UseDistanceMatrix,
150          Content.ReadyTime,
151          Content.DueTime,
152          Content.ServiceTime);
153      }
154    }
155
156   void BestKnownQualityParameter_ValueChanged(object sender, EventArgs e) {
157      UpdateSolution();
158    }
159  }
160}
Note: See TracBrowser for help on using the repository browser.