Skip to content

Commit

Permalink
Merge pull request #30 from watakandai/integrated-examples
Browse files Browse the repository at this point in the history
temp
  • Loading branch information
watakandai authored Aug 12, 2024
2 parents 8bf8fc5 + 9259bb7 commit 32b9d37
Show file tree
Hide file tree
Showing 10 changed files with 287 additions and 30 deletions.
75 changes: 75 additions & 0 deletions examples/demo/integrated_example1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import specless as sl # or load from specless.inference import TPOInference
from examples.demo.planning_utils import get_location_assignments


def main():

### Timed Partial Order Inference

# Manually prepare a list of demonstrations
demonstrations: list = [
[[1, "Room B"], [16, "Room C"], [31, "Room J"], [46, "Room I"]],
[[10, "Room J"], [20, "Room I"], [30, "Room B"], [40, "Room C"]],
]

# Timed Partial Order Inference
inference = sl.TPOInferenceAlgorithm()
timed_partial_order: sl.Specification = inference.infer(demonstrations)

print(timed_partial_order)

#####################
# Given #
#####################
# A floormap
floormap = {
"Room A": (0, 5),
"Room B": (0, 15),
"Room C": (3, 0),
"Room D": (4, 5),
"Room E": (4, 10),
"Room F": (6, 5),
"Room G": (6, 10),
"Room H": (7, 0),
"Room I": (10, 5),
"Room J": (10, 15),
}
# Distance cost between two locations
# We can similarly use A* to compute the "actual" distance
dist = lambda v1, v2: ((v1[0] - v2[0]) ** 2 + (v1[1] - v2[1]) ** 2) ** 0.5
# For now, let's just use the euclidean distance
costs = [[dist(v1, v2) for v2 in floormap.values()] for v1 in floormap.values()]

#####################
# Define #
#####################
# Define a task (rooms to visit)
rooms_to_visit = ["Room B", "Room C", "Room J", "Room I"]
# Define initial locations of the robot
robot_initial_locations = ["Room A", "Room D"]
rooms_of_interest = rooms_to_visit + robot_initial_locations

#####################
# Main #
#####################
# Recreate the cost matrix
rooms = list(floormap.keys())
costs = [
[costs[rooms.index(r1)][rooms.index(r2)] for r2 in rooms_of_interest]
for r1 in rooms_of_interest
]

tours, cost, timestamps = get_location_assignments(
rooms_of_interest,
robot_initial_locations,
costs,
timed_partial_order=timed_partial_order,
)

print(tours)
print(cost)
print(timestamps)


if __name__ == "__main__":
main()
20 changes: 9 additions & 11 deletions examples/demo/learning_example1.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ def main():

# Manually prepare a list of demonstrations
demonstrations = [
["e1", "e2", "e3", "e4", "e5"], # trace 1
["e1", "e4", "e2", "e3", "e5"], # trace 2
["e1", "e2", "e4", "e3", "e5"], # trace 3
["e1", "e2", "e3", "e4", "e5"], # trace 1
["e1", "e4", "e2", "e3", "e5"], # trace 2
["e1", "e2", "e4", "e3", "e5"], # trace 3
]

# Run the inference
inference = sl.POInferenceAlgorithm()
specification = inference.infer(demonstrations) # returns a Specification
specification = inference.infer(demonstrations) # returns a Specification

# prints the specification
print(specification) # doctest: +ELLIPSIS
print(specification) # doctest: +ELLIPSIS

# exports the specification to a file

# drawws the specification to a file
sl.draw_graph(specification, filepath='spec')
sl.draw_graph(specification, filepath="spec")

### Timed Partial Order Inference

Expand All @@ -31,15 +31,13 @@ def main():
[[1, "a"], [2, "b"], [3, "c"]],
[[4, "d"], [5, "e"], [6, "f"]],
]
columns: list = ["timestamp", "symbol"]

timedtrace_dataset = sl.ArrayDataset(demonstrations, columns)

# Timed Partial Order Inference
inference = sl.TPOInferenceAlgorithm()
specification: sl.Specification = inference.infer(timedtrace_dataset)
specification: sl.Specification = inference.infer(demonstrations)

# TODO: Plan with the TPO.


if __name__ == "__main__":
main()

22 changes: 16 additions & 6 deletions examples/demo/planning_example1.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
"""
Goal
====
In this script, we will show how to solve/implement for Example 1,2,3,4
In this script, we will show how to solve/implement the examples
Examples
========
1. Given number of rooms, solve a TSP for a single agent.
2. Multiple Agent
3. Global Time Constraints
4. Local Time Constraints
#! Note: Add an example with TPOs & Add a flag to export MILP formulation to a file
3. Global Time Constraints (and with the TPO representation)
4. Local Time Constraints (and with the TPO representation)
4.5. How to export the MILP formulation to a file for debugging?
5. What if we don't need to come back to its home depot?
6. What if robots are in a new location (not in the designated area)
-> Run A* to get the cost matrix
7. What if a path between two locations are blocked?
-> Set the cost to a big number (not infinity)
8. Can we visit the same place multiple times?
8.1. Can we visit the same place N times?
-> Yes, specless can easily deal with the problem
8.2. Can we visit the same place arbitrary many times?
-> There are multiple ways: (1) Reformulate MILP, (2) Use OR-Tools
-> https://developers.google.com/optimization/routing/penalties
#! NOTE: The following constraints are difficult to model in our MILP formulation,
# because we formulate the TSP as a flow constraining problem whic makes it difficult
# to add additional "dimension" constraints, e.g., pickup, resource, and capacity.
9. Pick and Delivery constraints
#! NOTE: Pick and Delivery constraints are difficult to model in MILP,
because it constraining the same vehicle to pickup and deliver is difficult.
-> Not Possible. Use OR-Tools
-> https://developers.google.com/optimization/routing/pickup_delivery
10. Resource constraints, etc. Battery
Expand Down Expand Up @@ -73,8 +82,9 @@ def main():
}

# Compute the distance cost between two locations
# For now, let's just use the euclidean distance
# For now, let's just use the euclidean distance for demo.
# (In a continuous environment, we can similarly use A* to compute the distance)
#! NOTE: We need to use A* for modeling more accurate distance of the environment
dist = lambda v1, v2: ((v1[0] - v2[0]) ** 2 + (v1[1] - v2[1]) ** 2) ** 0.5
costs = [[dist(v1, v2) for v2 in floormap.values()] for v1 in floormap.values()]

Expand Down
2 changes: 1 addition & 1 deletion examples/demo/planning_example2.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def main():
# Define #
#####################
# Define a task (rooms to visit)
rooms_to_visit = ["Room B", "Room C", "Room J", "Room I"]
rooms_to_visit = ["Room B", "Room C", "Room J", "Room I1", "Room I2"]
# NOTE: Define initial locations of the robot
robot_initial_locations = ["Room A", "Room D"]
rooms_of_interest = rooms_to_visit + robot_initial_locations
Expand Down
7 changes: 7 additions & 0 deletions examples/demo/planning_example3.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"""

from examples.demo.planning_utils import get_location_assignments
import specless as sl


def main():
Expand Down Expand Up @@ -90,6 +91,11 @@ def main():
"Room B": (0, 15),
"Room J": (21, 30),
}
# OR
# timed_partial_order: sl.TimedPartialOrder = (
# sl.TimedPartialOrder.from_constraints(global_constraints, {})
# )

#####################
# Main #
#####################
Expand All @@ -105,6 +111,7 @@ def main():
robot_initial_locations,
costs,
global_constraints=global_constraints,
# timed_partial_order=timed_partial_order,
)

print(tours)
Expand Down
6 changes: 6 additions & 0 deletions examples/demo/planning_example4.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ def main():
("Room B", "Room C"): (0, 15),
("Room J", "Room I"): (20, 30),
}
# OR
# timed_partial_order: sl.TimedPartialOrder = (
# sl.TimedPartialOrder.from_constraints({}, local_constraints)
# )

#####################
# Main #
#####################
Expand All @@ -105,6 +110,7 @@ def main():
robot_initial_locations,
costs,
local_constraints=local_constraints,
# timed_partial_order=timed_partial_order,
)

print(tours)
Expand Down
123 changes: 123 additions & 0 deletions examples/demo/planning_example4_5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
"""
Goal
====
In this script, we will show how to solve/implement for Example 1,2,3,4
Examples
========
1. Given number of rooms, solve a TSP for a single agent.
2. Multiple Agent
3. Global Time Constraints
4. Local Time Constraints
5. What if we don't need to come back to its home depot?
6. What if robots are in a new location (not in the designated area)
-> Run A* to get the cost matrix
7. What if a path between two locations are blocked?
-> Set the cost to a big number (not infinity)
8. Can we visit the same place multiple times?'
-> There are multiple ways: (1) Reformulate MILP, (2) Use OR-Tools
-> https://developers.google.com/optimization/routing/penalties
9. Pick and Delivery constraints?
-> Not Possible. Use OR-Tools
-> https://developers.google.com/optimization/routing/pickup_delivery
10. Resource constraints, etc. Battery
-> Use OR-Tools
-> https://developers.google.com/optimization/routing/cvrptw_resources
11. Carriege Capacity constraints, etc, weight of the package.
-> Use OR-Tools
-> https://developers.google.com/optimization/routing/cvrp
Environment
===========
Consider the following environment:
########################################################
# # # #
# # A # B #
# ############ ######################### ####
# C #
# # ####### ############ ##### #
# # # D # E # #
# # # # # #
###### ################################### #
# # # # # #
# # # F # G # #
# # ####### ############ ##### #
# H #
# ############ ######################### ####
# # I # J #
# # # #
########################################################
"""

from examples.demo.planning_utils import get_location_assignments


def main():
#####################
# Given #
#####################
# A floormap
floormap = {
"Room A": (0, 5),
"Room B": (0, 15),
"Room C": (3, 0),
"Room D": (4, 5),
"Room E": (4, 10),
"Room F": (6, 5),
"Room G": (6, 10),
"Room H": (7, 0),
"Room I": (10, 5),
"Room J": (10, 15),
}
# Distance cost between two locations
# We can similarly use A* to compute the "actual" distance
dist = lambda v1, v2: ((v1[0] - v2[0]) ** 2 + (v1[1] - v2[1]) ** 2) ** 0.5
# For now, let's just use the euclidean distance
costs = [[dist(v1, v2) for v2 in floormap.values()] for v1 in floormap.values()]

#####################
# Define #
#####################
# Define a task (rooms to visit)
rooms_to_visit = ["Room B", "Room C", "Room J", "Room I"]
# Define initial locations of the robot
robot_initial_locations = ["Room A", "Room D"]
rooms_of_interest = rooms_to_visit + robot_initial_locations

#! NOTE: Adding Local Constraints
local_constraints = {
("Room B", "Room C"): (0, 15),
("Room J", "Room I"): (20, 30),
}
# OR
# timed_partial_order: sl.TimedPartialOrder = (
# sl.TimedPartialOrder.from_constraints({}, local_constraints)
# )

#####################
# Main #
#####################
# Recreate the cost matrix
rooms = list(floormap.keys())
costs = [
[costs[rooms.index(r1)][rooms.index(r2)] for r2 in rooms_of_interest]
for r1 in rooms_of_interest
]

tours, cost, timestamps = get_location_assignments(
rooms_of_interest,
robot_initial_locations,
costs,
local_constraints=local_constraints,
# timed_partial_order=timed_partial_order,
export_filename="examples/demo/planning_example4_5.lp",
)

print(tours)
print(cost)
print(timestamps)


if __name__ == "__main__":
main()
4 changes: 3 additions & 1 deletion examples/demo/planning_example9.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
########################################################
"""

"""Capacited Vehicles Routing Problem (CVRP)."""
"""
Capacited Vehicles Routing Problem (CVRP).
"""

from ortools.constraint_solver import routing_enums_pb2
from ortools.constraint_solver import pywrapcp
Expand Down
Loading

0 comments on commit 32b9d37

Please sign in to comment.