""" experiment.py Curiosity project Experiment class definition. Aim for better encapsulation. Experiment class - This class should get the various classes to use in running an experiment - EvolveWeights - mda? - Environ (GridWorld, ConvBelt, Puzzle) - Still is going to require ad hoc function to create the particular Environ - But could pass in function to use - Agentclass - And experimental attributes - For example - Experiment constructs EW instance, passes in weight length - Experiment constructs Environ instance - Experiment requests evolution run of EW with parameters - EW calls Experiment for each evaluation of an individual (and in what generation) - Experiment calls Environ.evaluate with individual weights, agentclass - Passes w, tuple back to EW """ import sys import os import traceback class Holder(object): def __init__(self): pass class Experiment(object): """ Experiment class. Instances will drive reinforcement learning experiments. """ def __init__(self): self.agentclass = None self.environclass = None self.evolverclass = None self.environmaker = None pass def validate(self): valid = True # Test that we have classes to use valid = valid and (not self.agentclass in [None]) valid = valid and (not self.environclass in [None]) valid = valid and (not self.evolverclass in [None]) # Test other values here return valid def set_schedule(self, schedule): self.schedule = schedule def set_environ_maker(self, environmaker): self.environmaker = environmaker def make_environ(self): if not self.environmaker in [None]: try: self.environ = self.environmaker() except: estr = f"Error: traceback.format_exc()" print(estr) self.environ = None def set_agentclass(self, agentclass): # Test class for compatibility okclass = True # No test yet if okclass: self.agentclass = agentclass def get_agentclass(self): return self.agentclass def set_environclass(self, environclass): # Test class for compatibility okclass = True if not 'evaluate' in dir(environclass): okclass = False print("set_environclass error: class does not provide 'evaluate'") if okclass: self.environclass = environclass def get_environclass(self): return self.environclass def set_evolverclass(self, evolverclass): # Test class for compatibility okclass = True if not 'driver' in dir(evolverclass): okclass = False print("set_evolverclass error: class does not provide 'driver'") if okclass: self.evolverclass = evolverclass def set_agent_attributes(self, alpha=0.005): self.agent_props = Holder() self.agent_props.alpha = 0.005 def set_evolver_attributes(self, popsize=100, maxgenerations=10000, cxpb=0.5, mtpb=0.05, wmin=-20.0, wmax=20.0, mut_center=0.0, mut_sigma=0.1, mut_indpb=0.05, tournsize=5, tournk=2, normalize_fitness=True, tag='environ' ): self.evolver_props = Holder() self.evolver_props.popsize = popsize self.evolver_props.maxgenerations = maxgenerations self.evolver_props.cxpb = cxpb self.evolver_props.mtpb = mtpb self.evolver_props.wmin = wmin self.evolver_props.wmax = wmax self.evolver_props.mut_center = mut_center self.evolver_props.mut_sigma = mut_sigma self.evolver_props.mut_indpb = mut_indpb self.evolver_props.tournsize = tournsize self.evolver_props.tournk = tournk self.evolver_props.normalize_fitness = normalize_fitness self.evolver_props.tag = tag def make_evolver_instance(self): self.evolver = self.evolverclass( self.environclass, popsize=self.evolver_props.popsize, maxgenerations=self.evolver_props.maxgenerations, cxpb=self.evolver_props.cxpb, mtpb=self.evolver_props.mtpb, wmin=self.evolver_props.wmin, wmax=self.evolver_props.wmax, mut_center= self.evolver_props.mut_center, mut_sigma= self.evolver_props.mut_sigma, mut_indpb= self.evolver_props.mut_indpb, tournsize= self.evolver_props.tournsize, tournk= self.evolver_props.tournk, normalize_fitness= self.evolver_props.normalize_fitness, tag= self.evolver_props.tag ) def set_env_attributes(self): self.env_props = Holder() def handle_evaluation(self, ind, generation): """ evolver calls this to get an evaluation of an individual. Depending on the experiment schedule and generation, this may require constructing a new environment. """ pass def run_experiment(self): """ # Run experiment ew = EvolveWeights(world, popsize=100, maxgenerations=1000, tournsize=75, tournk=3, normalize_fitness=False) ew.driver() """