I have CVXPY problem defined with a volume array, and a cost array to match each of volumes. The problem has 192 variables and 3 constraints which I have defined.
My goal is to minimize the cost in this problem to deliver a specific volume and avoid multiple periods where I get a 0, 1, 0, 1
.
My current output could look something like follows:
[0, 0, 1, 1, 0, 1... 0, 1, 0, 1]
The ideal solution would avoid an amount. So if the selection decides a 1 at a point, the next 2 points should be 0. Such as below:
[0, 0, 1, 1, 0, 0... 0, 1, 0, 0]
I am unsure how to write such a constraint to include my selection with the problem I have currently programmed as can be seen here:
import cvxpy as cp
import numpy as np
# Volume and cost
full_cost = [[0, data] for data in [0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45,0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4]]
cost_ = np.array(full_cost)
ex = np.array([[0, 17100] for data in [i for i in range(0, 96)]])
# Minimum volume required
v_min = 300000
# Selection variable
selection = cp.Variable(shape=ex.shape, boolean=True)
# Constraints
assignment_constraint = cp.sum(selection,axis=1) == 1
volume_= cp.sum(cp.multiply(ex,selection))
volume_constraint = volume_ >= v_min
cost_constraint = cp.sum(cp.multiply(cost_, selection))
constraints = [assignment_constraint, volume_constraint, cost_constraint]
cost_ = cp.sum(cp.multiply(cost_,selection))
# Problem definition
assign_problem = cp.Problem(cp.Minimize(cost_), constraints)
assign_problem.solve(solver=cp.CPLEX, verbose=True)
# Find solution in ex variable
assignments = [np.where(r==1)[0][0] for r in selection.value]
c = [ ex[i][assignments[i]] for i in range(len(assignments)) ]
best_volume = np.sum(np.multiply(ex,selection.value))
best_cost = np.sum(np.multiply(cost_,selection.value))
print(best_cost)
print(c)
I believe that the constraint should be based around my selection variable, but I am struggling to see how to include it as a constraint.