Esempio n. 1
0
"""
Basical implementation of a terminal view for World object.

"""
import neural_world.commons as commons
import neural_world.actions as action
from neural_world.individual import Individual
from neural_world.nutrient import Nutrient
from neural_world.neural_network import NeuralNetwork
from . import observer


LOGGER = commons.logger()


class TerminalWorldView(observer.Observer, action.ActionEmitter):
    def __init__(self, engine):
        super().__init__(invoker=engine)
        self.graphics = {
            Nutrient: '·',
            Individual: '#',
        }


    def update(self, world, signals={}):
        """Print World in the terminal"""
        if len(signals) == 0 or observer.Signal.NEW_STEP in signals:
            print('\nstep:', world.step_number,
                  '\tindividuals:', world.object_counter[Individual],
                  '\tmemories:', len(NeuralNetwork.MEMORIES), NeuralNetwork.MEMORIES,
                  '\ndirections:', NeuralNetwork.DIRECTIONS,
"""Definition of the NeuralNetworkEngine base class."""

import itertools

from neural_world import commons
from neural_world import default
from neural_world import solving
from neural_world.commons import (NeuronType, Direction, FILE_ASP_CLEANING,
                                  FILE_ASP_RUNNING)

LOGGER = commons.logger(commons.SUBLOGGER_LIFE)
MINIMAL_NEURON_ID = 1


class NeuralNetworkEngine:
    """Base class for the NeuralNetwork class. Define the underlying behavior
    of neural networks, as an abstract object.

    Each neural network figures a set of atoms, and provides an API for
    building new neural networks, reacting to neighbors states and cloning.

    A neural network is a string, containing ASP-formatted data as atoms as:
        - neuron(I,T): neuron of id I is of type T (T in IXANO)
        - output(I): neuron of id I is an output neuron
        - edge(I,J): there is an edge between neurons of id I and J


    The object assume that inputs and outputs attributes are iterables
    of pairs (f, n), where f is a function and n the number of neuron
    associated to the function.
    For input neurons, function takes no parameters, and should return
Esempio n. 3
0
Individual is a class that keep together id, dna and neural networks.

"""
from itertools import islice, chain
from collections import defaultdict

import tergraw

from neural_world import actions
from neural_world import default
from neural_world import commons
from neural_world.commons import Direction
from neural_world import neural_network


LOGGER = commons.logger('life')


class Individual:
    """Unit of life, having a neural network and able to move in space."""
    next_individual_id = 1  # useful for give to each instance a unique id

    def __init__(self, neural_network:str, energy:int):
        # Attribution of an unique ID
        self.unique_id = Individual.next_individual_id
        Individual.next_individual_id += 1
        # Life support
        self.neural_network = neural_network
        self.energy = energy

    def update(self, engine, neighbors, coords):
Esempio n. 4
0
"""
import time
import docopt
from functools import partial

import neural_world.actions as action
from neural_world import commons
from neural_world.info import VERSION
from neural_world.config import Configuration
from neural_world.engine import Engine
from neural_world.prompt import Prompt
from neural_world.observer import (Archivist, TerminalWorldView,
                                   NullTerminalWorldView, TreeBuilder)

LOGGER = commons.logger()


def run_simulation(config, render_png):
    """Run a simulation, with CLI and many default behaviors"""
    # Observers
    v = TerminalWorldView
    a = partial(Archivist,
                archive_directory=config.dir_archive_simulation,
                render_graph=render_png)
    t = partial(TreeBuilder,
                archive_directory=config.dir_archive_simulation,
                render_graph=render_png)

    # Engine from rules
    e = Engine.generate_from(config, observers=(v, a, t))
Esempio n. 5
0
"""Definition of generic solving routines,
encapsulating ASP calls through pyasp API.

"""
from pyasp import asp
from neural_world import commons


LOGGER = commons.logger(commons.SUBLOGGER_SOLVING)

# ASP SOLVING OPTIONS
ASP_GRINGO_OPTIONS = ''  # no default options
ASP_CLASP_OPTIONS  = ''  # options of solving heuristics
# ASP_CLASP_OPTIONS += ' -Wno-atom-undefined'
# ASP_CLASP_OPTIONS += ' --configuration=frumpy'
# ASP_CLASP_OPTIONS += ' --heuristic=Vsids'


def model_from(base_atoms, aspfiles, aspargs={},
               gringo_options='', clasp_options=''):
    """Compute a model from ASP source code in aspfiles, with aspargs
    given as grounding arguments and base_atoms given as input atoms.

    base_atoms -- string, ASP-readable atoms
    aspfiles -- (list of) filename, contains the ASP source code
    aspargs -- dict of constant:value that will be set as constants in aspfiles
    gringo_options -- string of command-line options given to gringo
    clasp_options -- string of command-line options given to clasp

    """
    # use the right basename and use list of aspfiles in all cases
Esempio n. 6
0
"""Definition of generic solving routines,
encapsulating ASP calls through pyasp API.

"""
from pyasp import asp
from neural_world import commons

LOGGER = commons.logger(commons.SUBLOGGER_SOLVING)

# ASP SOLVING OPTIONS
ASP_GRINGO_OPTIONS = ''  # no default options
ASP_CLASP_OPTIONS = ''  # options of solving heuristics
# ASP_CLASP_OPTIONS += ' -Wno-atom-undefined'
# ASP_CLASP_OPTIONS += ' --configuration=frumpy'
# ASP_CLASP_OPTIONS += ' --heuristic=Vsids'


def model_from(base_atoms,
               aspfiles,
               aspargs={},
               gringo_options='',
               clasp_options=''):
    """Compute a model from ASP source code in aspfiles, with aspargs
    given as grounding arguments and base_atoms given as input atoms.

    base_atoms -- string, ASP-readable atoms
    aspfiles -- (list of) filename, contains the ASP source code
    aspargs -- dict of constant:value that will be set as constants in aspfiles
    gringo_options -- string of command-line options given to gringo
    clasp_options -- string of command-line options given to clasp
Esempio n. 7
0
import random
import itertools
from functools   import partial
from collections import deque

from neural_world import actions
from neural_world import default
from neural_world import atoms
from neural_world import commons
from neural_world import solving
from neural_world.commons import (FILE_ASP_RUNNING, FILE_ASP_CLEANING,
                                  NeuronType, Direction)
from neural_world.neural_network_engine import NeuralNetworkEngine, MINIMAL_NEURON_ID


LOGGER = commons.logger(commons.SUBLOGGER_LIFE)


class NeuralNetwork(NeuralNetworkEngine):
    """Definition of the neural network behaviors.

    About the memory:

    The input memory neurons are named input because they inject
    the memory value inside the network.
    The output memory neurons are named output because they are output
    of the neural running.

    When the neural network is ran, memory input neurons are mapped to up
    or down accordingly to the memory values.
    When an output memory neuron is up after the run, the memory value at the