From fd32796c18f3d72d3d2f611f02e3dd2ca7d4de1d Mon Sep 17 00:00:00 2001 From: kasperg3 Date: Mon, 8 Apr 2024 13:41:35 +0200 Subject: [PATCH] fixed floating point error and ruff --- main.py | 8 ++++---- pyproject.toml | 2 +- trajallocpy/CBBA.py | 37 +++++++++++++++------------------- trajallocpy/CoverageProblem.py | 2 +- trajallocpy/Experiment.py | 7 ++++--- 5 files changed, 26 insertions(+), 30 deletions(-) diff --git a/main.py b/main.py index a2fed0c..072b524 100644 --- a/main.py +++ b/main.py @@ -46,7 +46,7 @@ def main( ): results = [] run_experiment( - "amagervaerket", + "custom", n_agents, capacity, show_plots, @@ -88,12 +88,12 @@ def run_experiment(experiment_title, n_agents, capacity, show_plots, debug, resu print(file_name, " Tasks: ", number_of_tasks) # Initialize coverage problem and the agents - geometries["boundary"] = geometries["boundary"].buffer(0.1) + geometries["boundary"] = geometries["boundary"].buffer(1) # Scale each polygon in the MultiPolygon scaled_polygons = [] for polygon in geometries["obstacles"].geoms: - scaled_polygon = polygon.buffer(-0.1) # scale(polygon, xfact=0.95, yfact=0.95, origin="centroid") + scaled_polygon = polygon.buffer(-1) # scale(polygon, xfact=0.95, yfact=0.95, origin="centroid") scaled_polygons.append(scaled_polygon) # Create a new MultiPolygon with scaled polygons @@ -174,7 +174,7 @@ def run_experiment(experiment_title, n_agents, capacity, show_plots, debug, resu else: ds = "AC300" n_agents = 3 - capacity = 5000 + capacity = 100000 main( dataset_name=ds, experiment_title=ds + "_" + str(n_agents) + "agents_" + str(capacity) + "capacity", diff --git a/pyproject.toml b/pyproject.toml index d005531..66db6cd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,7 +20,7 @@ classifiers = [ ] [tool.ruff] -extend-select = ["C4", "SIM", "TCH", "SLF", "S", "W"] +extend-select = ["C4", "SIM", "TCH", "SLF", "W"] show-fixes = true line-length = 150 diff --git a/trajallocpy/CBBA.py b/trajallocpy/CBBA.py index 6085feb..6923a3c 100644 --- a/trajallocpy/CBBA.py +++ b/trajallocpy/CBBA.py @@ -1,10 +1,7 @@ import copy import itertools -import math import multiprocessing import random -from functools import cache -from queue import Queue from typing import List import numpy as np @@ -12,7 +9,7 @@ from trajallocpy import Agent from trajallocpy.Task import TrajectoryTask -EPSILON = 1e-6 +EPSILON = 1e-10 class BundleResult: @@ -129,7 +126,7 @@ def getCij(self): j, n, self.state, self.tasks, self.path, self.environment, self.use_single_point_estimation ) c_ijn = S_pj - S_p - if c[j] <= c_ijn: + if c[j] < c_ijn: c[j] = c_ijn # Store the cost best_pos[j] = n reverse[j] = should_be_reversed @@ -213,9 +210,9 @@ def update_task(self): # Rule 3 elif z_ij != -1: m = z_ij - if (s_k[m] > self.timestamps[m]) or (y_kj > y_ij): - self.__update(j, y_kj, z_kj) - elif abs(y_kj - y_ij) < EPSILON and k < self.id: # Tie Breaker + if ( + (s_k[m] > self.timestamps[m]) or (y_kj > y_ij) or (abs(y_kj - y_ij) < EPSILON and k < self.id) + ): # Combine conditions using logical or self.__update(j, y_kj, z_kj) # Rule 4 elif z_ij == -1: @@ -245,10 +242,7 @@ def update_task(self): m = z_kj # Rule 9 if z_ij == i: - if (s_k[m] >= self.timestamps[m]) and (y_kj > y_ij): - self.__update(j, y_kj, z_kj) - # Tie Breaker - elif (s_k[m] >= self.timestamps[m]) and (abs(y_kj - y_ij) < EPSILON and m < self.id): + if (s_k[m] >= self.timestamps[m]) and ((y_kj > y_ij) or (abs(y_kj - y_ij) < EPSILON and m < self.id)): self.__update(j, y_kj, z_kj) # Rule 10 elif z_ij == k: @@ -263,15 +257,16 @@ def update_task(self): # Rule 12 elif z_ij != -1: n = z_ij - if (s_k[m] > self.timestamps[m]) and (s_k[n] > self.timestamps[n]): - self.__update(j, y_kj, z_kj) - elif (s_k[m] > self.timestamps[m]) and (y_kj > y_ij): - self.__update(j, y_kj, z_kj) - # Tie Breaker - elif (s_k[m] > self.timestamps[m]) and (abs(y_kj - y_ij) < EPSILON): - if m < n: - self.__update(j, y_kj, z_kj) - elif (s_k[n] > self.timestamps[n]) and (self.timestamps[m] > s_k[m]): + if ( + (s_k[m] > self.timestamps[m]) + and (s_k[n] > self.timestamps[n]) + or (s_k[m] > self.timestamps[m]) + and (y_kj > y_ij) + or (s_k[m] > self.timestamps[m]) + and (abs(y_kj - y_ij) < EPSILON and m < n) + or (s_k[n] > self.timestamps[n]) + and (self.timestamps[m] > s_k[m]) + ): self.__update(j, y_kj, z_kj) # Rule 13 elif z_ij == -1: diff --git a/trajallocpy/CoverageProblem.py b/trajallocpy/CoverageProblem.py index 4b76bb8..b35f886 100644 --- a/trajallocpy/CoverageProblem.py +++ b/trajallocpy/CoverageProblem.py @@ -24,7 +24,7 @@ def __init__( holes.append(list(shapely.geometry.polygon.orient(polygon, -1).exterior.coords[:-1])) shapely.geometry.polygon.orient(search_area, 1.0) - self.environment.store(list(shapely.geometry.polygon.orient(search_area, 1.0).exterior.coords[:-1]), holes, validate=True) + self.environment.store(list(shapely.geometry.polygon.orient(search_area, 1.0).exterior.coords[:-1]), holes, validate=False) task_list = [] for id, trajectory in enumerate(tasks.geoms): diff --git a/trajallocpy/Experiment.py b/trajallocpy/Experiment.py index 635e7e0..4351684 100644 --- a/trajallocpy/Experiment.py +++ b/trajallocpy/Experiment.py @@ -37,9 +37,9 @@ def evaluateSolution(self): route_list = [] max_path_cost = 0 for r in self.robot_list.values(): - total_path_length += ACBBA.getTotalPathLength(r.state, r.getPathTasks(), r.environment) - total_task_length += ACBBA.getTotalTaskLength(r.getPathTasks()) - agent_path_cost = ACBBA.getTotalTravelCost(r.state, r.getPathTasks(), r.environment) + total_path_length += Agent.getTotalPathLength(r.state, r.getPathTasks(), r.environment) + total_task_length += Agent.getTotalTaskLength(r.getPathTasks()) + agent_path_cost = Agent.getTotalTravelCost(r.state, r.getPathTasks(), r.environment) total_path_cost += agent_path_cost route = [r.state] for task in r.getPathTasks(): @@ -104,6 +104,7 @@ def solve(self, profiling_enabled=False, debug=False): # Create a list to store the threads processes: list[multiprocessing.Process] = [] # Start multiple threads + for robot in self.robot_list.values(): # robot.build_bundle(result_queue) process = multiprocessing.Process(target=robot.build_bundle, args=(result_queue,))