Exemplo n.º 1
0
 def __init__(self, sensory_input, model):
     self.sensory_input = sensory_input
     self.model = model
     self.atomic_updates = [
         self.add_node, self.add_edge, self.change_parameters
     ]  #TODO: Add change_valency
     draw_network(model)
Exemplo n.º 2
0
def verification(graphical, source):
    logging.basicConfig()
    logging.getLogger().setLevel(logging.INFO)
    # generate_obstacles(500)

    os.environ['SDL_VIDEO_CENTERED'] = '1'
    if graphical:
        pg.init()
        pg.display.set_caption(CAPTION)
        pg.display.set_mode(SCREEN_SIZE)

    max_age = 100000
    obstacles = read_obstacles(graphical)
    peepo_network = read_from_file(source)
    draw_network(peepo_network)

    peepos = [
        SurvivalPeepo('peepo', peepo_network, graphical, (5, 400), obstacles)
    ]
    world = World(graphical, peepos, obstacles)

    world.main_loop(max_age)

    pg.quit()
    sys.exit()
Exemplo n.º 3
0
def test_add_all():
    gen_model = create_network_for_add_edge()

    prediction = gen_model.predict(gen_model.model)['B'].values
    observation = gen_model.sensory_input.value('B')
    prediction_error = gen_model.error(prediction, observation)

    # time.sleep(20)

    new_model = gen_model.model_update('B', prediction=prediction, prediction_error=prediction_error)
    draw_network(new_model, True)
Exemplo n.º 4
0
def test_change_params():
    gen_model = create_network()

    prediction = gen_model.predict(gen_model.model)['B'].values
    observation = gen_model.sensory_input.value('B')
    prediction_error = gen_model.error(prediction, observation)

    # time.sleep(20)
    logging.warning('Changing params now...')

    new_model = gen_model.change_parameters(gen_model.model, 'B', prediction, observation)
    draw_network(new_model, True)
Exemplo n.º 5
0
def test_add_edge():
    gen_model = create_network_for_add_edge()

    prediction = gen_model.predict(gen_model.model)['B'].values
    observation = gen_model.sensory_input.value('B')
    prediction_error = gen_model.error(prediction, observation)

    # time.sleep(20)
    logging.warning('Adding edge now...')

    new_model = gen_model.add_edge(gen_model.model, 'B', prediction, observation)
    draw_network(new_model, True)
Exemplo n.º 6
0
    def model_update(self, node, prediction_error, prediction):
        """
        Updates the generative model by changing its structure (i.e. nodes, edges) or its parameters (i.e. CPDs)

        :param node: name of the node for which the prediction generated a prediction error high/precise enough to
        warrant a model update
        :param prediction_error: the difference between the prediction and the observation
        :param prediction: the prediction of the node - based on the hypothesis nodes in the model
        :return: the update model, hopefully one which generates predictions with less prediction error

        :type node: str
        :type prediction_error: np.array
        :type prediction: np.array
        :rtype BayesianModel
        """
        lowest_error_size = self.error_size(prediction,
                                            prediction_error + prediction)
        best_model = self.model
        best_update = 'none'

        for idx, val in enumerate(self.atomic_updates):
            updated_model = val(self.model.copy(), node, prediction,
                                prediction_error + prediction)
            updated_prediction = self.predict(updated_model)[node].values
            updated_error_size = self.error_size(updated_prediction,
                                                 prediction_error + prediction)
            if updated_error_size < lowest_error_size:
                logging.info('Better update from: ' + val.__name__)
                lowest_error_size = updated_error_size
                best_model = updated_model
                best_update = val.__name__

        self.model = best_model
        logging.info('Best Update: ' + best_update)
        draw_network(self.model)
        return self.model