def test_random_generator(self, fix_optimization_object_kwargs,
                              fix_optimization_object_kwargs_with_seed):
        """Test if random_generator works, and if given it a seed, ensure
        random_generator return the same randomize numbers for each
        constructed object."""

        opt_object = Optimization(**fix_optimization_object_kwargs)
        is_different = False
        for i in range(100):
            rand_val_1 = opt_object.random_generator.random()
            rand_val_2 = opt_object.random_generator.random()
            is_different |= rand_val_1 != rand_val_2
        assert is_different

        opt_object_custom_seed_1 = \
            Optimization(**fix_optimization_object_kwargs_with_seed)
        rand_val_3 = opt_object_custom_seed_1.random_generator.random()
        rand_val_4 = opt_object_custom_seed_1.random_generator.random()

        opt_object_custom_seed_2 = \
            Optimization(**fix_optimization_object_kwargs_with_seed)
        rand_val_5 = opt_object_custom_seed_2.random_generator.random()
        rand_val_6 = opt_object_custom_seed_2.random_generator.random()

        assert rand_val_3 == rand_val_5 and rand_val_4 == rand_val_6
    def test___repr__(self, fix_optimization_object_kwargs,
                      fix_optimization_constraint_1):
        """Test __repr__ function."""

        opt_object = Optimization(**fix_optimization_object_kwargs)
        opt_object.add_constraint(fix_optimization_constraint_1)
        print_string = opt_object.__repr__()
        print_string.find("x[0] ** 4 + x[0] ** 3 - 3 * (x[0] ** 2) + 1")
        print_string.find("x0: (-3, 3)")
        print_string.find("x[0]**4 + x[0]**3 - 2*(x[0] ** 2) + 2 >= 0")
    def test_check_constraints(self, fix_optimization_object_kwargs,
                               fix_optimization_constraint_1,
                               fix_optimization_constraint_2):
        """
        Test check constraints function work.

        Success cases:
        - Boundaries check work
        - Constraints check work
        """

        opt_object = Optimization(**fix_optimization_object_kwargs)
        opt_object.add_constraint(fix_optimization_constraint_1)
        opt_object.add_constraint(fix_optimization_constraint_2)

        # Boundaries fail check
        assert not opt_object.check_constraints([-4])

        # Constraint 1 fail check
        assert not opt_object.check_constraints([-1.6])

        # Constraint 2 fail check
        assert not opt_object.check_constraints([0.0])

        # Satisfy all check
        assert opt_object.check_constraints([-1.8])
        assert opt_object.check_constraints([0.9])
    def test___init__(self, fix_optimization_object_kwargs):
        """Test __init__() constructor function."""

        opt_object = Optimization(**fix_optimization_object_kwargs)
        assert opt_object.func == \
            fix_optimization_object_kwargs['optimizing_function']
        assert opt_object.no_dimensions == \
            fix_optimization_object_kwargs['no_dimensions']
        assert opt_object.boundaries == \
            fix_optimization_object_kwargs['boundaries']
        assert opt_object.find_max == \
            fix_optimization_object_kwargs['find_max']
Beispiel #5
0
 def run(self, **kwargs):
     no_particles = kwargs.get('no_particles', 10)
     no_iterations = kwargs.get('no_iterations', 20)
     c_1 = kwargs.get('c_1', 2.0)
     c_2 = kwargs.get('c_2', 2.0)
     opt_obj = Optimization(optimizing_function=self._optimizing_function,
                            boundaries=[(0, self.max_particles),
                                        (0, self.max_iteration),
                                        (0, self.max_learning_factor),
                                        (0, self.max_learning_factor)],
                            no_dimensions=4,
                            find_max=False)
     opt_obj.add_constraint(self._constraint_function)
     pso = PSO(optimization_object=opt_obj,
               no_particles=no_particles,
               no_iteration_steps=no_iterations,
               c_1=c_1,
               c_2=c_2,
               verbose=True)
     ms = MultipleSolving(algorithm_obj=pso,
                          no_tries=kwargs.get('no_tries', 5))
     max_thread = kwargs.get('max_thread', 0)
     ms.run(pool=Pool(max_thread if max_thread else None))
     return ms
    def test_add_constraints(self, fix_optimization_object_kwargs,
                             fix_optimization_constraint_1,
                             fix_optimization_constraint_2):
        """Test if add_constraint() work."""

        opt_object = Optimization(**fix_optimization_object_kwargs)
        opt_object.add_constraint(fix_optimization_constraint_1)
        assert opt_object.constraints.__len__() == 1
        opt_object.add_constraint(fix_optimization_constraint_2)
        assert opt_object.constraints.__len__() == 2
Beispiel #7
0
    x[2]: 2nd year gaz
    x[3]: 2nd year gaz
    """

    return x[0] * 40 + x[1] * 50


def constraint(x):
    return (x[0] + x[1] <= 50) & \
           (x[0] * 3 + x[1] * 4 <= 180) & \
           (x[1] <= 40)


optimization = Optimization(
    optimizing_function=optimizing_function,
    boundaries=[(0, 100), (0, 100)],
    no_dimensions=2,
    find_max=True
)

optimization.add_constraint(constraint)

pso = PSO(
    optimization_object=optimization,
    no_particles=10,
    no_iteration_steps=20,
    c_1=2.0,
    c_2=2.0
)

ms = MultipleSolving(pso, 20)
ms.run()
Beispiel #8
0
def fix_optimization_object():
    kwargs = fix_optimization_object_kwargs()
    opt_object = Optimization(**kwargs)
    opt_object.add_constraint(fix_optimization_constraint_1())
    opt_object.add_constraint(fix_optimization_constraint_2())
    return opt_object
Beispiel #9
0
year_2 = ProducePeriod(
    resources=[50, 180, 40],
    predicted_prices=[40, 50],
    profit_calculator=profit_calculator_1st,
    last_period=year_1
)
year_2.add_consumer(consumer_1st_n)
year_2.add_consumer(consumer_1st_h)
year_2.add_consumer(consumer_1st_ch)

optimization_2_years = Optimization(
    optimizing_function=year_2.calculate_profit,
    boundaries=[
        (5, 100.0),
        (5, 100.0),
        (5, 100.0),
        (5, 100.0),
    ],
    no_dimensions=4,
    find_max=True
)
optimization_2_years.add_constraint(year_2.check_consumption)

pso_1st = PSO(
    optimization_object=optimization_2_years,
    no_particles=40,
    no_iteration_steps=20,
    c_1=2.0,
    c_2=2.0
)
print("Result for 1st case:")