Skip to content

Instantly share code, notes, and snippets.

@aecanales
Created June 18, 2019 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aecanales/7cd774485a8adff9dabc4afcf7d00e22 to your computer and use it in GitHub Desktop.
Save aecanales/7cd774485a8adff9dabc4afcf7d00e22 to your computer and use it in GitHub Desktop.
Generador de problemas para la T3 del curso IIC2613 @ PUC 2019-1.
from random import randint
from os import sep
MAX_MOVES = 50
PROBLEMS = 20
ROBOTS = [2, 4, 8, 16]
RANGE_X = 20
RANGE_Y = 20
OBSTACLES = 0.1 # Percentage between 0 and 1.
for robots in ROBOTS:
nl = '\n'
print(f'----------- {nl}{robots} AGENTS {nl}-----------')
for i in range(PROBLEMS):
with open (f'problems{sep}{robots}_agents_problem_ins_{i}.lp', 'w') as file:
# Add max movements, grid range and robots to file.
file.write(f'#const bound = {MAX_MOVES}.\n')
file.write(f'time(1..bound).\n')
file.write(f'rangeX(0..{RANGE_X - 1}).\n')
file.write(f'rangeY(0..{RANGE_Y - 1}).\n')
file.write(f'robot(1..{robots}).\n')
# Create internal represntation of grid.
grid = [['' for x in range(RANGE_X)] for y in range(RANGE_Y)]
# Populate grid with robots and their corresponding goals.
for robot in range(robots):
# Loop until a valid position is found.
while True:
r_x, r_y = randint(0, RANGE_X - 1), randint(0, RANGE_Y - 1)
g_x, g_y = randint(0, RANGE_X - 1), randint(0, RANGE_Y - 1)
if grid[r_y][r_x] == '' and grid[g_y][g_x] == '':
grid[r_y][r_x] = f'R{robot + 1}'
grid[g_y][g_x] = f'G{robot + 1}'
break
# Populate grid with obstacles.
for obstacle in range(round(OBSTACLES * RANGE_X * RANGE_Y)):
while True:
x, y = randint(0, RANGE_X - 1), randint(0, RANGE_Y - 1)
if grid[y][x] == '':
grid[y][x] = f'X'
break
# Write resulting grid to file.
for y in range(RANGE_Y):
for x in range(RANGE_X):
if 'R' in grid[y][x]:
file.write(f'on({grid[y][x][1:]},{x},{y},0).' + '\n')
if 'G' in grid[y][x]:
file.write(f'goal({grid[y][x][1:]},{x},{y}).' + '\n')
if grid[y][x] == 'X':
file.write(f'obstacle({x},{y}).' + '\n')
print(f'---- PROBLEM {i} ----')
for row in grid:
for c in row:
if c == '':
print('| ', end='')
elif c == 'X':
print('|XXX', end='')
else:
print(f'|{c}', end='')
if len(c) == 2:
print(' ', end='')
print('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment