Skip to content

Commit

Permalink
Merge pull request #459 from AI4Finance-Foundation/r12d
Browse files Browse the repository at this point in the history
vrptw
  • Loading branch information
zhumingpassional authored Nov 12, 2024
2 parents 90bec47 + fbf8f54 commit 3697adc
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 34 deletions.
2 changes: 0 additions & 2 deletions rlsolver/beta1.py

This file was deleted.

4 changes: 2 additions & 2 deletions rlsolver/methods/VRPTW_algs/ESPPRC2.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
calc_dist_of_path,
obtain_paths_based_on_vehicles,
calc_demands_of_paths,
obtai_var_vals,
obtain_var_vals,
calc_durations_of_paths, )
from Label import Label
from util import (read_data_as_nxdigraph,
Expand Down Expand Up @@ -198,7 +198,7 @@ def opt_arc_bounding_model(duration_lowerbound_dict: dict, duration_to_depot: fl
obj = model.ObjVal if feasible else None

var_name: str = "y"
y_vals = obtai_var_vals(model, var_name) if feasible else None
y_vals = obtain_var_vals(model, var_name) if feasible else None
return y_vals, obj, feasible


Expand Down
6 changes: 3 additions & 3 deletions rlsolver/methods/VRPTW_algs/ESPPRC_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import copy
from typing import List, Tuple, Set, Dict


class Label:
path = []
travel_time = 0
dist = 0


# dominance rule
def dominate(labels: List[Label], path_dict: Dict[int, Label]):
labels_copy = copy.deepcopy(labels)
Expand All @@ -35,8 +37,6 @@ def dominate(labels: List[Label], path_dict: Dict[int, Label]):
return labels, path_dict




# labeling algorithm
def labeling_SPPRC(graph, orig, dest):
# initial Q
Expand Down Expand Up @@ -135,6 +135,6 @@ def main():
print('optimal path : ', opt_path[1].path)
print('optimal path (dist): ', opt_path[1].dist)


if __name__ == '__main__':
main()

6 changes: 3 additions & 3 deletions rlsolver/methods/VRPTW_algs/ESPPRC_demo2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import copy
from typing import List, Tuple, Set, Dict


class Label:
path = []
duration = 0
Expand Down Expand Up @@ -42,6 +43,7 @@ def make_unique(labels) -> List:
res.append(labels[i])
return res


# dominance rule
def EFF(labels2: List[Label]):
labels = Label.make_unique(labels2)
Expand Down Expand Up @@ -75,8 +77,6 @@ def EFF(labels2: List[Label]):
return filtered_labels




# labeling algorithm
def labeling_SPPRC(graph, orig, dest):
# initial Q
Expand Down Expand Up @@ -175,6 +175,6 @@ def main():
print(f'The {i + 1}-th optimal path (time): {opt_label.duration}')
print()


if __name__ == '__main__':
main()

1 change: 0 additions & 1 deletion rlsolver/methods/VRPTW_algs/ESPPRC_demo3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
rlsolver_path = os.path.join(cur_path, '../../../rlsolver')
sys.path.append(os.path.dirname(rlsolver_path))

import numpy as np
import networkx as nx
import copy
from typing import List, Tuple, Union, Dict
Expand Down
1 change: 1 addition & 0 deletions rlsolver/methods/VRPTW_algs/Label.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def dominate(self, another, forward: bool):
return False

@staticmethod
# keep non-dominated labels
def EFF(labels2: List, forward: bool) -> List:
labels = Label.make_unique(labels2)
if len(labels) <= 1:
Expand Down
1 change: 1 addition & 0 deletions rlsolver/methods/VRPTW_algs/Vehicle.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import os

cur_path = os.path.dirname(os.path.abspath(__file__))
rlsolver_path = os.path.join(cur_path, '../../../rlsolver')
sys.path.append(os.path.dirname(rlsolver_path))
Expand Down
6 changes: 3 additions & 3 deletions rlsolver/methods/VRPTW_algs/column_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
calc_demands_of_paths,
calc_durations_of_paths,
obtain_paths_based_on_vehicles,
obtai_var_vals,
obtain_var_vals,
)
from rlsolver.methods.VRPTW_algs.ESPPRC1 import ESPPRC1_unidirectional
from rlsolver.methods.VRPTW_algs.config import Config
Expand Down Expand Up @@ -269,7 +269,7 @@ def run_column_generation():
if max_dist - min_dist <= Config.CHECK_DIFF_THRESHOLD_IN_CG:
break

theta_vals_this_iteration = obtai_var_vals(model, "theta")
theta_vals_this_iteration = obtain_var_vals(model, "theta")
theta_valss.append(theta_vals_this_iteration)
print(f"theta_vals_this_iteration: {theta_vals_this_iteration}")

Expand Down Expand Up @@ -330,7 +330,7 @@ def run_column_generation():
else:
raise ValueError

theta_vals = obtai_var_vals(model, "theta")
theta_vals = obtain_var_vals(model, "theta")
theta_vals_in_set = set(theta_vals)
print(f"theta_vals: {theta_vals}")
print(f"theta_vals_in_set: {theta_vals_in_set}")
Expand Down
23 changes: 12 additions & 11 deletions rlsolver/methods/VRPTW_algs/impact_heuristic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# The impact heuristic is implemented based on the paper "A greedy look-ahead heuristic for the vehicle routing problem with time windows"
import sys
import os

cur_path = os.path.dirname(os.path.abspath(__file__))
rlsolver_path = os.path.join(cur_path, '../../../rlsolver')
sys.path.append(os.path.dirname(rlsolver_path))
Expand All @@ -12,19 +13,19 @@
import networkx as nx

from rlsolver.methods.VRPTW_algs.Customer import (Customer,
)
)
from rlsolver.methods.VRPTW_algs.Vehicle import Vehicle
from rlsolver.methods.VRPTW_algs.util import (read_data,
read_data_as_nxdigraph,
generate_vehicles,
generate_vehicles_and_assign_paths,
obtain_paths_based_on_vehicles,
calc_demands_of_paths,
calc_durations_of_paths,
calc_dists_of_paths,
write_result,
write_result_based_on_vehicles,
)
read_data_as_nxdigraph,
generate_vehicles,
generate_vehicles_and_assign_paths,
obtain_paths_based_on_vehicles,
calc_demands_of_paths,
calc_durations_of_paths,
calc_dists_of_paths,
write_result,
write_result_based_on_vehicles,
)

from rlsolver.methods.VRPTW_algs.config import Config

Expand Down
2 changes: 1 addition & 1 deletion rlsolver/methods/VRPTW_algs/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ def filter_vehicles_based_on_paths(vehicles2: List[Vehicle], paths: List[str], c
# res.append(vehicle)
# return res

def obtai_var_vals(model, var_name: str):
def obtain_var_vals(model, var_name: str):
theta_vals = []
num_vars = model.NumVars
for i in range(num_vars):
Expand Down
16 changes: 8 additions & 8 deletions rlsolver/methods/genetic_algorithm.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import sys
import os

cur_path = os.path.dirname(os.path.abspath(__file__))
rlsolver_path = os.path.join(cur_path, '../../rlsolver')
sys.path.append(os.path.dirname(rlsolver_path))

import random
import os
os.environ['KMP_DUPLICATE_LIB_OK']='True'

os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
import copy
import time
import numpy as np
Expand All @@ -20,13 +22,13 @@
plt = None

from rlsolver.methods.util import (calc_txt_files_with_prefixes,
)
)
from rlsolver.methods.util_read_data import (read_nxgraph,
)
)
from rlsolver.methods.util_obj import (obj_maxcut,
)
)
from rlsolver.methods.util_result import (write_graph_result,
)
)

# constants for tabuSearch
P_iter = 100
Expand Down Expand Up @@ -161,7 +163,6 @@ def tabu_search(initial_solution, graph):
return best_solution, best_score



def cross_over(population, graph):
selected_parents = random.sample(population, num_parents)

Expand Down Expand Up @@ -210,8 +211,7 @@ def genetic_maxcut(graph: nx.Graph(), filename):
print("Genetic Search Complete")



def run_genetic_over_multiple_files(directory_data: str, prefixes: List[str])-> List[List[float]]:
def run_genetic_over_multiple_files(directory_data: str, prefixes: List[str]) -> List[List[float]]:
assert PROBLEM == Problem.maxcut
scoress = []
files = calc_txt_files_with_prefixes(directory_data, prefixes)
Expand Down

0 comments on commit 3697adc

Please sign in to comment.