def initializeModels(): model = newModel() board = [0 for x in range(10)] board = [list(board) for x in range(20)] species = Species(model,[board]*2,[[1,0,0,0,0,0,0,0],[0,0,0,0,0,0,1,0]],0) assert (len(species.intake) == len(species.output)) species.create() return species
def reset(): try: intake = np.load('saves/DInput.npy') output = np.load('saves/DOutput.npy') intake = np.vstack([intake, lastGen.p1.intake, lastGen.p2.intake]) output = np.vstack([output, lastGen.p1.output, lastGen.p2.output]) except: traceback.print_exc() intake = np.vstack([lastGen.p1.intake, lastGen.p2.intake]) output = np.vstack([lastGen.p1.output, lastGen.p2.output]) p = np.random.permutation(output.shape[0]) intake, output = intake[p], output[p] if output.shape[0] > 1500000: intake, output = intake[:1500000], output[:1500000] np.save("saves/DInput.npy", intake) np.save("saves/DOutput.npy", output) tempSpecies = Species(newModel(), intake, output, mChance) tempSpecies.create() gen = Generation(tempSpecies.base, mChance) return gen
genFile = open('gen.txt','r') read = genFile.readlines() totalGen = int(read[0])+1 genFile.close() except: totalGen=0 try: inputs1 = np.load('saves/inputsr.npy').tolist() outputs1 = np.load('saves/outputsr.npy').tolist() modelp1 = load_model('saves/pr.h5') species = Species(modelp1,inputs1,outputs1,mChance) print('Load models successful!') except Exception: totalGen=0 traceback.print_exc() print("Couldn't find save files... Initializing models") species = initializeModels() for i in range(100): if i%10 == 0: species.save(f'pr{i}.h5') playGame(species) print(f'For a total of {species.score:.2f}!') species.create() species.output = [] species.intake = [] with open('tetris.log','a') as log: log.write(f'{strftime("%d %b %Y %H:%M:%S", localtime())}, {species.name}, finished with a score of {species.score:.2f}, clearing {species.cleared} lines\n') with open('gen.txt','w') as genFile: genFile.write(str(totalGen+i))