Skip to content

Commit

Permalink
Sparsification attempt lava-nc#1: DistProxy with sign inversion and m…
Browse files Browse the repository at this point in the history
…ax cut-off

Signed-off-by: Risbud, Sumedh <sumedh.risbud@intel.com>
  • Loading branch information
srrisbud committed May 4, 2023
1 parent d7925ec commit 5309540
Show file tree
Hide file tree
Showing 4 changed files with 642 additions and 30 deletions.
79 changes: 79 additions & 0 deletions src/lava/lib/optimization/apps/vrp/quality_sparsity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import numpy as np
import networkx as ntx
import argparse
from lava.lib.optimization.apps.vrp.problems import VRP
from lava.lib.optimization.apps.vrp.solver import VRPSolver, VRPConfig, \
CoreSolver


def main(j=0):
max_dist_cutoff_fraction_list = np.around(np.geomspace(1.0, 0.1,
15), 2).tolist()
max_dist_cutoff_fraction_list.reverse()
np.random.seed(42313)
max_dist_cutoff_fraction_list = [0.0, 0.1, 0.12, 0.14, 0.16, 0.19,
0.23, 0.27, 0.32, 0.37, 0.44, 0.52]
dist_sparsity_list = []
dist_proxy_sparsity_list = []
total_cost_list = []
frac_wp_clustered_list = []
print(f"Loading vrp_instance_{j}.dat")
all_coords = np.loadtxt(f"vrp_instance_{j}.dat")
v_c = [tuple(coords) for coords in all_coords[:10, :].tolist()]
w_c = [tuple(coords) for coords in all_coords[10:, :].tolist()]
vrp_instance = VRP(node_coords=w_c, vehicle_coords=v_c)
solver = VRPSolver(vrp=vrp_instance)
print(f"Iterating over cutoff fractions\n")
for cutoff_factor in max_dist_cutoff_fraction_list:
scfg = VRPConfig(backend="Loihi2",
core_solver=CoreSolver.LAVA_QUBO,
max_dist_cutoff_fraction=cutoff_factor,
hyperparameters={},
target_cost=-1000000,
timeout=10000,
probe_time=False,
log_level=40)
try:
clusters, routes = solver.solve(scfg=scfg)
except ValueError:
routes = dict(
zip(
range(1, vrp_instance.num_vehicles + 1),
[[-1]] * vrp_instance.num_vehicles
)
)
dist_sparsity_list.append(solver.dist_sparsity)
dist_proxy_sparsity_list.append(solver.dist_proxy_sparsity)
flat_waypoint_list = []
total_cost = 0
for route in routes.values():
flat_waypoint_list.extend(route)
try:
route_cost = ntx.path_weight(
solver.problem.problem_graph, route, weight="cost")
except ntx.exception.NetworkXNoPath:
route_cost = -1
total_cost += route_cost
flat_waypoint_list.sort()
frac_wp_clusered = np.sum(np.in1d(np.arange(11, 111),
flat_waypoint_list)) / 100
frac_wp_clustered_list.append(frac_wp_clusered)
total_cost_list.append(total_cost)

np.savetxt(f"problem_{j}_dist_sp.dat",
np.array(dist_sparsity_list), fmt="%.3f")
np.savetxt(f"problem_{j}_distpr_sp.dat",
np.array(dist_proxy_sparsity_list), fmt="%.3f")
np.savetxt(f"problem_{j}_total_cost.dat",
np.array(total_cost_list), fmt="%.3f")
np.savetxt(f"problem_{j}_frac_wp_clustered.dat",
np.array(frac_wp_clustered_list), fmt="%.3f")


if __name__ == '__main__':
parser = argparse.ArgumentParser(prog="quality_sparsity.py")
parser.add_argument("prob_num", type=int, choices=list(range(15)))
args = parser.parse_args()
print(f"\n------------------\nProblem number: "
f"{args.prob_num}\n------------------\n")
main(j=args.prob_num)
61 changes: 36 additions & 25 deletions src/lava/lib/optimization/apps/vrp/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,16 @@ class VRPConfig(SolverConfig):
"""

core_solver: CoreSolver = CoreSolver.VRPY_CPU
max_dist_cutoff_fraction: float = 1.0


class VRPSolver:
"""Solver for vehicle routing problems.
"""
def __init__(self, vrp: VRP):
self.problem = vrp
self.dist_sparsity = 0.
self.dist_proxy_sparsity = 0.

def solve(self, scfg: VRPConfig = VRPConfig()) -> \
Tuple[npty.NDArray, Dict[int, List[int]]]:
Expand Down Expand Up @@ -127,9 +130,11 @@ def _prepare_graph_for_vrpy(g):
g.add_edge("Source", n, cost=cost_src_to_nod)
g.add_edge(n, "Sink", cost=cost_nod_to_snk)
return g
vrp = VehicleRoutingProblem(self.problem.problem_graph)
vrpy_is_installed = not hasattr(vrp, "vrpy_not_installed")
if scfg.core_solver == CoreSolver.VRPY_CPU and vrpy_is_installed:
if scfg.core_solver == CoreSolver.VRPY_CPU:
vrp = VehicleRoutingProblem(self.problem.problem_graph)
vrpy_is_installed = not hasattr(vrp, "vrpy_not_installed")
if not vrpy_is_installed:
raise ImportError("VRPy is not installed.")
# 1. Prepare problem for VRPy
graph_to_solve = self.problem.problem_graph.copy()
graph_to_solve = _prepare_graph_for_vrpy(graph_to_solve)
Expand Down Expand Up @@ -162,17 +167,22 @@ def _prepare_graph_for_vrpy(g):
self.problem.node_coords
# number of binary variables = total_num_nodes * num_clusters
mat_size = len(node_list_for_clustering) * self.problem.num_vehicles
Q_clust = QMatrixVRP(node_list_for_clustering,
num_vehicles=self.problem.num_vehicles,
problem_type=ProblemType.CLUSTER,
mat_size_for_random=mat_size,
lamda_dist=1,
lamda_wypts=100,
lamda_vhcles=100,
lamda_cnstrt=1,
fixed_pt=True,
fixed_pt_range=(-128, 127),
profile_mat_gen=False).matrix.astype(int)
q_clust_obj = QMatrixVRP(
node_list_for_clustering,
num_vehicles=self.problem.num_vehicles,
problem_type=ProblemType.CLUSTER,
mat_size_for_random=mat_size,
lamda_dist=1,
lamda_wypts=100,
lamda_vhcles=100,
lamda_cnstrt=1,
fixed_pt=True,
fixed_pt_range=(-128, 127),
max_dist_cutoff_fraction=scfg.max_dist_cutoff_fraction,
profile_mat_gen=False)
Q_clust = q_clust_obj.matrix.astype(int)
self.dist_sparsity = q_clust_obj.dist_sparsity
self.dist_proxy_sparsity = q_clust_obj.dist_proxy_sparsity
# 2. Call Lava QUBO solvers
prob = QUBO(q=Q_clust)
solver = OptimizationSolver(problem=prob)
Expand Down Expand Up @@ -203,17 +213,18 @@ def _prepare_graph_for_vrpy(g):
node_idxs[0] >= self.problem.num_vehicles]
nodes_to_pass = np.array(node_list_for_clustering)[node_idxs, :]
nodes_to_pass = [tuple(node) for node in nodes_to_pass.tolist()]
Q_VRP = QMatrixVRP(nodes_to_pass,
num_vehicles=1,
problem_type=ProblemType.TSP,
mat_size_for_random=matsize,
lamda_dist=1,
lamda_wypts=1,
lamda_vhcles=1,
lamda_cnstrt=100,
fixed_pt=True,
fixed_pt_range=(-128, 127),
profile_mat_gen=False).matrix.astype(int)
Q_VRP = QMatrixVRP(
nodes_to_pass,
num_vehicles=1,
problem_type=ProblemType.TSP,
mat_size_for_random=matsize,
lamda_dist=1,
lamda_wypts=1,
lamda_vhcles=1,
lamda_cnstrt=100,
fixed_pt=True,
fixed_pt_range=(-128, 127),
profile_mat_gen=False).matrix.astype(int)
tsp = QUBO(q=Q_VRP)
tsp_solver = OptimizationSolver(problem=tsp)
scfg.hyperparameters.update({
Expand Down
Loading

0 comments on commit 5309540

Please sign in to comment.