Esempio n. 1
0
def solve2(objective, constraint, step, limit):
    """ Solve a maximization problem for 2 states.

    :param objective: The objective function.
     :type objective: function

    :param constraint: A tuple representing the constraint.
     :type constraint: tuple(function, function, number)

    :param step: The step size.
     :type step: number,>0

    :param limit: The maximum value of the variables.
     :type limit: number,>0

    :return: The problem solution.
     :rtype: list(number)
    """
    res_best = 0
    solution = []
    for x in frange(0, limit, step):
        for y in frange(0, limit, step):
            try:
                res = objective(x, y)
                if res > res_best and \
                    constraint[1](constraint[0](x, y), constraint[2]):
                        res_best = res
                        solution = [x, y]
            except ZeroDivisionError:
                pass
    return solution
Esempio n. 2
0
 def test_frange(self):
     self.assertEqual([round(x, 1) for x in common.frange(0, 1.0, 0.5)],
                      [0.0, 0.5, 1.0])
     self.assertEqual([round(x, 1) for x in common.frange(0, 1.0, 0.2)],
                      [0.0, 0.2, 0.4, 0.6, 0.8, 1.0])
 def test_frange(self):
     self.assertEqual([round(x, 1) for x in common.frange(0, 1.0, 0.5)],
                      [0.0, 0.5, 1.0])
     self.assertEqual([round(x, 1) for x in common.frange(0, 1.0, 0.2)],
                      [0.0, 0.2, 0.4, 0.6, 0.8, 1.0])