Beispiel #1
0
def solve(A, rhs, ct=None, logLevel=0):
    s = CyClpSimplex()
    s.logLevel = logLevel
    lp_dim = A.shape[1]

    x = s.addVariable('x', lp_dim)
    A = np.matrix(A)
    rhs = CyLPArray(rhs)

    s += A * x >= rhs

    s += x[lp_dim - 1] >= 0
    s.objective = x[lp_dim - 1]
    nnz = np.count_nonzero(A)
    logging.debug(f"TASK SIZE XCOUNT: {lp_dim} GXCOUNT: {len(rhs)}")

    s.primal()
    k = list(s.primalConstraintSolution.keys())[0]
    k2 = list(s.dualConstraintSolution.keys())[0]
    q = s.dualConstraintSolution[k2]
    logging.debug(f"{s.getStatusString()} objective: {s.objectiveValue}")
    logging.debug(
        f"nonzeros rhs: {np.count_nonzero(s.primalConstraintSolution[k])}")
    logging.debug(
        f"nonzeros dual: {np.count_nonzero(s.dualConstraintSolution[k2])}")

    basis = s.getBasisStatus()
    print("Basis[0]")
    print(basis[0])
    print("basis[1]")
    print(basis[1])
    np.savetxt("out", basis[1])

    print("#" * 100)

    s = CyClpSimplex()
    s.logLevel = logLevel
    lp_dim = A.shape[1]

    x = s.addVariable('x', lp_dim)
    A = np.matrix(A)
    rhs = CyLPArray(rhs)

    s += A * x >= rhs

    s += x[lp_dim - 1] >= 0
    s.objective = x[lp_dim - 1]
    nnz = np.count_nonzero(A)
    logging.debug(f"TASK SIZE XCOUNT: {lp_dim} GXCOUNT: {len(rhs)}")

    s.setBasisStatus(*basis)
    s.primal()

    return s.primalVariableSolution['x']
    def _base_branch(self: T, idx: int) -> Dict[str, T]:
        """ Creates two new copies of the node with new bounds placed on the variable
        with index <idx>, one with the variable's lower bound set to the ceiling
        of its current value and another with the variable's upper bound set to
        the floor of its current value.

        :param idx: index of variable to branch on

        :return: dict of Nodes with the new bounds keyed by direction they branched
        """
        assert self.lp_feasible, 'must solve before branching'
        assert idx in self._integerIndices, 'must branch on integer index'
        b_val = self.solution[idx]
        assert self._is_fractional(
            b_val), "index branched on must be fractional"

        # get end basis to warm start the kiddos
        # appears to be tuple  (variable statuses, slack statuses)
        basis = self._lp.getBasisStatus()

        # when branching down set floor as upper bound for given index
        u = self._lp.variablesUpper.copy()
        u[idx] = floor(b_val)
        down_lp = CyClpSimplex()
        down_lp.loadProblem(self._lp.matrix, self._lp.variablesLower, u,
                            self._lp.objective, self._lp.constraintsLower,
                            self._lp.constraintsUpper)
        down_lp.setBasisStatus(*basis)  # warm start

        # when branching up set ceiling as lower bound for given index
        l = self._lp.variablesLower.copy()
        l[idx] = ceil(b_val)
        up_lp = CyClpSimplex()
        up_lp.loadProblem(self._lp.matrix, l, self._lp.variablesUpper,
                          self._lp.objective, self._lp.constraintsLower,
                          self._lp.constraintsUpper)
        up_lp.setBasisStatus(*basis)  # warm start

        # return instances of the subclass that calls this function
        return {
            'down':
            type(self)(down_lp, self._integerIndices, self.objective_value,
                       idx, 'down', b_val, self.depth + 1),
            'up':
            type(self)(up_lp, self._integerIndices, self.objective_value, idx,
                       'up', b_val, self.depth + 1)
        }