Monday, 6 September 2021

Inconsistency in solutions using CVXPY

Please, consider the following optimisation problem. Specifically, x and b are (1,n) vectors, C is (n,n) symmetric matrix, k is an arbitrary constant and i is a (1,n) vector of ones.

Please, also consider the following equivalent optimisation problem. In such case, k is determined during the optimisation process so there is no need to scale the values in x to obtain the solution y.

Please, also consider the following code for solving both the problems with cvxpy.

import cvxpy as cp
import numpy as np

    def problem_1(C):
        n, t = np.shape(C)
        
        x = cp.Variable(n)
        b = np.array([1 / n] * n)
        
        obj =  cp.quad_form(x, C)
        constraints = [b.T @ cp.log(x)>=0.5, w >= 0]
        cp.Problem(cp.Minimize(obj), constraints).solve()
    
        return (x.value / (np.ones(n).T @ x.value))
    
    def problem_2(C):
        n, t = np.shape(C)
        
        y = cp.Variable(n)
        k = cp.Variable()
        b = np.array([1 / n] * n)
        
        obj =  cp.quad_form(y, C)
        constraints = [b.T @ cp.log(y)>=k, np.ones(n)@y.T==1, y >= 0]
        cp.Problem(cp.Minimize(obj), constraints).solve()
        
        return y.value

While the first function do provide me with the correct solution for a sample set of data I am using, the second does not. Specifically, values in y differ heavily while employing the second function with some of them being equal to zero (which cannot be since all values in b are positive and greater than zero). I am wondering wether or not the second function minimise also k. Its value should not be minimised on the contrary it should just be determined during the optimisation problem as the one that leads to the solution that minimise the objective function.

UPDATE_1

I just found that the solution that I obtain with the second formulation of the problem is equal to the one derived with the following function. It appears that the constraint with the logarithmic barrier and the k variable is ignored.

def problem_3(C):
    n, t = np.shape(C)
    
    y = cp.Variable(n)
    k = cp.Variable()
    b = np.array([1 / n] * n)
    
    obj =  cp.quad_form(y, C)
    constraints = np.ones(n)@y.T==1, y >= 0]
    cp.Problem(cp.Minimize(obj), constraints).solve()
    
    return y.value


from Inconsistency in solutions using CVXPY

No comments:

Post a Comment