if len(s) > 0: for i, d in enumerate(s): s[i] = d.split(os.getcwd() + '/' + DB_DIR + '/')[1] s[i] = int(s[i].split('.csv')[0]) ID = max(s) + 1 ## Initial ID else: ID = 0 for repetiton_id in range(REPETITIONS): ## Initialize Gym problem problem = Gym(ENV_NAME, iterations=ITERATIONS, max_steps=MAX_STEPS, action_mode=ACTION_MODE) ## Initialize UMDAc umdac = UMDAc(model, problem=problem, gen_size=GEN_SIZE) ## Write to db db = open(DB_DIR + '/' + str(ID) + '.csv', 'w') dbwriter = csv.DictWriter(db, fieldnames=DB_FIELDS) dbwriter.writeheader() print('Repetition: ', repetiton_id) print('id: ', ID) ### TRAINING ###
from UMDAc.Wrappers.Gym import Gym ### HYPERPARAMETERS ### GENERATIONS = 500 GEN_SIZE = 200 SURV = .5 RAND_SURV = .3 NOISE = None FILENAME = 'lunar_result.h5' # Filename of best specimen # Problem specific MAX_STEPS = 400 ITERATIONS = 3 ## Initialize Gym problem problem = Gym('LunarLander-v2', iterations=ITERATIONS, max_steps=MAX_STEPS) ## Initialize model a = Input(shape=(8, )) b = Dense(4)(a) model = Model(inputs=a, outputs=b) ## Initialize UMDAc umdac = UMDAc(model, problem=problem, gen_size=GEN_SIZE) model.summary() ### TRAINING ### for generation in range(GENERATIONS):
### HYPERPARAMETERS ### GENERATIONS = 1000 GEN_SIZE = 100 SURV = .5 RAND_SURV = .3 NOISE = None FILENAME = 'car_result.h5' # Filename of best specimen ITERATIONS = 1 MAX_STEPS = 200 ## Initialize Gym problem problem = Gym('CarRacing-v0', iterations=ITERATIONS, max_steps=MAX_STEPS, action_mode='raw') in_shape = (96, 96, 3) action_size = problem.env.action_space.shape[0] ## Build Network model = Sequential() model.add( Convolution2D(5, kernel_size=(10, 10), strides=(10, 10), activation='relu', data_format="channels_last", input_shape=in_shape))
def evaluate(self, ids): from tqdm import tqdm from UMDAc.UMDAc import UMDAc from UMDAc.Wrappers.Gym import Gym from GA.GA import GA max_steps = input('Maximum step number [None]:') if max_steps == '': max_steps = None else: max_steps = int(max_steps) action_mode = None while action_mode not in range(2): action_mode = input('action mode [0:raw/1:argmax]:') action_mode = int(action_mode) if action_mode == 0: action_mode = 'raw' elif action_mode == 1: action_mode = 'argmax' repetitions = input('Repetitions [100]:') if repetitions == '': repetitions = 100 else: repetitions = int(repetitions) print('') print('--- Selected options ---') print('Maximum steps: ', max_steps) print('Action mode: ', action_mode) print('Repetitions: ', repetitions) print('') if input('Are you sure to continue? [N/y] >') != 'y': print('aborting...') quit() print('') ## Open evaluation log evalog = open('evaluation.csv', 'a') evalwriter = csv.DictWriter( evalog, fieldnames=['id', 'Algorithm', 'Environment', 'Total reward']) for ID in ids: print('Experiment: ', ID) ## Get experiment's environment mainlog = self.get_main() env = mainlog.loc[mainlog['id'] == ID]['Environment'] env = list(env)[0] ## Get algorithm used in experiment alg = mainlog.loc[mainlog['id'] == ID]['Algorithm'] alg = list(alg)[0] ## Init env problem = Gym(env, iterations=1, max_steps=max_steps, action_mode=action_mode) ## Select algorithm if alg == 'UMDAc': algorithm = UMDAc(model=None, problem=problem, gen_size=None) elif alg == 'GA': algorithm = GA(model=None, problem=problem, gen_size=None) else: print('Error, invalid algorithm name, name found:', alg) quit() algorithm.load_model('db/' + str(ID) + '.h5') for i in tqdm(range(repetitions)): ## Evaluate specimen, render enabled tr = problem.evaluate(specimen=None, model=algorithm.model, render=False, verbose=False) evalwriter.writerow({ 'id': ID, 'Algorithm': alg, 'Environment': env, 'Total reward': tr }) evalog.close()
print('') act_sel = None while act_sel not in range(len(envs)): act_sel = int(input('Selection > ')) action_mode = action_modes[act_sel] from UMDAc.UMDAc import UMDAc from UMDAc.Wrappers.Gym import Gym ### INITIALIZATION ### ## Init env problem = Gym(envs[sel], iterations=1, max_steps=MAX_STEPS, action_mode=action_mode) ## Init UMDAc umdac = UMDAc(model=None, problem=problem, gen_size=None) umdac.load_model(sname) from tqdm import tqdm log = [] print('') if input('Render environment? [Y/n]') == 'n': RENDER = False else: