Skip to content

Commit

Permalink
fixed floating point error and ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperg3 committed Apr 8, 2024
1 parent 12c9a69 commit fd32796
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 30 deletions.
8 changes: 4 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def main(
):
results = []
run_experiment(
"amagervaerket",
"custom",
n_agents,
capacity,
show_plots,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 16 additions & 21 deletions trajallocpy/CBBA.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
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

from trajallocpy import Agent
from trajallocpy.Task import TrajectoryTask

EPSILON = 1e-6
EPSILON = 1e-10


class BundleResult:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion trajallocpy/CoverageProblem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 4 additions & 3 deletions trajallocpy/Experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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,))
Expand Down

0 comments on commit fd32796

Please sign in to comment.