Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding some of the practical excersises done through out the term #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Term Practical Excersices/seri1/HW1_practical.pdf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
74 changes: 74 additions & 0 deletions Term Practical Excersices/seri1/Rush-Hour/classes/car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import numpy as np

class Car:
def __init__(self, carnum, orientation, main=False) -> None:
self.carnum = carnum
self.start = np.zeros(2)
self.end = np.zeros(2)
self.is_main = main
self.orientation = orientation
self.occupied = []

def set_start(self, x, y):
self.start[0] = x
self.start[1] = y

def get_start(self):
return self.start

def set_end(self, x, y):
self.end[0] = x
self.end[1] = y
self.cells_occupied()

def get_end(self):
return self.end

def cells_occupied(self):
self.occupied.clear()
car_x = int(self.start[0])
car_y = int(self.start[1])
while car_x <= self.end[0] and car_y <= self.end[1]:
self.occupied.append(f"{car_x},{car_y}")
if self.orientation == "v":
car_x += 1
elif self.orientation == "h":
car_y += 1

def move_forward(self):

start_x = self.start[0]
start_y = self.start[1]
end_x = self.end[0]
end_y = self.end[1]

if self.orientation == "h":
start_y += 1
end_y += 1
elif self.orientation == "v":
start_x += 1
end_x += 1

self.start = np.array([start_x, start_y])
self.end = np.array([end_x, end_y])
self.cells_occupied()

def move_backward(self):

start_x = self.start[0]
start_y = self.start[1]
end_x = self.end[0]
end_y = self.end[1]

if self.orientation == "h":
start_y -= 1
end_y -= 1

elif self.orientation == "v":
start_x -= 1
end_x -= 1

# if end_x >= 0 and end_y >= 0 and start_x >= 0 and start_y >= 0:
self.start = np.array([start_x, start_y])
self.end = np.array([end_x, end_y])
self.cells_occupied()
104 changes: 104 additions & 0 deletions Term Practical Excersices/seri1/Rush-Hour/classes/parking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
from classes.car import Car
from copy import deepcopy,copy
from icecream import ic


class Parking:
def __init__(self, w, h, g = 0, parent= None, cars = []) -> None:
self.parent = parent
self.cars: list = cars
self.main_car: Car = self.get_main_car()
self.width = w
self.height = h
self.parking = self.make_parking()
self.generate_parking()
self.next_states = []
self.g = g
self.h = self.heuristic()
self.f = self.g + self.h

def heuristic(self):
end_x = int(self.main_car.end[0])
end_y = int(self.main_car.end[1])
h = 0
if self.main_car.orientation == "v":
for i in range(end_x + 1, self.height):
if self.parking[i][end_y] != "-":
h += 1


if self.main_car.orientation == "h":
for i in range(end_y + 1, self.height):
if self.parking[end_x][i] != "-":
h += 1
return h

def check_goal(self):
if self.main_car.orientation == "v" and self.main_car.end[0] == self.height - 1:
return True
elif self.main_car.orientation == "h" and self.main_car.end[1] == self.width - 1:
return True
return False

def get_main_car(self):
for car in self.cars:
if car.is_main:
return car


def make_parking(self):
return [[ "-" for _ in range(self.width)] for _ in range(self.height)]

def remove_car(self, car: Car):
self.cars.remove(car)
self.generate_parking()

def add_car(self, car):
self.cars.append(car)
self.generate_parking()


def check_if_valid(self, x, y):
if 0 <= y < self.width and 0 <= x < self.height and self.parking[int(x)][int(y)] == "-":
return True
return False


def generate_next_states(self):
for car in self.cars:
temp_cars: list = copy(self.cars)
temp_car: Car = deepcopy(car)

temp_car.move_backward()
if self.check_if_valid(x=temp_car.start[0], y=temp_car.start[1]):
temp_cars.remove(car)
temp_cars.append(temp_car)
self.next_states.append(Parking(w=self.width, h=self.height, g=self.g + 1, parent=self, cars=temp_cars))

temp_cars: list = copy(self.cars)
temp_car: Car = deepcopy(car)

temp_car.move_forward()
if self.check_if_valid(x=temp_car.end[0], y=temp_car.end[1]):
temp_cars.remove(car)
temp_cars.append(temp_car)
self.next_states.append(Parking(w=self.width, h=self.height, g=self.g + 1, parent=self, cars=temp_cars))

def generate_parking(self):
for car in self.cars:
car: Car = car
car.cells_occupied()
for cell in car.occupied:
cell = cell.split(",")
self.parking[int(cell[0])][int(cell[1])] = str(car.carnum)

def get_string_version(self):
string = ""
for row in self.parking:
for char in row:
string += char
return string

def show_parking(self):
self.generate_parking()
ic(self.parking)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import heapq
class PriorityQueue:
""" O(1) access to the lowest-priority item """
def __init__(self):
self.heap = []
self.count = 0

def push(self, item, priority):
entry = (priority, self.count, item)
heapq.heappush(self.heap, entry)
self.count += 1

def pop(self):
(_, _, item) = heapq.heappop(self.heap)
return item

def isEmpty(self):
return len(self.heap) == 0

def update(self, item, priority):
# If item already in priority queue with higher priority, update its priority and rebuild the heap.
for index, (p, c, i) in enumerate(self.heap):
if i == item:
if p <= priority:
break
del self.heap[index]
self.heap.append((priority, c, item))
heapq.heapify(self.heap)
break
else:
self.push(item, priority)
79 changes: 79 additions & 0 deletions Term Practical Excersices/seri1/Rush-Hour/classes/rush_hour.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# from priority_queue import PriorityQueue
from icecream import ic
from classes.parking import Parking

import heapq
class PriorityQueue:
""" O(1) access to the lowest-priority item """
def __init__(self):
self.heap = []
self.count = 0

def push(self, item, priority):
entry = (priority, self.count, item)
heapq.heappush(self.heap, entry)
self.count += 1

def pop(self):
(_, _, item) = heapq.heappop(self.heap)
return item

def isEmpty(self):
return len(self.heap) == 0

def update(self, item, priority):
# If item already in priority queue with higher priority, update its priority and rebuild the heap.
for index, (p, c, i) in enumerate(self.heap):
if i == item:
if p <= priority:
break
del self.heap[index]
self.heap.append((priority, c, item))
heapq.heapify(self.heap)
break
else:
self.push(item, priority)
class Rush_hour:
def __init__(self, initial_state ) -> None:
self.initial_state = initial_state
self.frontier: PriorityQueue = PriorityQueue()
self.explored_set = set()

def run(self):
self.frontier.push(item=self.initial_state, priority=(self.initial_state.f))
i = 0
while not self.frontier.isEmpty():
state: Parking = self.frontier.pop()
state_str = state.get_string_version()
if state_str in self.explored_set:
continue

i += 1
ic(i)

self.explored_set.add(state_str)
if state.check_goal():
ic(f"Found with {state.g} moves")
# self.show_solution(state=state)
return
state.generate_next_states()
list_states = state.next_states
self.add_to_frontier(list_states=list_states)


def add_to_frontier(self, list_states):
for state in list_states:
ic(state.f)
self.frontier.push(item=state, priority=(state.f))

def add_to_explored_set(self, state_str):
self.explored_set.add(state_str)

def show_solution(self, state: Parking):
while state:
state.show_parking()
print(" ")
print(" ")
print(" ")
state = state.parent

88 changes: 88 additions & 0 deletions Term Practical Excersices/seri1/Rush-Hour/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from classes.car import Car
from classes.parking import Parking
from classes.rush_hour import Rush_hour


# first map cars
main_car = Car(0, "h", main=True)
main_car.set_start(x=2, y=1)
main_car.set_end(x=2, y=2)

c1 = Car(1, orientation= "v")
c1.set_start(x=1, y=3)
c1.set_end(x=3, y=3)

c2 = Car(2, orientation= "v")
c2.set_start(x=2, y=4)
c2.set_end(x=3, y=4)

c3 = Car(3, orientation= "h")
c3.set_start(x=4, y=1)
c3.set_end(x=4, y=3)


# easy map
map_one = [main_car, c1, c2, c3]


####################################

#second map cars

main_car = Car(0, "h", main=True)
main_car.set_start(x=2, y=0)
main_car.set_end(x=2, y=1)

c1 = Car(1, orientation= "h")
c1.set_start(x=0, y=3)
c1.set_end(x=0, y=5)

c2 = Car(2, orientation= "h")
c2.set_start(x=3, y=0)
c2.set_end(x=3, y=1)

c3 = Car(3, orientation= "h")
c3.set_start(x=3, y=3)
c3.set_end(x=3, y=4)

c4 = Car(4, orientation= "h")
c4.set_start(x=4, y=2)
c4.set_end(x=4, y=3)

c5 = Car(5, orientation= "h")
c5.set_start(x=5, y=2)
c5.set_end(x=5, y=3)

c6 = Car(6, orientation= "v")
c6.set_start(x=4, y=0)
c6.set_end(x=5, y=0)

c7 = Car(7, orientation= "v")
c7.set_start(x=4, y=1)
c7.set_end(x=5, y=1)

c8 = Car(8, orientation= "v")
c8.set_start(x=2, y=2)
c8.set_end(x=3, y=2)

c9 = Car(9, orientation= "v")
c9.set_start(x=1, y=3)
c9.set_end(x=2, y=3)

c10 = Car(10, orientation= "v")
c10.set_start(x=2, y=5)
c10.set_end(x=4, y=5)

c11 = Car(11, orientation= "v")
c11.set_start(x=0, y=2)
c11.set_end(x=1, y=2)

#harder map
map_two = [main_car, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11]

# pass the map you want as the cars argument

parking = Parking(w = 6, h = 6, g = 0, parent = None, cars = map_two)
game = Rush_hour(initial_state=parking)
game.run()

Loading