Dear all,
I installed Gurobi via Anaconda on Python 3.5, and just to learn how it works, I tried to solve a simple least squares problem without constraints. ||Ax -b||**2
Given are mxn matrix A and target-vector b with length m, and I just want to calculate the optimal x. This can be easily done in Numpy and Scipy (in fact A and b are Numpy arrays), however, I want to understand the gurubi Python syntax for this simple case before I add quadratic constraints in the next step.
I started with
m = Model("test")
m.addVar(vtype=GRB.CONTINUOUS, name='w')
How would I add the matrix as a parameter? Can I use or convert the Numpy arrays directly?
Thank youfrom gurobipy import *import numpy as np
m = 60n = 30
A = np.random.rand(n, m)-0.5b = np.random.rand(m) - 0.5mod = Model()
# build variablesz = [mod.addVar(lb=-GRB.INFINITY) for j in range(m)]x = [mod.addVar(lb=-GRB.INFINITY) for i in range(n)]# update model with variable changesmod.update()
# build constraintszDefConst = [mod.addConstr(z[j],GRB.EQUAL,quicksum([A[i,j]*x[i] for i in range(n)])-b[j]) for j in range(m)]
# build objectivemod.setObjective(quicksum([z[j]*z[j] for j in range(m)]))
# update model with constraints and objectivemod.update()
# execute optimization algorithmmod.optimize()
# extract x valuesbestX = [x[i].X for i in range(n)]
# print the solutionprint bestX, mod.objvalThank you very much!
Your example works, however, if I change A and b to a complex matrix and vector, respectively,
A = np.random.rand(n, m)-0.5 + 1j* np.random.rand(n, m)
b = np.random.rand(m) - 0.5 +1j*np.random.rand(m)
I get the following error message:
GurobiError: Invalid argument to LinExpr multiplication
It seems that the quicksum function does not accept complex numbers?
I wonder if the problem can be converted into a real one?
Does Gurobi support primal-dual interior point methods? This is where I wanted to get at. I need to solve the stated least squares problem with quadratic matrix inequality constraints. (all complex matrices)
Thank you very much. Indeed, the Q are psd, I made sure of that. The reason I attempt this problem is because I know that it is feasible and solvable, however, I want to verify it myself.The only way to do this with the Gurobi solver, is to somehow convert the complex problem into a real one?Thank you