Search code examples
optimizationgurobicvxpycvxopt

How can I use a CVX variable in a Numpy product that is to be Minimized?


I'm trying to optimize a configuration X (boolean), such that the total price : base_price + discount, on a configuration is minimized, but the problem formulation gives a Matmul error since x is a cvxpy Variable and thus doesn't conform to the Numpy shape even though it was defined with the correct length.

n = len(Configuration)
x = cp.Variable(n, boolean=True)
problem = cp.Problem(cp.Minimize(base_price + price@(price_rules_A@x <= price_rules_B)), [
        config_rules_A@x <= config_rules_B, 
        config_rules_2A@x == config_rules_2B
    ])
# where price@(price_rules_A@x <= price_rules_B) is the total discount 
# and price, price_rules_A and price_rules_B are numpy arrays 

The error i get is

ValueError: matmul: Input operand 1 does not have enough dimensions (has 0, gufunc core with signature (n?,k),(k,m?)->(n?,m?) requires 1)

I expect it to find an optimal config for x ( 0010110...) such that the discount is minimized but it doesn't. Any idea what might be causing this?


Solution

  • Assuming the evaluation of the inequality in the objective function is suppose to work as index to price, you can rewrite the function as

    cp.Minimize(base_price + price@(1-(price_rules_B - price_rules_A@x))
    

    Then the elements in price where the inequality is true will be summed.