def test_distance(self): """Test distance.""" for i in range(20): point = matrix(random.random(3)) assert 0 == distance(point, point) pa = matrix([5, 3, 4]) pb = matrix([10, 3, 4]) assert 5 == distance(pa, pb) pa = matrix([5, 3, 4]) pb = matrix([5, 7, 4]) assert 4 == distance(pa, pb) pa = matrix([5, 3, 4]) pb = matrix([5, 3, 11]) assert 7 == distance(pa, pb)
def test_generate_sequential_points(self): """Test generate sequential points.""" gen = generate_sequential_points(matrix([0, 0, 0]), random) point = next(gen) for i in range(30): next_point = next(gen) assert distance(point, next_point) < 0.87 point = next_point
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 ))
side_length=side_length, num_of_points=1) base = next(base_generator) point = next(point_generator) random_generator = Random() # Load logging configuration log_config = "examples/benchmark_1.log.json" with open(log_config) as data_file: data = json.load(data_file) logging.config.dictConfig(data) logger = logging.getLogger("benchmark") for index, (param, engine) in enumerate( builder(base, point, params, random_generator, around_beacons)): engine.start() engine.join() bestParticle = engine.environment.neighborBest best_position = bestParticle.best.position error = distance(point, best_position) fitness = bestParticle.best.fitness logger.debug( "{} {:.3f} {:.3f} {:.3f} {:.3f} {:.3f} {:.3f} {:.3f} {:.3f}".format( index, param[0], param[1], param[2], param[3], param[4], param[5], param[6], error))
center=center, side_length=side_length, num_of_points=1) base = next(base_generator) point = next(point_generator) random_generator = Random() # Load logging configuration log_config = "examples/benchmark_1.log.json" with open(log_config) as data_file: data = json.load(data_file) logging.config.dictConfig(data) logger = logging.getLogger("benchmark") for index, (param, engine) in enumerate(builder(base, point, params, random_generator, around_beacons)): engine.start() engine.join() bestParticle = engine.environment.neighborBest best_position = bestParticle.best.position error = distance(point, best_position) fitness = bestParticle.best.fitness logger.debug( "{} {:.3f} {:.3f} {:.3f} {:.3f} {:.3f} {:.3f} {:.3f} {:.3f}".format( index, param[0], param[1], param[2], param[3], param[4], param[5], param[6], error))
random_generator = Random() center = [1000, 1000, 1000] side_length = 100 base_generator = generate_matrix_of_points_in_cube( center=center, side_length=side_length, num_of_points=4) point_generator = generate_matrix_of_points_in_cube( center=center, side_length=side_length, num_of_points=1) base = next(base_generator) point = next(point_generator) error = 10 disturbed_point_gen = apply_noise_linear(point=point, error=error) radius = matrix([distance(b, next(disturbed_point_gen)) for b in base]) env_config = { 'inertial_weight': 2, 'cognition': 2, 'social': 1, 'random': random_generator, 'base': base, 'radius': radius } env1 = Environment(config=env_config) position_generator = around_beacons(env1) vmax = 5
center = [1000, 1000, 1000] side_length = 100 base_generator = generate_matrix_of_points_in_cube(center=center, side_length=side_length, num_of_points=4) point_generator = generate_matrix_of_points_in_cube(center=center, side_length=side_length, num_of_points=1) base = next(base_generator) point = next(point_generator) error = 10 disturbed_point_gen = apply_noise_linear(point=point, error=error) radius = matrix([distance(b, next(disturbed_point_gen)) for b in base]) env_config = { 'inertial_weight': 2, 'cognition': 2, 'social': 1, 'random': random_generator, 'base': base, 'radius': radius } env1 = Environment(config=env_config) position_generator = around_beacons(env1) vmax = 5