def main(mechanism, world, saveimg): """ The main script that runs the simulation. :param mechanism: which mechanism will be used to simulate behavior (simple, recursive, constructive) :param world: which world will be used for simulation (command-line simple world, real world) :param saveimg: will the simulation output be saved """ # initialize existence ex = None if world == "real": # initialize pygame environment pygame.init() clock = pygame.time.Clock() screen = pygame.display.set_mode((canvas.WIDTH, canvas.HEIGHT)) done = False # initialize output path wd = os.getcwd() output_path = '{0}/output/'.format(wd) imgsaver = None if saveimg: # empty the output folder map(os.unlink, [ os.path.join(output_path, f) for f in os.listdir(output_path) ]) imgsaver = ImageSaver(output_path) # pick random start location start_location = (random.randint(canvas.BORDER, canvas.WIDTH - canvas.BORDER), random.randint(canvas.BORDER, canvas.HEIGHT - canvas.BORDER)) # initialize agent kenny = canvas.Agent(start_location) # initialize primitive interactions primitive_interactions = { "move forward": ("e1", "r1", 2), "bump": ("e1", "r2", -50), "turn left": ("e2", "r3", -1), "turn right": ("e3", "r4", -1), "touch empty": ("e4", "r5", -1), "touch wall": ("e4", "r6", -2) } # initialize environments and existences if mechanism == "simple": environment = Environment(kenny, screen, clock) ex = Existence(primitive_interactions, environment) elif mechanism == "recursive": environment = Environment(kenny, screen, clock) ex = RecursiveExistence(primitive_interactions, environment) elif mechanism == "constructive": environment = ConstructiveEnvironment(kenny, screen, clock, imgsaver) ex = ConstructiveExistence(primitive_interactions, environment) i = 1 while not done: # screen.fill((0, 0, 0)) # quit if close button is pressed for event in pygame.event.get(): if event.type == pygame.QUIT: done = True # perform one simulation step (that might consist of several primitive steps) step_trace = ex.step() print(i, step_trace) print "\n" i += 1 # pygame.draw.polygon(screen, kenny.color, kenny.vertices) # if saveimg: # # save each frame as image # pygame.image.save(screen, output_path + str(format(i, '03'))+".jpeg") # pygame.display.flip() # clock.tick(3) elif world == "test": primitive_interactions = { "i1": ("e1", "r1", -1), "i2": ("e1", "r2", 1), "i3": ("e2", "r1", -1), "i4": ("e2", "r2", 1) } if mechanism == "simple": environment = TestEnvironmentD1() ex = Existence(primitive_interactions, environment) elif mechanism == "recursive": environment = TestEnvironmentD2() ex = RecursiveExistence(primitive_interactions, environment) elif mechanism == "constructive": environment = TestEnvironment() ex = ConstructiveExistence(primitive_interactions, environment) for i in range(0, 15): step_trace = ex.step() print(i, step_trace) print "\n"
def main(saveimg): wd = os.getcwd() output_path = '{0}/output/'.format(wd) if saveimg: # empty the output folder map(os.unlink, [os.path.join(output_path, f) for f in os.listdir(output_path)]) pygame.init() clock = pygame.time.Clock() screen = pygame.display.set_mode((canvas.WIDTH, canvas.HEIGHT)) done = False start_location = (random.randint(canvas.BORDER, canvas.WIDTH - canvas.BORDER), random.randint(canvas.BORDER, canvas.HEIGHT - canvas.BORDER)) kenny = canvas.Agent(start_location) primitive_interactions = { "move forward": ("e1", "r1", 5), "bump": ("e1", "r2", -10), "turn left": ("e2", "r3", -1), "turn right": ("e3", "r4", -1), "touch empty": ("e4", "r5", -1), "touch wall": ("e4", "r6", -2) } ex = Existence(primitive_interactions) i = 1 while not done: screen.fill((0, 0, 0)) for event in pygame.event.get(): if event.type == pygame.QUIT: done = True resultstr = None anticipations = ex.anticipate() experiment = ex.select_experiment(anticipations) if experiment.get_label() == 'e1': if kenny.move(1): resultstr = 'r1' # moved forward else: resultstr = 'r2' # bumped elif experiment.get_label() == 'e2': kenny.rotate(90) resultstr = 'r3' elif experiment.get_label() == 'e3': kenny.rotate(-90) resultstr = 'r4' elif experiment.get_label() == 'e4': if kenny.feel_front(1): resultstr = 'r5' # clear ahead else: resultstr = 'r6' # feel wall result = ex.addget_result(resultstr) if result is not None: enacted_interaction = ex.addget_primitive_interaction( experiment, result) print "Enacted " + enacted_interaction.__repr__() if enacted_interaction.get_valence() > 0: ex.mood = 'HAPPY' else: ex.mood = 'SAD' ex.learn_composite_interaction(ex.context_interaction, enacted_interaction) ex.context_interaction = enacted_interaction print(i, experiment.get_label(), result.get_label(), str(enacted_interaction.get_valence())) pygame.draw.polygon(screen, kenny.color, kenny.vertices) if saveimg: # save each frame as image pygame.image.save(screen, output_path + str(format(i, '03')) + ".jpeg") pygame.display.flip() clock.tick(10) i += 1
def main(mechanism, world, saveimg): """ The main script that runs the simulation. :param mechanism: which mechanism will be used to simulate behavior (simple, recursive, constructive) :param world: which world will be used for simulation (command-line simple world, real world) :param saveimg: will the simulation output be saved """ # initialize existence ex = None if world == "real": # initialize pygame environment pygame.init() clock = pygame.time.Clock() screen = pygame.display.set_mode((canvas.WIDTH, canvas.HEIGHT)) done = False # initialize output path wd = os.getcwd() output_path = '{0}/output/'.format(wd) imgsaver = None if saveimg: # empty the output folder map(os.unlink, [os.path.join(output_path, f) for f in os.listdir(output_path)]) imgsaver = ImageSaver(output_path) # pick random start location start_location = (random.randint(canvas.BORDER,canvas.WIDTH-canvas.BORDER), random.randint(canvas.BORDER,canvas.HEIGHT-canvas.BORDER)) # initialize agent kenny = canvas.Agent(start_location) # initialize primitive interactions primitive_interactions = {"move forward": ("e1", "r1", 2), "bump": ("e1", "r2", -50), "turn left": ("e2", "r3", -1), "turn right": ("e3", "r4", -1), "touch empty": ("e4", "r5", -1), "touch wall": ("e4", "r6", -2)} # initialize environments and existences if mechanism == "simple": environment = Environment(kenny, screen, clock) ex = Existence(primitive_interactions, environment) elif mechanism == "recursive": environment = Environment(kenny, screen, clock) ex = RecursiveExistence(primitive_interactions, environment) elif mechanism == "constructive": environment = ConstructiveEnvironment(kenny, screen, clock, imgsaver) ex = ConstructiveExistence(primitive_interactions, environment) i = 1 while not done: # screen.fill((0, 0, 0)) # quit if close button is pressed for event in pygame.event.get(): if event.type == pygame.QUIT: done = True # perform one simulation step (that might consist of several primitive steps) step_trace = ex.step() print (i, step_trace) print "\n" i += 1 # pygame.draw.polygon(screen, kenny.color, kenny.vertices) # if saveimg: # # save each frame as image # pygame.image.save(screen, output_path + str(format(i, '03'))+".jpeg") # pygame.display.flip() # clock.tick(3) elif world == "test": primitive_interactions = {"i1": ("e1", "r1", -1), "i2": ("e1", "r2", 1), "i3": ("e2", "r1", -1), "i4": ("e2", "r2", 1)} if mechanism == "simple": environment = TestEnvironmentD1() ex = Existence(primitive_interactions, environment) elif mechanism == "recursive": environment = TestEnvironmentD2() ex = RecursiveExistence(primitive_interactions, environment) elif mechanism == "constructive": environment = TestEnvironment() ex = ConstructiveExistence(primitive_interactions, environment) for i in range(0, 15): step_trace = ex.step() print (i, step_trace) print "\n"
def main(saveimg): wd = os.getcwd() output_path = '{0}/output/'.format(wd) if saveimg: # empty the output folder map(os.unlink, [os.path.join(output_path, f) for f in os.listdir(output_path)]) pygame.init() clock = pygame.time.Clock() screen = pygame.display.set_mode((canvas.WIDTH, canvas.HEIGHT)) done = False start_location = (random.randint(canvas.BORDER,canvas.WIDTH-canvas.BORDER), random.randint(canvas.BORDER,canvas.HEIGHT-canvas.BORDER)) kenny = canvas.Agent(start_location) primitive_interactions = {"move forward": ("e1", "r1", 5), "bump": ("e1", "r2", -10), "turn left": ("e2", "r3", -1), "turn right": ("e3", "r4", -1), "touch empty": ("e4", "r5", -1), "touch wall": ("e4", "r6", -2)} ex = Existence(primitive_interactions) i = 1 while not done: screen.fill((0, 0, 0)) for event in pygame.event.get(): if event.type == pygame.QUIT: done = True resultstr = None anticipations = ex.anticipate() experiment = ex.select_experiment(anticipations) if experiment.get_label() == 'e1': if kenny.move(1): resultstr = 'r1' # moved forward else: resultstr = 'r2' # bumped elif experiment.get_label() == 'e2': kenny.rotate(90) resultstr = 'r3' elif experiment.get_label() == 'e3': kenny.rotate(-90) resultstr = 'r4' elif experiment.get_label() == 'e4': if kenny.feel_front(1): resultstr = 'r5' # clear ahead else: resultstr = 'r6' # feel wall result = ex.addget_result(resultstr) if result is not None: enacted_interaction = ex.addget_primitive_interaction(experiment, result) print "Enacted " + enacted_interaction.__repr__() if enacted_interaction.get_valence() > 0: ex.mood = 'HAPPY' else: ex.mood = 'SAD' ex.learn_composite_interaction(ex.context_interaction, enacted_interaction) ex.context_interaction = enacted_interaction print (i, experiment.get_label(), result.get_label(), str(enacted_interaction.get_valence())) pygame.draw.polygon(screen, kenny.color, kenny.vertices) if saveimg: # save each frame as image pygame.image.save(screen, output_path + str(format(i, '03'))+".jpeg") pygame.display.flip() clock.tick(10) i += 1