good starting point, working pulp

This commit is contained in:
2025-01-15 19:55:48 +01:00
parent 814da4b5f6
commit 3ebe0b7191
2 changed files with 15 additions and 25 deletions

View File

@@ -2,7 +2,6 @@ import yaml, logging
import numpy as np
import pulp as pl
from scipy.optimize import linprog
from scipy.sparse import lil_matrix, csr_matrix
from collections import defaultdict, deque
from ..structs.landmark import Landmark
@@ -508,21 +507,14 @@ class Optimizer:
# SET CONSTRAINTS FOR INEQUALITY
c, A_ub, b_ub = self.init_ub_time(landmarks, max_time) # Adds the distances from each landmark to the other.
print("ok1")
self.respect_number(A_ub, b_ub, L, max_landmarks) # Respects max number of visits (no more possible stops than landmarks).
print("ok2")
self.break_sym(A_ub, b_ub, L) # Breaks the 'zig-zag' symmetry. Avoids d12 and d21 but not larger cirlces.
# SET CONSTRAINTS FOR EQUALITY
print("ok3")
A_eq, b_eq = self.init_eq_not_stay(landmarks) # Force solution not to stay in same place
print("ok4")
self.respect_start_finish(A_eq, b_eq, L) # Force start and finish positions
print("ok5")
self.respect_order(A_eq, b_eq, L) # Respect order of visit (only works when max_time is limiting factor)
print("ok6")
self.respect_user_must(A_eq, b_eq, landmarks) # Force to do/avoid landmarks set by user.
print("ok7")
self.logger.debug(f"Optimizing with {A_ub.shape[0]} + {A_eq.shape[0]} = {A_ub.shape[0] + A_eq.shape[0]} constraints.")
@@ -545,14 +537,12 @@ class Optimizer:
prob += (pl.lpSum([A_eq[i][j] * x[j] for j in range(L*L)]) == b_eq[i])
# 6. Solve the problem
prob.solve(pl.PULP_CBC_CMD(msg=False))
prob.solve(pl.PULP_CBC_CMD(msg=False, gapRel=0.3))
# 7. Extract Results
status = pl.LpStatus[prob.status]
solution = [pl.value(var) for var in x] # The values of the decision variables (will be 0 or 1)
print(status)
self.logger.debug("First results are out. Looking out for circles and correcting.")
# Raise error if no solution is found. FIXME: for now this throws the internal server error