Ejemplo n.º 1
0
def main():
    # Create the linear solver with the GLOP backend.
    solver = Solver('simple_lp_program', Solver.GLOP_LINEAR_PROGRAMMING)

    # Create the variables x and y.
    x = solver.NumVar(0, 1, 'x')
    y = solver.NumVar(0, 2, 'y')

    print('Number of variables =', solver.NumVariables())

    # Create a linear constraint, 0 <= x + y <= 2.
    ct = solver.Constraint(0, 2, 'ct')
    ct.SetCoefficient(x, 1)
    ct.SetCoefficient(y, 1)

    print('Number of constraints =', solver.NumConstraints())

    # Create the objective function, 3 * x + y.
    objective = solver.Objective()
    objective.SetCoefficient(x, 3)
    objective.SetCoefficient(y, 1)
    objective.SetMaximization()

    solver.Solve()

    print('Solution:')
    print('Objective value =', objective.Value())
    print('x =', x.solution_value())
    print('y =', y.solution_value())
Ejemplo n.º 2
0
def find_transform(x, y, limit, threshold=1e-7):
    """
    find a integer solution to ax + b - cxy - dy = 0
    this will give us the mobius transform: T(x) = y
    :param x: numeric constant to check
    :param y: numeric manipulation of constant
    :param limit: range to look at
    :param threshold: optimal solution threshold.
    :return MobiusTransform in case of success or None.
    """
    x1 = x
    x2 = dec(1.0)
    x3 = -x * y
    x4 = -y
    solver = Solver('mobius', Solver.CBC_MIXED_INTEGER_PROGRAMMING)
    a = solver.IntVar(-limit, limit, 'a')
    b = solver.IntVar(-limit, limit, 'b')
    c = solver.IntVar(-limit, limit, 'c')
    d = solver.IntVar(-limit, limit, 'd')
    f = solver.NumVar(0, 1, 'f')
    solver.Add(f == (a * x1 + b * x2 + c * x3 + d * x4))
    solver.Add(a * x1 + b >=
               1)  # don't except trivial solutions and remove some redundancy
    solver.Minimize(f)
    status = solver.Solve()
    if status == Solver.OPTIMAL:
        if abs(solver.Objective().Value()) <= threshold:
            res_a, res_b, res_c, res_d = int(a.solution_value()), int(b.solution_value()),\
                                         int(c.solution_value()), int(d.solution_value())
            ret = MobiusTransform(
                np.array([[res_a, res_b], [res_c, res_d]], dtype=object))
            ret.normalize()
            return ret
    else:
        return None
Ejemplo n.º 3
0
def _parse_objective_function(solver: pywraplp.Solver, core_line: str,
                              line_nr: int, var_names: set):
    spl_colon = core_line.split(":", maxsplit=1)

    objective = solver.Objective()

    # Set maximization / minimization if specified
    if len(spl_colon) == 1:
        raise ValueError(
            "Objective function on line %d must start with \"max:\" or \"min:\"."
            % line_nr)
    else:
        if spl_colon[0] != "max" and spl_colon[0] != "min":
            raise ValueError(
                "Objective function on line %d must start with \"max:\" or \"min:\"."
                % line_nr)
        elif spl_colon[0] == "max":
            objective.SetMaximization()
        else:
            objective.SetMinimization()

        # Set the remainder
        coefficient_part = spl_colon[1].strip()

        # Finally set the coefficients of the objective
        constant = _set_coefficients(solver, objective, coefficient_part,
                                     line_nr, var_names)
        objective.SetOffset(constant)