-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
1684 lines (1441 loc) · 76 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from collections import defaultdict
import threading
import time
import queue
import colorsys # Import colorsys for color generation
import random # If needed for other random functionalities
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s:%(message)s')
import tkinter as tk
from tkinter import simpledialog, messagebox, filedialog
from tkinter import ttk
from PIL import Image, ImageTk
import json
import os
from math import sqrt
from gurobipy import *
import random # Added for random container generation
import ast
# Define data classes
class Node:
def __init__(self, node_id, x, y, node_type='terminal'):
self.id = node_id
self.x = x # Pixel x-coordinate on the map
self.y = y # Pixel y-coordinate on the map
self.type = node_type # 'depot', 'dummy', or 'terminal'
class Container:
def __init__(self, container_id, size, release_date, opening_date, closing_date, origin, destination, container_type):
self.id = container_id
self.size = size
self.release_date = release_date
self.opening_date = opening_date
self.closing_date = closing_date
self.origin = origin
self.destination = destination
self.type = container_type # 'I' or 'E'
class Barge:
def __init__(self, barge_id, capacity, fixed_cost, origin):
self.id = barge_id
self.capacity = capacity
self.fixed_cost = fixed_cost
self.origin = origin # Origin depot node ID
class Truck:
def __init__(self, cost_per_container):
self.cost_per_container = cost_per_container
class Arc:
def __init__(self, origin, destination, travel_time):
self.origin = origin # Origin node ID
self.destination = destination # Destination node ID
self.travel_time = travel_time # Tij: Travel time in minutes between origin and destination
# Main Application Class
class NetworkCreatorApp:
def __init__(self, master):
self.master = master
self.master.title("Interactive Network Creator with Optimizer (Pixel Coordinates)")
# Initialize data storage
self.nodes = {}
self.containers = {}
self.barges = {}
self.trucks = {}
self.depot_to_dummy = {}
self.next_node_id = 0
self.next_container_id = 1
self.next_barge_id = 1
self.next_truck_id = 11 # Start from 11 since trucks are predefined with IDs 9 and 10
# Predefine trucks
self.predefine_trucks()
# Define a list of distinct colors
self.color_list = [
'red', 'green', 'blue', 'orange', 'purple',
'cyan', 'magenta', 'yellow', 'brown', 'pink',
'lime', 'teal', 'lavender', 'maroon', 'navy',
'olive', 'coral', 'grey', 'black', 'gold'
]
# Initialize a dictionary to map barge IDs to colors
self.barge_colors = {}
# Create the main frames
self.create_main_frames()
# Load map
self.load_map()
# Create menus and buttons
self.create_menus()
self.create_buttons()
# Bind click event
self.canvas.bind("<Button-1>", self.on_canvas_click)
def create_main_frames(self):
"""
Creates the main frames for the canvas and the button panel.
"""
# Frame for the map and its scrollbars
self.map_frame = tk.Frame(self.master)
self.map_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# Vertical scrollbar
self.v_scrollbar = tk.Scrollbar(self.map_frame, orient=tk.VERTICAL)
self.v_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# Horizontal scrollbar
self.h_scrollbar = tk.Scrollbar(self.map_frame, orient=tk.HORIZONTAL)
self.h_scrollbar.pack(side=tk.BOTTOM, fill=tk.X)
# Canvas
self.canvas = tk.Canvas(
self.map_frame,
bg="white",
yscrollcommand=self.v_scrollbar.set,
xscrollcommand=self.h_scrollbar.set,
scrollregion=(0, 0, 1000, 1000) # Initial scroll region; will be updated when image is loaded
)
self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# Configure scrollbars
self.v_scrollbar.config(command=self.canvas.yview)
self.h_scrollbar.config(command=self.canvas.xview)
# Frame for buttons on the right
self.button_frame = tk.Frame(self.master, padx=10, pady=10)
self.button_frame.pack(side=tk.RIGHT, fill=tk.Y)
def predefine_trucks(self):
# Predefine Truck 1
truck1 = Truck(cost_per_container=150) # Example values
self.trucks[9] = truck1
# Predefine Truck 2
truck2 = Truck(cost_per_container=200) # Example values
self.trucks[10] = truck2
messagebox.showinfo("Predefined Trucks", "Two trucks (ID 9 and ID 10) have been initialized.")
def load_map(self):
# Prompt user to select a PNG map
map_path = filedialog.askopenfilename(title="Select Map Image", filetypes=[("PNG Files", "*.png")])
if not map_path:
messagebox.showerror("Error", "No map selected. Exiting.")
self.master.destroy()
return
# Load the image
self.map_image = Image.open(map_path)
self.map_photo = ImageTk.PhotoImage(self.map_image)
# Update the scroll region to the size of the image
self.canvas.config(scrollregion=(0, 0, self.map_photo.width(), self.map_photo.height()))
# Display the image on the canvas
self.canvas.create_image(0, 0, anchor=tk.NW, image=self.map_photo)
def create_menus(self):
menubar = tk.Menu(self.master)
self.master.config(menu=menubar)
# File menu
file_menu = tk.Menu(menubar, tearoff=0)
file_menu.add_command(label="Save Network", command=self.save_network)
file_menu.add_command(label="Load Network", command=self.load_network)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=self.master.quit)
menubar.add_cascade(label="File", menu=file_menu)
# Help menu
help_menu = tk.Menu(menubar, tearoff=0)
help_menu.add_command(label="About", command=self.show_about)
menubar.add_cascade(label="Help", menu=help_menu)
def create_buttons(self):
# Frame for buttons
button_frame = tk.Frame(self.master)
button_frame.pack(side=tk.RIGHT, fill=tk.Y, padx=10, pady=10)
# Button to add containers
add_container_btn = tk.Button(button_frame, text="Add Containers", command=self.add_container)
add_container_btn.pack(pady=5, fill=tk.X)
# Button to generate random containers
generate_random_containers_btn = tk.Button(button_frame, text="Generate Random Containers", command=self.generate_random_containers)
generate_random_containers_btn.pack(pady=5, fill=tk.X) # New button
# Button to add barge
add_barge_btn = tk.Button(button_frame, text="Add Barge", command=self.add_barge)
add_barge_btn.pack(pady=5, fill=tk.X)
# Remove or disable the "Add Truck" button since trucks are predefined
# Option 1: Remove the button
# pass # No button added for adding trucks
# Button to view data
view_data_btn = tk.Button(button_frame, text="View Data", command=self.view_data)
view_data_btn.pack(pady=5, fill=tk.X)
# Button to solve the optimization model
solve_model_btn = tk.Button(button_frame, text="Solve Model", command=self.solve_model)
solve_model_btn.pack(pady=20, fill=tk.X)
def on_canvas_click(self, event):
# Get the absolute coordinates accounting for scrollbars
x = self.canvas.canvasx(event.x)
y = self.canvas.canvasy(event.y)
# Continue with adding the node using the absolute coordinates
# Prompt user for node type
node_type = simpledialog.askstring("Node Type", "Enter node type ('depot' or 'terminal'):")
if node_type not in ['depot', 'terminal']:
messagebox.showerror("Invalid Type", "Node type must be 'depot' or 'terminal'.")
return
node_id = self.next_node_id
self.next_node_id += 1
node = Node(node_id, x, y, node_type)
self.nodes[node_id] = node
self.draw_node(node)
messagebox.showinfo("Node Added", f"Node {node_id} added as {node_type}.")
if node_type == 'depot':
# Automatically create a dummy depot
dummy_node_id = self.next_node_id
self.next_node_id += 1
# Position the dummy depot slightly offset from the real depot for visibility
dummy_x = x + 10 # Offset by 20 pixels; adjust as needed
dummy_y = y + 10
dummy_node = Node(dummy_node_id, dummy_x, dummy_y, 'depot_arr')
self.nodes[dummy_node_id] = dummy_node
self.depot_to_dummy[node_id] = dummy_node_id
self.draw_node(dummy_node)
messagebox.showinfo("Dummy Depot Added", f"Dummy Depot {dummy_node_id} added for Depot {node_id}.")
def draw_node(self, node):
# Define colors based on node type
if node.type == 'depot':
color = 'blue'
elif node.type == 'depot_arr':
color = 'orange' # Different color for dummy depots
else:
color = 'green'
radius = 5
self.canvas.create_oval(node.x - radius, node.y - radius, node.x + radius, node.y + radius, fill=color, outline='black')
self.canvas.create_text(node.x, node.y - 10, text=str(node.id), fill='black', font=('Arial', 10, 'bold'))
def get_node_ids_by_type(self, node_type):
"""
Returns a list of node IDs filtered by the specified node type.
Args:
node_type (str): 'depot', 'dummy', or 'terminal'
Returns:
List of node IDs matching the type.
"""
return [str(node.id) for node in self.nodes.values() if node.type == node_type]
import tkinter as tk
from tkinter import messagebox
import ast
import json
def add_container(self):
if not any(self.nodes.values()):
messagebox.showerror("No Nodes", "Please add nodes before adding containers.")
return
# Create a new window for container input
container_window = tk.Toplevel(self.master)
container_window.title("Add Containers")
container_window.geometry("900x600") # Adjust size as needed
# Create a main frame to hold canvas and scrollbar
main_frame = tk.Frame(container_window)
main_frame.pack(fill='both', expand=True)
# Create a canvas and a vertical scrollbar for the container inputs
canvas = tk.Canvas(main_frame, borderwidth=0, background="#f0f0f0")
scrollbar = tk.Scrollbar(main_frame, orient="vertical", command=canvas.yview)
scrollable_frame = tk.Frame(canvas, background="#f0f0f0")
scrollable_frame.bind(
"<Configure>",
lambda e: canvas.configure(
scrollregion=canvas.bbox("all")
)
)
canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
canvas.configure(yscrollcommand=scrollbar.set)
canvas.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
# Initialize a list to keep track of container entries
container_entries = []
# Define table headers
headers = ["Size (2=20ft, 4=40ft)", "Release Date", "Opening Date", "Closing Date",
"Origin Node ID", "Destination Node ID", "Type", "Action"]
for col, header in enumerate(headers):
tk.Label(scrollable_frame, text=header, borderwidth=1, relief="solid", bg="#d3d3d3").grid(row=0, column=col,
padx=1, pady=1,
sticky='nsew')
# Function to add a new container input row
def add_container_row(data=None):
row = len(container_entries) + 1 # Start from row 1 as row 0 has headers
# Size
size_entry = tk.Entry(scrollable_frame)
size_entry.grid(row=row, column=0, padx=1, pady=1, sticky='nsew')
# Release Date
release_entry = tk.Entry(scrollable_frame)
release_entry.grid(row=row, column=1, padx=1, pady=1, sticky='nsew')
# Opening Date
opening_entry = tk.Entry(scrollable_frame)
opening_entry.grid(row=row, column=2, padx=1, pady=1, sticky='nsew')
# Closing Date
closing_entry = tk.Entry(scrollable_frame)
closing_entry.grid(row=row, column=3, padx=1, pady=1, sticky='nsew')
# Origin node
origin_var = tk.StringVar(scrollable_frame)
origin_menu = tk.OptionMenu(scrollable_frame, origin_var,
*self.get_node_ids_by_type('depot') + self.get_node_ids_by_type('depot_arr'))
origin_menu.grid(row=row, column=4, padx=1, pady=1, sticky='nsew')
# Destination node
destination_var = tk.StringVar(scrollable_frame)
destination_menu = tk.OptionMenu(scrollable_frame, destination_var,
*self.get_node_ids_by_type('terminal') + self.get_node_ids_by_type(
'depot_arr'))
destination_menu.grid(row=row, column=5, padx=1, pady=1, sticky='nsew')
# Container type
type_var = tk.StringVar(scrollable_frame)
type_menu = tk.OptionMenu(scrollable_frame, type_var, 'E', 'I')
type_menu.grid(row=row, column=6, padx=1, pady=1, sticky='nsew')
type_var.set('E') # Set default type
# Action (Delete button)
delete_btn = tk.Button(scrollable_frame, text="Delete", command=lambda: delete_container_row(row))
delete_btn.grid(row=row, column=7, padx=1, pady=1, sticky='nsew')
# Define the on_type_change function
def on_type_change(*args):
container_type = type_var.get()
# Update origin and destination menus based on container type
if container_type == 'E':
# Export: Origin = depot or dummy, Destination = terminal
origin_menu['menu'].delete(0, 'end')
for depot_id in self.get_node_ids_by_type('depot') + self.get_node_ids_by_type('depot_arr'):
origin_menu['menu'].add_command(label=depot_id,
command=lambda value=depot_id: origin_var.set(value))
destination_menu['menu'].delete(0, 'end')
for terminal_id in self.get_node_ids_by_type('terminal'):
destination_menu['menu'].add_command(label=terminal_id,
command=lambda value=terminal_id: destination_var.set(
value))
elif container_type == 'I':
# Import: Origin = terminal, Destination = dummy depots
origin_menu['menu'].delete(0, 'end')
for terminal_id in self.get_node_ids_by_type('terminal'):
origin_menu['menu'].add_command(label=terminal_id,
command=lambda value=terminal_id: origin_var.set(value))
destination_menu['menu'].delete(0, 'end')
for dummy_id in self.get_node_ids_by_type('depot_arr'):
destination_menu['menu'].add_command(label=dummy_id,
command=lambda value=dummy_id: destination_var.set(value))
# Bind the type_var to the on_type_change function
type_var.trace('w', on_type_change)
# Initialize menus based on default type 'E'
on_type_change()
# If data is provided, populate the fields
if data:
try:
size_entry.insert(0, data['size'])
release_entry.insert(0, data['release_date'])
opening_entry.insert(0, data['opening_date'])
closing_entry.insert(0, data['closing_date'])
origin_var.set(str(data['origin_node_id'])) # Ensure it's a string for the GUI
destination_var.set(str(data['destination_node_id'])) # Ensure it's a string for the GUI
type_var.set(data['type'])
on_type_change()
except KeyError as e:
messagebox.showerror("Data Error", f"Missing key in data: {e}")
# Append the entries to the list
container_entries.append({
'size': size_entry,
'release': release_entry,
'opening': opening_entry,
'closing': closing_entry,
'origin': origin_var,
'destination': destination_var,
'type': type_var
})
# Function to delete a container row
def delete_container_row(row):
# Remove the widgets from the grid
for widget in scrollable_frame.grid_slaves(row=row):
widget.grid_forget()
# Optionally, remove the entry from container_entries
# This requires tracking row numbers more carefully
# For simplicity, we're not implementing it here
# Function to handle bulk addition from list of tuples
def add_containers_from_list():
input_window = tk.Toplevel(container_window)
input_window.title("Add Containers from List")
input_window.geometry("500x400")
tk.Label(input_window, text="Enter list of container tuples:", wraplength=480).pack(pady=10)
text_input = tk.Text(input_window, wrap='word')
text_input.pack(padx=10, pady=10, fill='both', expand=True)
def submit_list():
input_text = text_input.get("1.0", tk.END).strip()
if not input_text:
messagebox.showerror("Input Error", "Please enter a list of tuples.")
return
try:
# Safely evaluate the list of tuples
data_list = ast.literal_eval(input_text)
if not isinstance(data_list, list):
raise ValueError("Input must be a list of tuples.")
for item in data_list:
if not isinstance(item, tuple) or len(item) != 7:
raise ValueError(
"Each item must be a tuple with 7 elements: (size, release_date, opening_date, closing_date, origin_node_id, destination_node_id, type)")
size, release_date, opening_date, closing_date, origin_node_id, destination_node_id, container_type = item
# Validate container_type
if container_type not in ('E', 'I'):
raise ValueError("Container type must be 'E' or 'I'.")
# Prepare data dictionary
data = {
'size': size,
'release_date': release_date,
'opening_date': opening_date,
'closing_date': closing_date,
'origin_node_id': origin_node_id,
'destination_node_id': destination_node_id,
'type': container_type
}
add_container_row(data)
messagebox.showinfo("Success", "Containers added successfully.")
input_window.destroy()
except Exception as e:
messagebox.showerror("Parsing Error", f"An error occurred while parsing the input:\n{e}")
submit_btn = tk.Button(input_window, text="Add Containers", command=submit_list)
submit_btn.pack(pady=10)
# Create a separate frame for the control buttons
control_frame = tk.Frame(container_window)
control_frame.pack(fill='x', padx=10, pady=10)
# Button to add more container rows
add_another_btn = tk.Button(control_frame, text="Add Another Container", command=add_container_row)
add_another_btn.pack(side='left', padx=5)
# Button to add containers from a list of tuples
add_from_list_btn = tk.Button(control_frame, text="Add Containers from List", command=add_containers_from_list)
add_from_list_btn.pack(side='left', padx=5)
# Submit button
submit_btn = tk.Button(control_frame, text="Submit All Containers",
command=lambda: self.submit_containers(container_window, container_entries))
submit_btn.pack(side='right', padx=5)
# Add the first container row
add_container_row()
def submit_containers(self, window, container_entries):
try:
for idx, entry in enumerate(container_entries, start=1):
size = entry['size'].get().strip()
release = entry['release'].get().strip()
opening = entry['opening'].get().strip()
closing = entry['closing'].get().strip()
origin = entry['origin'].get().strip()
destination = entry['destination'].get().strip()
container_type = entry['type'].get().strip()
# Validate size
if not size:
raise ValueError(f"Container {idx}: Size is required.")
size = float(size)
# Validate container type
if container_type not in ['E', 'I']:
raise ValueError(f"Container {idx}: Type must be 'E' or 'I'.")
# Validate origin and destination
origin = int(origin)
destination = int(destination)
if container_type == 'E':
# For export, destination should not be a dummy depot
if self.nodes[origin].type not in ['depot', 'depot_arr'] or self.nodes[destination].type != 'terminal':
raise ValueError(f"Container {idx}: Export containers must originate from a depot/dummy and be destined to a terminal.")
elif container_type == 'I':
# For import, origin should be a terminal and destination a dummy depot
if self.nodes[origin].type != 'terminal' or self.nodes[destination].type != 'depot_arr':
raise ValueError(f"Container {idx}: Import containers must originate from a terminal and be destined to a dummy depot.")
release = None # For import containers, release date is None
# Validate release, opening, and closing dates
if container_type == 'E':
if not release:
raise ValueError(f"Container {idx}: Release date is required for export containers.")
release = int(release)
else:
release = None # Ensure release is None for import containers
if not opening:
raise ValueError(f"Container {idx}: Opening date is required.")
opening = int(opening)
if not closing:
raise ValueError(f"Container {idx}: Closing date is required.")
closing = int(closing)
# Additional validation: Closing date must be after opening date
if closing <= opening:
raise ValueError(f"Container {idx}: Closing date must be after opening date.")
# Create and add the container
container_id = self.next_container_id
self.next_container_id += 1
container = Container(container_id, size, release, opening, closing, origin, destination, container_type)
self.containers[container_id] = container
except ValueError as ve:
messagebox.showerror("Invalid Input", str(ve))
return
messagebox.showinfo("Containers Added", f"{len(container_entries)} container(s) added successfully.")
window.destroy()
def add_barge(self):
# Create a new window for barge input
barge_window = tk.Toplevel(self.master)
barge_window.title("Add Barge")
# Barge attributes
tk.Label(barge_window, text="Capacity:").grid(row=0, column=0, padx=5, pady=5, sticky='e')
capacity_entry = tk.Entry(barge_window)
capacity_entry.grid(row=0, column=1, padx=5, pady=5, sticky='w')
tk.Label(barge_window, text="Fixed Cost:").grid(row=1, column=0, padx=5, pady=5, sticky='e')
fixed_cost_entry = tk.Entry(barge_window)
fixed_cost_entry.grid(row=1, column=1, padx=5, pady=5, sticky='w')
tk.Label(barge_window, text="Origin Depot ID:").grid(row=2, column=0, padx=5, pady=5, sticky='e')
# Origin Depot Selection
depots = self.get_node_ids_by_type('depot') # List of depots as strings
if not depots:
messagebox.showerror("No Depots", "Please add depots before adding a barge.")
barge_window.destroy()
return
origin_var = tk.StringVar(barge_window)
origin_menu = tk.OptionMenu(barge_window, origin_var, *depots)
origin_menu.grid(row=2, column=1, padx=5, pady=5, sticky='w')
origin_var.set(depots[0]) # Set default to first depot
# Submit button
submit_btn = tk.Button(barge_window, text="Add", command=lambda: self.submit_barge(barge_window, capacity_entry.get(), fixed_cost_entry.get(), origin_var.get()))
submit_btn.grid(row=3, column=0, columnspan=2, pady=10)
def submit_barge(self, window, capacity, fixed_cost, origin):
try:
capacity = float(capacity)
fixed_cost = float(fixed_cost)
origin = int(origin)
except ValueError:
messagebox.showerror("Invalid Input", "Please enter valid barge data.")
return
# Ensure the origin is a depot
if self.nodes[origin].type != 'depot':
messagebox.showerror("Invalid Origin", "Barge origin must be a depot.")
return
barge_id = self.next_barge_id
self.next_barge_id += 1
barge = Barge(barge_id, capacity, fixed_cost, origin)
self.barges[barge_id] = barge
messagebox.showinfo("Barge Added", f"Barge {barge_id} added with Origin Depot {origin}.")
window.destroy()
def add_truck(self):
# Trucks are predefined; no addition allowed
messagebox.showinfo("Add Truck", "Only two trucks (ID 1 and ID 2) are allowed. Please use 'Edit Trucks' to modify their details.")
def add_truck_button_disabled(self):
# Optionally, hide or disable the "Add Truck" button
pass # Already handled by not creating the button
def add_truck(self):
# Trucks are predefined; no addition allowed
messagebox.showinfo("Add Truck", "Only two trucks (ID 1 and ID 2) are allowed. Please use 'Edit Trucks' to modify their details.")
def view_data(self):
# Create a new window to display all data
data_window = tk.Toplevel(self.master)
data_window.title("Network Data")
# Create a notebook with tabs
notebook = ttk.Notebook(data_window)
notebook.pack(fill=tk.BOTH, expand=True)
# Nodes Tab
nodes_frame = ttk.Frame(notebook)
notebook.add(nodes_frame, text="Nodes")
nodes_text = tk.Text(nodes_frame, width=80, height=20)
nodes_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
nodes_scroll = tk.Scrollbar(nodes_frame, command=nodes_text.yview)
nodes_scroll.pack(side=tk.RIGHT, fill=tk.Y)
nodes_text.config(yscrollcommand=nodes_scroll.set)
for node in self.nodes.values():
nodes_text.insert(tk.END, f"ID: {node.id}, Type: {node.type}, Pixel Coords: ({node.x}, {node.y})\n")
# Containers Tab
containers_frame = ttk.Frame(notebook)
notebook.add(containers_frame, text="Containers")
containers_text = tk.Text(containers_frame, width=80, height=20)
containers_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
containers_scroll = tk.Scrollbar(containers_frame, command=containers_text.yview)
containers_scroll.pack(side=tk.RIGHT, fill=tk.Y)
containers_text.config(yscrollcommand=containers_scroll.set)
for container in self.containers.values():
containers_text.insert(tk.END, f"ID: {container.id}, Size: {container.size}, Type: {container.type}, "
f"Origin: {container.origin}, Destination: {container.destination}, "
f"Release: {container.release_date}, Opening: {container.opening_date}, "
f"Closing: {container.closing_date}\n")
# Barges Tab
barges_frame = ttk.Frame(notebook)
notebook.add(barges_frame, text="Barges")
barges_text = tk.Text(barges_frame, width=80, height=10)
barges_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
barges_scroll = tk.Scrollbar(barges_frame, command=barges_text.yview)
barges_scroll.pack(side=tk.RIGHT, fill=tk.Y)
barges_text.config(yscrollcommand=barges_scroll.set)
for barge in self.barges.values():
barges_text.insert(tk.END, f"ID: {barge.id}, Capacity: {barge.capacity}, Fixed Cost: {barge.fixed_cost}, Origin Depot: {barge.origin}\n")
# Trucks Tab
trucks_frame = ttk.Frame(notebook)
notebook.add(trucks_frame, text="Trucks")
trucks_text = tk.Text(trucks_frame, width=80, height=10)
trucks_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
trucks_scroll = tk.Scrollbar(trucks_frame, command=trucks_text.yview)
trucks_scroll.pack(side=tk.RIGHT, fill=tk.Y)
trucks_text.config(yscrollcommand=trucks_scroll.set)
for truck in self.trucks.values():
trucks_text.insert(tk.END, f"Cost per Container: {truck.cost_per_container}\n")
def save_network(self):
# Prompt user to select a file to save
save_path = filedialog.asksaveasfilename(defaultextension=".json", filetypes=[("JSON Files", "*.json")])
if not save_path:
return
# Prepare data
data = {
"nodes": {node_id: {"x": node.x, "y": node.y, "type": node.type}
for node_id, node in self.nodes.items()},
"containers": {container_id: {
"size": container.size,
"release_date": container.release_date,
"opening_date": container.opening_date,
"closing_date": container.closing_date,
"origin": container.origin,
"destination": container.destination,
"type": container.type
} for container_id, container in self.containers.items()},
"barges": {barge_id: {
"capacity": barge.capacity,
"fixed_cost": barge.fixed_cost,
"origin": barge.origin # Save origin depot ID
} for barge_id, barge in self.barges.items()},
"trucks": {truck_id: {
"cost_per_container": truck.cost_per_container
} for truck_id, truck in self.trucks.items()},
"depot_to_dummy": self.depot_to_dummy, # Save depot_to_dummy mapping
"next_ids": {
"node": self.next_node_id,
"container": self.next_container_id,
"barge": self.next_barge_id,
"truck": self.next_truck_id
}
}
# Save to JSON
with open(save_path, 'w') as f:
json.dump(data, f, indent=4)
messagebox.showinfo("Saved", f"Network data saved to {save_path}.")
def clear_data(self):
# Clear all data
self.nodes.clear()
self.containers.clear()
self.barges.clear()
self.trucks.clear()
self.depot_to_dummy.clear() # Clear depot_to_dummy mapping
self.next_node_id = 0
self.next_container_id = 1
self.next_barge_id = 1
self.next_truck_id = 11 # Reset to 3 since 1 and 2 are predefined
# Re-predefine the two trucks
self.predefine_trucks()
# Clear canvas (remove all except the map image)
self.canvas.delete("all")
self.canvas.create_image(0, 0, anchor=tk.NW, image=self.map_photo)
def show_about(self):
messagebox.showinfo("About", "Interactive Network Creator with Optimizer\nDeveloped with Tkinter and Gurobi.")
def solve_model(self):
if not self.nodes:
messagebox.showerror("No Nodes", "Please add nodes before solving the model.")
return
if not self.containers:
messagebox.showerror("No Containers", "Please add containers before solving the model.")
return
if not self.barges:
messagebox.showerror("No Barges", "Please add barges before solving the model.")
return
if not self.trucks:
messagebox.showerror("No Trucks", "Please add trucks before solving the model.")
return
# Prompt user for maximum optimization time in seconds
max_time = simpledialog.askinteger(
"Max Optimization Time",
"Enter the maximum optimization time in seconds:",
minvalue=10, # Minimum 10 sec
maxvalue=3600 # Maximum 1 hour
)
if not max_time:
return # User cancelled the dialog
# Create a progress window
progress_window = tk.Toplevel(self.master)
progress_window.title("Solving Model")
progress_window.geometry("400x100")
tk.Label(progress_window, text="Optimization in progress...").pack(pady=10)
progress_bar = ttk.Progressbar(progress_window, orient="horizontal", length=300, mode="determinate")
progress_bar.pack(pady=10)
progress_bar["maximum"] = max_time
progress_bar["value"] = 0
# Event to signal optimization completion
opt_done = threading.Event()
# Dictionary to store optimization results
self.optimization_results = {}
# Define the optimization thread
def optimization_thread():
try:
nodes_dict, arcs_list = self.construct_arcs()
model, variables = self.build_and_solve_model(nodes_dict, arcs_list, max_time)
if model is not None and variables is not None:
self.optimization_results['model'] = model
self.optimization_results['variables'] = variables
except Exception as e:
self.optimization_results['error'] = str(e)
finally:
opt_done.set()
# Start the optimization in a separate thread
thread = threading.Thread(target=optimization_thread)
thread.start()
# Start time tracking
start_time = time.time()
# Function to update the progress bar
def update_progress():
if opt_done.is_set():
progress_bar["value"] = max_time
progress_window.destroy()
error = self.optimization_results.get('error')
if error:
messagebox.showerror("Optimization Error", f"An error occurred:\n{error}")
return
model = self.optimization_results.get('model')
variables = self.optimization_results.get('variables')
if model and variables:
if model.Status == GRB.OPTIMAL:
messagebox.showinfo("Optimization Completed", "An optimal solution was found.")
elif model.Status == GRB.TIME_LIMIT and model.SolCount > 0:
messagebox.showwarning("Time Limit Reached",
"Optimization reached the time limit. A feasible solution was found.")
# Display results regardless of status
self.display_results(model, variables)
self.visualize_routes(variables)
return
else:
elapsed_time = time.time() - start_time
progress_bar["value"] = min(elapsed_time, max_time)
if elapsed_time < max_time:
self.master.after(1000, update_progress) # Update every second
else:
# Time limit reached; Gurobi should have stopped
progress_window.destroy()
# Handle cases where optimization might not have set opt_done yet
if not opt_done.is_set():
messagebox.showwarning("Time Limit Reached", "Optimization reached the maximum time limit.")
opt_done.set()
return
# Initiate the progress bar update loop
update_progress()
def build_and_solve_model(self, nodes, arcs,max_time):
try:
logging.info("Initializing the Gurobi model...")
model = Model("BargeScheduling")
# Big M
M = 2000 # A large constant used in Big M method for conditional constraints
logging.debug(f"Big M value set to {M}")
# Define sets
N = list(nodes.keys()) # Set of all node IDs
C = list(self.containers.keys()) # Set of all container IDs
E = [c.id for c in self.containers.values() if c.type == 'E'] # Export containers
I = [c.id for c in self.containers.values() if c.type == 'I'] # Import containers
K = list(self.barges.keys()) + [9, 10] # Set of barges and trucks (IDs 1 and 2)
KB = list(self.barges.keys()) # Set of barges only
logging.debug(
f"Sets defined - Nodes: {N}, Containers: {C}, Export Containers: {E}, Import Containers: {I}, Vehicles: {K}, Barges: {KB}")
# Define parameters
Wc = {c.id: c.size for c in self.containers.values()} # Wc: Container sizes
Rc = {c.id: c.release_date for c in self.containers.values() if
c.type == 'E'} # Rc: Release dates for export containers
Oc = {c.id: c.opening_date for c in self.containers.values()} # Oc: Opening dates for all containers
Dc = {c.id: c.closing_date for c in self.containers.values()} # Dc: Closing dates for all containers
logging.debug("Parameters defined - Wc, Rc, Oc, Dc")
# Zcj: Indicator if container c is associated with node j
Zcj = {}
for c in self.containers.values():
for j in N:
if c.origin == j:
Zcj[c.id, j] = 1
elif c.destination == j:
Zcj[c.id, j] = 1
else:
Zcj[c.id, j] = 0
logging.debug("Zcj indicators set for container-node associations")
HBk = {k: self.barges[k].fixed_cost for k in self.barges.keys()} # HBk: Fixed costs for each barge
Qk = {k: self.barges[k].capacity for k in self.barges.keys()} # Qk: Capacities for each barge
Or = {k: self.barges[k].origin for k in self.barges.keys()} # Origin for each barge
logging.debug("Barge parameters defined - HBk, Qk, Or")
# depot_to_dummy mapping
depot_to_dummy = self.depot_to_dummy # Assuming it's already populated
logging.debug(f"Depot to Dummy Depot mapping: {depot_to_dummy}")
# Update arcs_list to include only Arc objects
arcs_list = arcs # List of Arc instances
logging.debug(f"Number of arcs constructed: {len(arcs_list)}")
Tij = {(arc.origin, arc.destination): arc.travel_time for arc in arcs} # Tij: Travel times between nodes
logging.debug("Travel times (Tij) between nodes computed")
# Handling time and penalty
L = 20 # Handling time per container in hours (e.g., loading/unloading time)
gamma = 50 # Penalty cost for visiting sea terminals
logging.debug(f"Handling time (L): {L} hours, Penalty (gamma): {gamma}")
# =========================================================================================================================
# Define Decision Variables
# =========================================================================================================================
logging.info("Defining decision variables...")
# f_ck: Binary variable indicating if container c is assigned to vehicle k
f_ck = {}
for c in C:
for k in K:
f_ck[c, k] = model.addVar(vtype=GRB.BINARY, name=f"f_{c}_{k}")
logging.debug(f"Defined f_ck variables for {len(f_ck)} container-vehicle pairs")
# x_ijk: Binary variable indicating if barge k traverses arc (i, j)
x_ijk = {}
for k in KB:
x_ijk[k] = {}
for arc in arcs_list:
i, j, travel_time = arc.origin, arc.destination, arc.travel_time
if i != j:
x_ijk[k][(i, j)] = model.addVar(vtype=GRB.BINARY, name=f"x_{i}_{j}_{k}")
logging.debug(f"Defined x_ijk variables for {len(x_ijk)} barges and their possible arcs")
# p_jk: Continuous variable representing import quantities loaded by barge k at terminal j
# d_jk: Continuous variable representing export quantities unloaded by barge k at terminal j
p_jk = {}
d_jk = {}
for k in KB:
for j in N:
if self.nodes[j].type == 'terminal':
p_jk[j, k] = model.addVar(vtype=GRB.CONTINUOUS, lb=0, name=f"p_{j}_{k}")
d_jk[j, k] = model.addVar(vtype=GRB.CONTINUOUS, lb=0, name=f"d_{j}_{k}")
logging.debug(f"Defined p_jk and d_jk variables for barges at terminal nodes")
# y_ijk: Continuous variable for import containers on arc (i, j) by barge k
# z_ijk: Continuous variable for export containers on arc (i, j) by barge k
y_ijk = {}
z_ijk = {}
for k in KB:
y_ijk[k] = {}
z_ijk[k] = {}
for arc in arcs_list:
i, j, travel_time = arc.origin, arc.destination, arc.travel_time
if i != j:
y_ijk[k][(i, j)] = model.addVar(vtype=GRB.CONTINUOUS, lb=0, name=f"y_{i}_{j}_{k}")
z_ijk[k][(i, j)] = model.addVar(vtype=GRB.CONTINUOUS, lb=0, name=f"z_{i}_{j}_{k}")
logging.debug(f"Defined y_ijk and z_ijk variables for barges on all arcs")
# t_jk: Continuous variable representing the arrival time of barge k at node j
t_jk = {}
for k in KB:
for j in N:
t_jk[j, k] = model.addVar(vtype=GRB.CONTINUOUS, lb=0, name=f"t_{j}_{k}")
logging.debug(f"Defined t_jk variables for barges at terminal nodes")
logging.debug(t_jk)
# =========================================================================================================================
# Define Objective Function
# =========================================================================================================================
logging.info("Defining objective function...")
# Corrected Objective Function
model.setObjective(
quicksum(f_ck[c, k] * 2000 for c in C for k in [9, 10]) + # Truck costs
quicksum(x_ijk[k][(i, j)] * HBk[k] for k in KB for (i, j) in x_ijk[k]) + # Barge fixed costs
quicksum(Tij[(i, j)] * x_ijk[k][(i, j)] for k in KB for (i, j) in x_ijk[k]) + # Barge travel time costs
quicksum(
gamma * x_ijk[k][(i, j)] for k in KB for (i, j) in x_ijk[k] if self.nodes[i].type == "terminal"
), # Penalty for visiting sea terminals
GRB.MINIMIZE
)
logging.debug("Objective function defined")
# =========================================================================================================================
# Define Constraints
# =========================================================================================================================
logging.info("Defining constraints...")
# (1) Each container is allocated to exactly one barge or truck
for c in C:
model.addConstr(
quicksum(f_ck[c, k] for k in K) == 1,
name=f"Assignment_{c}"
)
logging.debug(f"Defined assignment constraints for {len(C)} containers")
# (2) Flow conservation for x_ijk (Barge Routes)
for k in KB:
origin_node = Or[k] # Get the origin node for barge k
try:
destination_node = depot_to_dummy[origin_node] # Map to the corresponding dummy depot node