def __init__(self, sim_state: SimulationState = SimulationState()) -> None: BasicStrategy.__init__(self, sim_state) self.seed: Coordinate = Coordinate(0, 0) self.cache: Coordinate = Coordinate(0, 1) self.goal_stack: Goals = [] self.index = 0 self.goal_stacks: List[Goals] = [] self.sinstructions: List[List[Tuple[Coordinate, SInstruction]]] = []
def create_base_sim(strat: ClassVar[BasicStrategy], target: CoordinateList = list()): sim_state = SimulationState() sim_state.s_blocks[Coordinate(0, 0)] = ScaffoldState(SInstruction.STOP) sim_state.robots[Coordinate(0, 0)] = BuilderState() sim_state.target_structure = target sim = BasicAnalyzerSimulation(sim_state, strat) sim.strategy.cache = Down return sim
def configure_target(target: CoordinateList, allow_offset: bool = True) \ -> CoordinateList: if allow_offset: min_x = min(coord.x for coord in target) min_y = min(coord.y for coord in target) max_y = max(coord.y for coord in target) rows = [0]*(max_y-min_y+1) centroid_y = sum(coord.y for coord in target) / len(target) centroid_y = int(centroid_y+0.5) # to do use counter class for coord in target: rows[coord.y-min_y] += 1 row_indicies =[] row_value = 0 for i, value in enumerate(rows): if value > row_value: row_indicies= [i+min_y] row_value = value elif value == row_value: row_indicies.append(i+min_y) row_value = abs(centroid_y - row_indicies[0]) row = row_indicies[0] for curr_row in row_indicies[1:]: if abs(centroid_y - curr_row) < row_value: row = curr_row row_value = abs(centroid_y - curr_row) offset = Coordinate(1 - min_x, -row) target = [coord + offset for coord in target] target.sort(key=OffsetSpineStrat.target_sort_key_tuple) return target
def move_robot(self, robo_coord: Coordinate, robot: BuilderState): new_coords = robo_coord.get_coord_in_direction(robot.direction) if new_coords not in self.sim_state.s_blocks: raise RobotActionError( 'Robot is moving onto a space without scaffolding') del self.sim_state.robots[robo_coord] self.sim_state.robots[new_coords] = robot
def load_run(self): strat = LoadStrat sim = BasicSimulation.create_with_target_structure([Coordinate(0, 0)], strat) file_name = self.load_box.get('1.0', 'end').strip('\n') sim.strategy.load(file_name) player = BasicPlayer(self.winfo_toplevel(), sim, BasicCreator, 'Create') player.grid() player.winfo_toplevel().title("RoboScaffold Sim") self.root_frame.destroy()
def load(self): file_name = self.load_box.get('1.0', 'end')[:-1] with open(file_name) as file: struct = load_coords(file) min_x = min(coord.x for coord in struct) min_y = min(coord.y for coord in struct) offset = Coordinate(-min_x, -min_y) struct = [coord+offset for coord in struct] self.callback(struct)
def update_off_block(self, h_coord: Coordinate, work_dir: Dir, work_y: int) -> Dir: off_spine_block: ScaffoldState = self.sim_state.s_blocks[Coordinate( h_coord.x, 0)] if work_y != h_coord.y != 0: dir = Dir.SOUTH if h_coord.y > 0 else Dir.NORTH work_dir = off_spine_block.set_drive_instr(work_dir, dir) return work_dir
def configure_target(target: CoordinateList, allow_offset: bool=True) \ -> CoordinateList: target.sort(key=lambda coord: (coord.x, -coord.y)) if allow_offset: min_x = min(coord.x for coord in target) min_y = min(coord.y for coord in target) offset = Coordinate(1 - min_x, 1 - min_y) target = [coord + offset for coord in target] return target
def configure_target(target: CoordinateList, allow_offset: bool = True) \ -> CoordinateList: if allow_offset: min_x = min(coord.x for coord in target) centroid_y = sum(coord.y for coord in target) / len(target) centroid_y = int(centroid_y+0.5) offset = Coordinate(1 - min_x, -centroid_y) target = [coord + offset for coord in target] target.sort(key=OffsetSpineStrat.target_sort_key_tuple) return target
def configure_target(target: CoordinateList, allow_offset: bool = True) \ -> CoordinateList: if allow_offset: min_x = min(coord.x for coord in target) min_y = min(coord.y for coord in target) max_y = max(coord.y for coord in target) offset = Coordinate(1 - min_x, -int(round((max_y - min_y) / 2))) target = [coord + offset for coord in target] target.sort(key=OffsetSpineStrat.target_sort_key_tuple) return target
def pick(self, robo_coord: Coordinate, robot: BuilderState): block_coord = robo_coord.get_coord_in_direction(robot.direction) if block_coord in self.sim_state.s_blocks: del self.sim_state.s_blocks[block_coord] robot.block = HeldBlock.SCAFFOLD elif block_coord in self.sim_state.b_blocks: self.sim_state.b_blocks.remove(block_coord) robot.block = HeldBlock.BUILD else: raise RobotActionError( f'Attempting to pick an empty block at {block_coord}')
def add_inner_struct(dimension, n, structure): # print(f'inner {n}') for y in range(dimension - 1): for x in range(dimension - 1): if n % 2: structure.append(Coordinate(x + 1, y + 1)) n //= 2 if n == 0: return elif n < 0: raise Exception('n should always be positive') raise Exception( f'n should be 0 and function should have returned by now but is {n}')
def add_outer_struct(dimension, n, structure): # print(f'outer {n}') for x in range(dimension - 1): if n % 2: # print(f'outer x hit {x+1} at {n}') structure.append(Coordinate(x + 1, 0)) n //= 2 if n == 0: break elif n < 0: raise Exception('n should always be positive') for y in range(dimension - 1): if n % 2: # print(f'outer y hit {y+1} at {n}') structure.append(Coordinate(0, y + 1)) n //= 2 if n == 0: break elif n < 0: raise Exception('n should always be positive') if n not in [0, 1]: raise Exception('n should be 0 or 1') elif n: structure.append(Coordinate(0, 0))
def drop(self, robo_coord: Coordinate, robot: BuilderState): block_coord = robo_coord.get_coord_in_direction(robot.direction) if robot.block is HeldBlock.NONE: raise RobotActionError('Cannot drop NONE Block') sim_state = self.sim_state if block_coord in sim_state.b_blocks or block_coord in sim_state.s_blocks: raise RobotActionError('Block already present') if robot.block is HeldBlock.BUILD: self.sim_state.b_blocks.add(block_coord) elif robot.block is HeldBlock.SCAFFOLD: self.sim_state.s_blocks[block_coord] = ScaffoldState() robot.block = HeldBlock.NONE
def coord_is_reachable(self, start: Coordinate, goal: Coordinate) -> bool: work_coord = Coordinate(start.x, start.y) while work_coord.x != goal.x: while work_coord.y != 0: work_coord += Up if work_coord.y > 0 else Down if work_coord not in self.sim_state.s_blocks: return False work_coord += Right if work_coord.x < goal.x else Left if work_coord not in self.sim_state.s_blocks: return False while work_coord.y != goal.y: work_coord += Down if work_coord.y < goal.y else Up if work_coord not in self.sim_state.s_blocks: return False return True
def update_start_on_blocks(self, robo_coord: Coordinate, h_coord: Coordinate, work_dir: Dir) -> (Dir, int): start_block = self.sim_state.s_blocks[robo_coord] on_spine_block = self.sim_state.s_blocks[Coordinate(robo_coord.x, 0)] if robo_coord.x != h_coord.x: if robo_coord.y != 0: dir = Dir.NORTH if robo_coord.y > 0 else Dir.SOUTH work_dir = start_block.set_drive_instr(work_dir, dir) move_dir = Dir.EAST if robo_coord.x < h_coord.x else Dir.WEST work_dir = on_spine_block.set_drive_instr(work_dir, move_dir) return work_dir, 0 else: if robo_coord.y < h_coord.y: work_dir = start_block.set_drive_instr(work_dir, Dir.SOUTH) return work_dir, h_coord.y elif robo_coord.y > h_coord.y: work_dir = start_block.set_drive_instr(work_dir, Dir.NORTH) return work_dir, h_coord.y return work_dir, robo_coord.y
import tkinter as tk from roboscaffold_sim.coordinate import Coordinate from roboscaffold_sim.simulators.basic_simulator import BasicSimulation from roboscaffold_sim.veiw.basic_player import BasicPlayer sim = BasicSimulation.create_with_target_structure([Coordinate(5, 5)]) root = tk.Tk() player = BasicPlayer(root, sim, load_to=100) player.grid() root.mainloop()
def __init__(self, sim_state: SimulationState) -> None: BasicStrategy.__init__(self, sim_state) self.seed: Coordinate = Coordinate(0, 0) self.cache: Coordinate = Coordinate(0, 1) self.goal_stack = list()
def create_scaffold(string: str) -> Tuple[Coordinate, SInstruction]: coord, stype = string.split(':') coord = Coordinate.from_string(coord) stype = SInstruction[stype] return coord, stype
state_controls.max_state = 10 s_blocks = {} b_blocks = {} robots = {} for instruction in SInstruction: s_blocks[get_next_coord()] = ScaffoldState(instruction) b_blocks[get_next_coord()] = BuildingBlockState() for direction in Direction: for held_block in HeldBlock: robot = BuilderState() robot.block = held_block robot.direction = direction coord = get_next_coord() s_blocks[coord] = ScaffoldState(SInstruction.DRIVE_RIGHT) robots[coord] = robot sim = SimulationState() sim.s_blocks = s_blocks sim.b_blocks = b_blocks sim.robots = robots sim.target_structure = [get_next_coord()] board.draw_sim(sim) goal = Goal(get_next_coord(), GoalType.PLACE_SCAFFOLD, Coordinate(0, 0), Direction.NORTH) board.draw_goal(goal) root.mainloop()
def load_coords(file: TextIO) -> CoordinateList: coords: CoordinateList = [] line = file.readline() for block in line.split(): coords.append(Coordinate.from_string(block)) return coords
def set_struct(self, target): min_x = min(coord.x for coord in target) min_y = min(coord.y for coord in target) offset = Coordinate(-min_x, -min_y) target = [coord+offset for coord in target] return lambda: self.struct_callback(target)
from roboscaffold_sim.coordinate import Coordinate column = [ Coordinate(5, 5), Coordinate(5, 4), Coordinate(5, 3), Coordinate(5, 2), Coordinate(5, 1), ] house = [ Coordinate(1, 4), Coordinate(1, 5), Coordinate(1, 6), Coordinate(1, 7), Coordinate(1, 8), Coordinate(1, 9), Coordinate(2, 9), Coordinate(3, 9), Coordinate(4, 9), Coordinate(5, 9), Coordinate(6, 9), Coordinate(6, 8), Coordinate(6, 7), Coordinate(6, 6), Coordinate(6, 5), Coordinate(6, 4), Coordinate(5, 4), Coordinate(4, 4), Coordinate(3, 4), Coordinate(2, 4),
def create_goal(string: str) -> Goal: coord, gtype = string.split(':') coord = Coordinate.from_string(coord) gtype = GoalType[gtype] return Goal(coord, gtype, Origin, Origin)
def get_next_coord() -> Coordinate: global i next_coord = Coordinate(i % 10, i // 10) i += 1 return next_coord
get_next_coord(): ScaffoldState(SInstruction.NONE), get_next_coord(): ScaffoldState(SInstruction.STOP), get_next_coord(): ScaffoldState(SInstruction.STOP), get_next_coord(): ScaffoldState(SInstruction.NONE), # row 9 get_next_coord(): ScaffoldState(SInstruction.DRIVE_RIGHT), get_next_coord(): ScaffoldState(SInstruction.NONE), get_next_coord(): ScaffoldState(SInstruction.NONE), get_next_coord(): ScaffoldState(SInstruction.DRIVE_RIGHT), get_next_coord(): ScaffoldState(SInstruction.STOP), get_next_coord(): ScaffoldState(SInstruction.STOP), get_next_coord(): ScaffoldState(SInstruction.DRIVE_RIGHT), get_next_coord(): ScaffoldState(SInstruction.NONE), get_next_coord(): ScaffoldState(SInstruction.NONE), get_next_coord(): ScaffoldState(SInstruction.DRIVE_RIGHT), } sim = SimulationState() sim.s_blocks = s_blocks robot = BuilderState() robot.direction = Direction.EAST robot.block = HeldBlock.BUILD sim.robots = {Coordinate(1, 0): copy.copy(robot)} root = tk.Tk() player = BasicPlayer(root, sim) player.grid() root.mainloop()