Esempio n. 1
0
def builder(base, point, params, random_generator, position_initializator):
    """Sistematically generate environments from parameters combination."""
    index = 1
    for param in parameters_matrix(params):
        # read parameters from the matrix
        error = param[0]
        inertial_weight = param[1]
        cognition = param[2]
        social = param[3]
        num_particles = param[4]
        velocity_max = param[5]
        iterations_per_particle = param[6]
        # point disturbed by a gaussian noise
        disturbed_point_gen = apply_noise_linear(point=point, error=error)
        # compute radius
        radius = matrix([distance(b, next(disturbed_point_gen)) for b in base])
        # build environment configuration
        env_config = {
            'inertial_weight': inertial_weight,
            'cognition': cognition,
            'social': social,
            'random': random_generator,
            'base': base,
            'radius': radius
        }
        env = Environment(config=env_config)
        # particle position generator
        position_generator = position_initializator(env)
        # particles inside env
        for i in range(num_particles):
            # particles
            PSOParticle(
                environment=env,
                id="P{}env{}".format(i, index),
                current=Information(position=next(position_generator)),
                velocity=velocity_max * random_generator.random(),
                vmax=velocity_max
            )

        # engine for env
        yield (param, ParticleEngine(
            config={
                'max_iterations': iterations_per_particle
            },
            environment=env
        ))
        id="P{}env2".format(i),
        current=Information(position=next(position_generator)),
        velocity=random_generator.random(),
        vmax=5
    )

# Load logging configuration
config_file = "examples/followbest_particles.json"
with open(config_file) as data_file:
    data = json.load(data_file)
logging.config.dictConfig(data)

# engine1 for env1
engine1 = ParticleEngine(
    config={
        'max_iterations': 40
    },
    environment=env1
)
engine1.start()
# engine2 for env2
engine2 = ParticleEngine(
    config={
        'max_iterations': 40
    },
    environment=env2
)
engine2.start()

# wait engines
engine1.join()
engine2.join()
Esempio n. 3
0
    matrix([
        3.024,  # beacon 1
        6.4,  # beacon 2
        6.63,  # beacon 3
    ])
}
env = Environment(config=config)

PSOParticle(environment=env,
            id="P1",
            current=Information(position=config['base'][0]),
            velocity=matrix([0.1, 0.1, 0.1]),
            vmax=3)
PSOParticle(environment=env,
            id="P2",
            current=Information(position=config['base'][1]),
            vmax=10)
PSOParticle(environment=env,
            id="P3",
            current=Information(position=config['base'][2]),
            vmax=10)

logging.basicConfig(filename='logs/find_point.log', level=logging.DEBUG)

engine = ParticleEngine(config={'max_iterations': 100}, environment=env)
engine.start()
engine.join()

bestParticle = env.best
print("Object located: ", bestParticle.best.position)
        environment=env,
        id="P{}env1".format(i + total),
        current=Information(position=next(position_generator_2)),
        velocity=random_generator.random(),
        vmax=1,
    )
    # start around beacon 3
    PSOParticle(
        environment=env,
        id="P{}env1".format(i + total * 2),
        current=Information(position=next(position_generator_3)),
        velocity=random_generator.random(),
        vmax=1,
    )
    # start around beacon 4
    FollowBestParticle(
        environment=env,
        id="P{}env1".format(i + total * 3),
        current=Information(position=next(position_generator_4)),
        velocity=random_generator.random(),
        vmax=1,
    )

# start engine
engine = ParticleEngine(config={"max_iterations": 70}, environment=env)
engine.start()
engine.join()

bestParticle = env.best
print("Object located: ", bestParticle.best.position)