From 14385342ccb4519ca5c5631a6db2fda0c1ef322c Mon Sep 17 00:00:00 2001 From: Helldragon67 Date: Wed, 15 Jan 2025 09:35:25 +0100 Subject: [PATCH] better sym breaking --- backend/src/tests/test_main.py | 1 + backend/src/utils/optimizer.py | 16 ++++++++-------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/backend/src/tests/test_main.py b/backend/src/tests/test_main.py index 1a353f4..92b7be6 100644 --- a/backend/src/tests/test_main.py +++ b/backend/src/tests/test_main.py @@ -35,6 +35,7 @@ def test_turckheim(client, request): # pylint: disable=redefined-outer-name } ) result = response.json() + print(result) landmarks = load_trip_landmarks(client, result['first_landmark_uuid']) diff --git a/backend/src/utils/optimizer.py b/backend/src/utils/optimizer.py index 7d4d345..77b30de 100644 --- a/backend/src/utils/optimizer.py +++ b/backend/src/utils/optimizer.py @@ -35,7 +35,7 @@ class Optimizer: """ Initialize the objective function coefficients and inequality constraints. -> Adds 1 row of constraints - -> Pre-allocates A_ub for the rest of the computations with 2*L rows + -> Pre-allocates A_ub for the rest of the computations with L + (L*L-L)/2 rows This function computes the distances between all landmarks and stores their attractiveness to maximize sightseeing. The goal is to maximize @@ -55,8 +55,8 @@ class Optimizer: c = np.zeros(L, dtype=np.int16) # Coefficients of inequality constraints (left-hand side) - A_ub = np.zeros((2*L, L*L), dtype=np.int16) - b_ub = np.zeros(2*L, dtype=np.int16) + A_ub = np.zeros((L + int((L*L-L)/2), L*L), dtype=np.int16) + b_ub = np.zeros(L + int((L*L-L)/2), dtype=np.int16) # Fill in first row b_ub[0] = round(max_time*self.overshoot) @@ -65,9 +65,9 @@ class Optimizer: c[i] = -spot1.attractiveness for j in range(i+1, L) : if i !=j : - t = get_time(spot1.location, landmarks[j].location) + spot1.duration - A_ub[0, i*L + j] = t - A_ub[0, j*L + i] = t + t = get_time(spot1.location, landmarks[j].location) + A_ub[0, i*L + j] = t + spot1.duration + A_ub[0, j*L + i] = t + landmarks[j].duration # Expand 'c' to L*L for every decision variable c = np.tile(c, L) @@ -120,7 +120,7 @@ class Optimizer: Generate constraints to prevent simultaneous travel between two landmarks in both directions. Constraint to not have d14 and d41 simultaneously. Does not prevent cyclic paths with more elements - -> Adds L rows of constraints (some of which might be zero) + -> Adds (L*L-L)/2 rows of constraints (some of which might be zero) Args: L (int): Number of landmarks. @@ -136,7 +136,7 @@ class Optimizer: # A = np.zeros((len(up_ind_x[1:]),L*L), dtype=np.int8) # Fill-in rows L to 2*L-1 - for i in range(L) : + for i in range(int((L*L-L)/2)) : if up_ind_x[i] != up_ind_y[i] : A[L+i, up_ind_x[i]*L + up_ind_y[i]] = 1 A[L+i, up_ind_y[i]*L + up_ind_x[i]] = 1