@@ -28,6 +28,7 @@ using JuMP:
2828 objective_value,
2929 set_silent,
3030 dual
31+ using Plots: Plots, plot, plot!, scatter!, annotate!, text
3132using Printf: @printf
3233using Random: Random, AbstractRNG, MersenneTwister
3334using SparseArrays: sparse
@@ -72,6 +73,7 @@ Note that computing solutions can be time-consuming, especially for large instan
7273You can also use instead `compact_mip` or `compact_linearized_mip` as the algorithm to compute solutions.
7374If you want to provide a custom algorithm to compute solutions, you can pass it as the `algorithm` keyword argument.
7475If `algorithm` takes keyword arguments, you can pass them as well directly in `kwargs...`.
76+ If `store_city=false`, the coordinates and unnecessary information about instances will not be stored in the dataset.
7577"""
7678function Utils. generate_dataset (
7779 benchmark:: StochasticVehicleSchedulingBenchmark ,
@@ -80,11 +82,14 @@ function Utils.generate_dataset(
8082 seed= nothing ,
8183 rng= MersenneTwister (0 ),
8284 algorithm= column_generation_algorithm,
85+ store_city= true ,
8386 kwargs... ,
8487)
8588 (; nb_tasks, nb_scenarios) = benchmark
8689 Random. seed! (rng, seed)
87- instances = [Instance (; nb_tasks, nb_scenarios, rng= rng) for _ in 1 : dataset_size]
90+ instances = [
91+ Instance (; nb_tasks, nb_scenarios, rng, store_city) for _ in 1 : dataset_size
92+ ]
8893 features = get_features .(instances)
8994 if compute_solutions
9095 solutions = [algorithm (instance; kwargs... ) for instance in instances]
@@ -114,8 +119,96 @@ function Utils.generate_statistical_model(bench::StochasticVehicleSchedulingBenc
114119 return Chain (Dense (20 => 1 ; bias= false ), vec)
115120end
116121
122+ function plot_instance (
123+ :: StochasticVehicleSchedulingBenchmark ,
124+ sample:: DataSample{<:Instance{City}} ;
125+ color_scheme= :lightrainbow ,
126+ kwargs... ,
127+ )
128+ (; tasks, district_width, width) = sample. instance. city
129+ ticks = 0 : district_width: width
130+ max_time = maximum (t. end_time for t in sample. instance. city. tasks[1 : (end - 1 )])
131+ pp = floor (Int, max (max_time))
132+ palette = Plots. palette (color_scheme, pp)
133+ fig = plot (;
134+ xlabel= " x" ,
135+ ylabel= " y" ,
136+ legend= false ,
137+ gridlinewidth= 3 ,
138+ aspect_ratio= :equal ,
139+ size= (500 , 500 ),
140+ xticks= ticks,
141+ yticks= ticks,
142+ xlims= (- 1 , width + 1 ),
143+ ylims= (- 1 , width + 1 ),
144+ )
145+ for (i_task, task) in enumerate (tasks[1 : (end - 1 )])
146+ (; start_point, end_point) = task
147+ points = [(start_point. x, start_point. y), (end_point. x, end_point. y)]
148+ plot! (fig, points; color= :black )
149+ scatter! (
150+ fig,
151+ points[1 ];
152+ markersize= 10 ,
153+ marker= :rect ,
154+ color= palette[max (floor (Int, task. start_time), 1 )],
155+ )
156+ scatter! (
157+ fig,
158+ points[2 ];
159+ markersize= 10 ,
160+ marker= :rect ,
161+ color= palette[max (floor (Int, task. end_time), 1 )],
162+ )
163+ annotate! (fig, (points[1 ]. .. , text (" $(i_task- 1 ) " , 10 )))
164+ end
165+ p2 = Plots. heatmap (
166+ rand (2 , 2 ); clims= (0 , pp), framestyle= :none , c= palette, cbar= true , lims= (- 1 , - 1 )
167+ )
168+ l = Plots. @layout [a{0.99 w} b]
169+ return plot (fig, p2; layout= l)
170+ end
171+
172+ function plot_solution (
173+ :: StochasticVehicleSchedulingBenchmark , sample:: DataSample{<:Instance{City}} ; kwargs...
174+ )
175+ (; tasks, district_width, width) = sample. instance. city
176+ ticks = 0 : district_width: width
177+ path_list = compute_path_list (sample. y_true)
178+ fig = plot (;
179+ xlabel= " x" ,
180+ ylabel= " y" ,
181+ legend= false ,
182+ gridlinewidth= 3 ,
183+ aspect_ratio= :equal ,
184+ size= (500 , 500 ),
185+ xticks= ticks,
186+ yticks= ticks,
187+ xlims= (- 1 , width + 1 ),
188+ ylims= (- 1 , width + 1 ),
189+ )
190+ for path in path_list
191+ X = Float64[]
192+ Y = Float64[]
193+ (; start_point, end_point) = tasks[path[1 ]]
194+ (; x, y) = end_point
195+ push! (X, x)
196+ push! (Y, y)
197+ for task in path[2 : end ]
198+ (; start_point, end_point) = tasks[task]
199+ push! (X, start_point. x)
200+ push! (Y, start_point. y)
201+ push! (X, end_point. x)
202+ push! (Y, end_point. y)
203+ end
204+ plot! (fig, X, Y; marker= :circle )
205+ end
206+ return fig
207+ end
208+
117209export StochasticVehicleSchedulingBenchmark
118210export generate_dataset, generate_maximizer, generate_statistical_model
211+ export plot_instance, plot_solution
119212export compact_linearized_mip,
120213 compact_mip, column_generation_algorithm, evaluate_solution, is_feasible
121214
0 commit comments