예제 #1
0
    def _affinexp_pic2scip(self, picosExpression):
        """
        Tranforms a PICOS (multidimensional) affine expression into a list of
        SCIP (scalar) expressions.

        :returns: A :class:`list` of :class:`SCIP expressions
            <pyscipopt.scip.Expr>`.
        """
        from pyscipopt.scip import Expr

        if picosExpression is None:
            return [Expr()]

        length = len(picosExpression)

        # Convert linear part.
        scipExpressions = [Expr() for _ in range(length)]
        for picosVar, picosCoefs in picosExpression.factors.items():
            startIndex = picosVar.startIndex
            for localIndex, localVarIndex, coefficient in zip(
                    picosCoefs.I, picosCoefs.J, picosCoefs.V):
                scipVar = self._scipVar[startIndex + localVarIndex]
                scipExpressions[localIndex] += coefficient * scipVar

        # Convert constant part.
        if picosExpression.constant:
            for i in range(length):
                scipExpressions[i] += picosExpression.constant[i]

        return scipExpressions
예제 #2
0
def encode_reward(model, A, S, Aux, x, y, v, d, reward, horizon):
    
    sum_expr = Expr()
    empty = True
    for t in range(horizon):
        for t_index, term in enumerate(reward[0]):
            coef = "1.0"
            variables = term.split("*")
            if variables[0] not in A + S + Aux:
                coef = variables[0]
                variables = variables[1:]
            if set(A).isdisjoint(variables) or t < horizon:
                prod_expr = ProdExpr()
                for var in variables:
                    if var in A:
                        prod_expr *= VarExpr(x[(var,t)])
                    elif var in S:
                        prod_expr *= VarExpr(y[(var,t)])
                    else:
                        prod_expr *= VarExpr(v[(var,t)])
            
                d[(t_index,t)] = model.addVar("rew_" + str(t_index) + "_" + str(t))
                model.addCons(prod_expr == d[(t_index,t)])
                sum_expr += float(coef) * d[(t_index,t)]
                empty = False
        
    if not empty:
        #print(sum_expr)
        model.setObjective(sum_expr, "maximize")
    
    return model
예제 #3
0
def test_degree(model):
    m, x, y, z = model
    expr = Expr()
    assert expr.degree() == 0

    expr = Expr() + 3.0
    assert expr.degree() == 0

    expr = x + 1
    assert expr.degree() == 1

    expr = x * x + y - 2
    assert expr.degree() == 2

    expr = (x + 1) * (y + 1) * (x - 1)
    assert expr.degree() == 3
예제 #4
0
def encode_initial_constraints(model, S, y, initials):
    
    for init in initials:
        variables = init[:-2]
        init_expr = Expr()
        for var in variables:
            coef = "1.0"
            if "*" in var:
                coef, var = var.split("*")
            init_expr += float(coef) * y[(var,0)]

        model.addCons(init_expr == float(init[len(init)-1]))

    return model
예제 #5
0
def test_degree(model):
    m, x, y, z = model
    expr = Expr()
    assert expr.degree() == 0

    expr = Expr() + 3.0
    assert expr.degree() == 0

    expr = x + 1
    assert expr.degree() == 1

    expr = x*x + y - 2
    assert expr.degree() == 2

    expr = (x + 1)*(y + 1)*(x - 1)
    assert expr.degree() == 3