import cvxpy as cvx import numpy n = 6; # number of variables (chosen because it can print on one line in my moniter) m = 10; # number of constraints. These are included so that the problem is not unbounded # generate some constraints numpy.random.seed(0) # for repeatibility A = numpy.random.randn(m,n) b = 10 * numpy.random.random([m,1]) # and the parameter p = cvx.Parameter(1,n) p.value = numpy.random.random([1,n]) # returnes values in the interval [0.0, 1.0) x = cvx.Variable(n,1) # (n,1) will give a colum vector objective = cvx.Maximize(p * cvx.sqrt(x)) constraint = [A*x <= b] problem = cvx.Problem(objective, constraint) # Try solving the problem. Print error if any. try: problem.solve() except Exception as e: print e print "objective.is_dcp(): ", objective.is_dcp() print "p * cvx.sqrt(x) curvature: ", (p * cvx.sqrt(x)).curvature print "\np value: ", p.value print "p >= 0: ", p.value >= 0 print "sqrt(x) curvature: ", cvx.sqrt(x).curvature print "p:" print "sign property: ", p.sign print "value property >= 0: ", p.value >= 0 q = cvx.Parameter(1,n,nonneg=True) print "q:" print "sign property: ", q.sign try: print q.value except Exception as e: print e negative_values = numpy.array([range(n)]) - n print "Values to assign to q: ", negative_values try: q.value = numpy.array([range(n)]) - n except Exception as e: print e print "\nSetting the sign of p for cvx:" try: p.sign = 'positive' except Exception as e: print e temp_p_value = p.value p = cvx.Parameter(1,n,nonneg=True) p.value = temp_p_value print p.value print p.sign objective = cvx.Maximize(p * cvx.sqrt(x)) problem = cvx.Problem(objective, constraint) print "DCP rules test: ", problem.is_dcp() try: print "solving... " problem.solve() except Exception as e: print(e) # print some stuff print problem.status print "Optimal value: ", problem.value print "\nx:\n", x.value print "\nResidual of constraints:\n", A.dot(x.value) - b