def optimise_proportional_training(songs, goal, conf, datasaver=None, iday=None): """ Optimise gestures randomly from the song models with a stochastic local search The number of trainings for this day is proportional to the total number of gestures in the day songs """ measure = conf['measure_obj'] comp = conf['comp_obj'] rng = conf['rng_obj'] train_multiplier = conf.get('train_multiplier', 3) if datasaver is None: datasaver = QuietDataSaver() if rng is None: rng = np.random.RandomState() train_per_day = 0 for song in songs: train_per_day += len(song.gestures) train_per_day *= train_multiplier train_per_day = round( train_per_day) # avoid possible float value with train_multiplier < 1 datasaver.add(label='day', cond='define_number_of_trainings', train_per_day=train_per_day) improvement_cpt = np.zeros(train_per_day) for itrain in range(train_per_day): isong = rng.randint(len(songs)) song = songs[isong] ig = rng.randint(len(song.gestures)) s = song.gen_sound() c = measure(s) pre_score = comp(goal, c) logger.info( '{}/{}: fit gesture {} of song {} (length {}, score {})'.format( itrain + 1, train_per_day, ig, isong, len(s), pre_score)) res, hill_score = fit_gesture_whole_local_search(goal, song, ig, conf) songs[isong].gestures[ig][1] = deepcopy(res) # datasaver.add(iday=iday, itrain=itrain, isong=isong, ig=ig, # pre_score=pre_score, new_score=hill_score) # too much verbose logger.info('new score {}'.format(hill_score)) assert pre_score >= hill_score, "{} >= {} est faux".format( pre_score, hill_score) if hill_score < pre_score: improvement_cpt[itrain] += 1 datasaver.add(label='day', cond='after_day_learning', improvement_cpt=improvement_cpt) return songs
def optimise_gesture_whole_local_search(songs, goal, conf, datasaver=None, iday=None): """ Optimise gestures randomly from the song models with a stochastic local search """ measure = conf['measure_obj'] comp = conf['comp_obj'] train_per_day = conf['train_per_day'] rng = conf['rng_obj'] if datasaver is None: datasaver = QuietDataSaver() if rng is None: rng = np.random.RandomState() improvement_cpt = np.zeros(train_per_day) for itrain in range(train_per_day): isong = rng.randint(len(songs)) song = songs[isong] ig = rng.randint(len(song.gestures)) s = song.gen_sound() c = measure(s) pre_score = comp(goal, c) logger.info( '{}/{}: fit gesture {} of song {} (length {}, score {})'.format( itrain + 1, train_per_day, ig, isong, len(s), pre_score)) res, hill_score = fit_gesture_whole_local_search(goal, song, ig, conf) songs[isong].gestures[ig][1] = deepcopy(res) # datasaver.add(iday=iday, itrain=itrain, isong=isong, ig=ig, # pre_score=pre_score, new_score=hill_score) # too much verbose logger.info('new score {}'.format(hill_score)) assert pre_score >= hill_score, "{} >= {} est faux".format( pre_score, hill_score) if hill_score < pre_score: improvement_cpt[itrain] += 1 datasaver.add(label='day', cond='after_day_learning', improvement_cpt=improvement_cpt) return songs
def mutate_best_models_elite(songs, goal, conf, datasaver=None): """Elite selection and mutation of the best models. Keep the best mutations after each replay, parents are present in the selection. """ if datasaver is None: datasaver = QuietDataSaver() measure = conf['measure_obj'] comp = conf['comp_obj'] nb_replay = conf['replay'] rng = conf['rng_obj'] nb_conc_song = len(songs) pscore = get_scores(goal, songs, measure, comp) score = pscore nb_conc_night = nb_conc_song * 2 # make night_songs an array to do list indexes. night_songs = np.array(songs) for dummy_i in range(nb_replay): fitness = len(night_songs) - rank(score) night_songs = np.random.choice(night_songs, size=nb_conc_night, p=fitness/np.sum(fitness)) night_songs = np.array([song.mutate() for song in night_songs]) score = get_scores(goal, night_songs, measure, comp) fitness = len(night_songs) - rank(score) isongs = rng.choice(len(night_songs), size=nb_conc_song, replace=False, p=fitness/np.sum(fitness)) night_songs = night_songs[isongs] score = score[isongs] nsongs = night_songs.tolist() datasaver.add(prev_songs=songs, prev_scores=pscore, new_songs=nsongs, new_scores=score) return nsongs
def mutate_best_models_dummy(songs, goal, measure, comp, nb_replay, datasaver=None, rng=None): """Dummy selection and mutation of the best models.""" if datasaver is None: datasaver = QuietDataSaver() if rng is None: rng = np.random.RandomState() nb_conc_song = len(songs) night_songs = np.array(songs) pscore = get_scores(goal, songs, measure, comp) nb_conc_night = nb_conc_song * 2 fitness = len(night_songs) - rank(pscore) night_songs = np.random.choice(night_songs, size=nb_conc_night, p=fitness/np.sum(fitness)) night_songs = np.array([song.mutate(nb_replay) for song in night_songs]) score = get_scores(goal, night_songs, measure, comp) fitness = len(night_songs) - rank(score) isongs = rng.choice(len(night_songs), size=nb_conc_song, replace=False, p=fitness/np.sum(fitness)) nsongs = night_songs[isongs].tolist() datasaver.add(prev_songs=songs, prev_scores=pscore, new_songs=nsongs, new_scores=score[isongs]) return nsongs