pythonoptimizationlookupcvxpy

Look-up based objective function in Cvxpy


I'd like to define an objective function based on a lookup table. As an example, we have an integer programming problem (binary) where we can pick one or more actions from a set of actions. Our cvxpy.Variable is:

element_vars = [1, 0, 0, 1] (p.s. element_vars is a Cvxpy placeholder with no value, but I put some numbers here for demsontartion.)

And the set of actions to pick is:

actions = [2, 3, 4, 5]

The objective function depends based on "total actions" (not single actions) which is calculated as:

total_actions = cvxpy.matmul(element_vars, actions.T) = [2+5] = [7]

The objective value is picked from a look-up table based on "total_actions" value. For example, if the look-up table is:

lookup_table = [[2, 3, 4, 5, 6, 7, 8, 9],
                [1, 2, 6, 8, 3, 4, 8,  9]]

Where the first row shows "total_actions" and the second row shows its corresponding objective value.

In this example, the objective value for total_actions=7 is 4.

Since element_vars is a placeholder, and it only exists inside the CVXPy environment, I can not use Pandas, df, or NumPy functions. I tried the below code

total_actions = cvxpy.matmul(actions, element_vars)
self.objective_terms.append(cvxpy.multiply(lookup_table[np.where(lookup_table[:,0] == total_actions),1], weight))

But, I get the below error

Error in python process: At line 23: <class 'Exception'>: Cannot evaluate the truth value of a constraint or chain constraints, e.g., 1 >= x >= 0.

I also tried to pass the lookup_table as a Cvxpy parameter, but no progress. I appreciate it if you have any idea how to process an array (look for a value inside an array and return the value or index) in the CVXPy environment.


Solution

  • I had a chance of chatting with Steve Diamond, the developer of CVXPy. As he mentioned here, CVXPy could not perform any indexing/look-up action. If your objective function is linear, you may use the approximation function which is developed here. In my case, I changed the structure of the problem to be able to use CVXPy.