Esempio n. 1
0
def start_env():
    # initializes simulation for coevolution evolution mode.
    env = Environment(experiment_name="test",
                      enemies=enemy,
                      multiplemode="yes",
                      playermode="ai",
                      player_controller=player_controller(),
                      enemy_controller=enemy_controller(),
                      level=2,
                      logs="off",
                      speed="fastest")

    # If model learning is enabled
    if use_model:
        MODEL = get_model()

    # System settings
    n_hidden = 10
    n_vars = (env.get_num_sensors() +
              1) * n_hidden + (n_hidden + 1) * 5  # multilayer with 50 neurons
    env.update_parameter('contacthurt', 'player')

    return env, n_vars
import numpy as np
import matplotlib.pyplot as plt
import sys
sys.path.insert(0, 'evoman')
from evoman.environment import Environment
from demo_controller import player_controller, enemy_controller
from random import sample

ENV = Environment(experiment_name="test",
                  enemies=[2],
                  playermode="ai",
                  player_controller=player_controller(),
                  enemy_controller=enemy_controller(),
                  level=2,
                  speed="fastest",
                  contacthurt='player',
                  logs='off')


class Individual:
    dom_u = 1
    dom_l = -1
    mutation_rate = .3
    n_hidden = 10
    n_vars = (ENV.get_num_sensors() + 1) * n_hidden + (n_hidden + 1) * 5  # multilayer with 50 neurons

    def __init__(self):
        self.age = 0
        self.weights = list()
        self.fitness = None
        self.enemy_life = None
                              self.get_time())


experiment_name = 'co_demo'
if not os.path.exists(experiment_name):
    os.makedirs(experiment_name)

n_hidden_neurons = 10

# initializes simulation for coevolution evolution mode.
env = environm(experiment_name=experiment_name,
               enemies=[2],
               playermode="ai",
               enemymode="ai",
               player_controller=player_controller(n_hidden_neurons),
               enemy_controller=enemy_controller(n_hidden_neurons),
               level=2,
               speed="fastest")

env.state_to_log()  # checks environment state

####   Optimization for controller solution (best genotype/weights for perceptron phenotype network): Ganetic Algorihm    ###

ini = time.time()  # sets time marker

# genetic algorithm params

run_mode = 'train'  # train or test

# number of weights for multilayer with 10 hidden neurons
n_vars = (env.get_num_sensors() + 1) * n_hidden_neurons + (n_hidden_neurons +