Affine Transformation for objective function
回答済みMy goal is to minimize the norm between the solution and a given matrix, I figured the best way to express it is via np.linalg.norm(x - ref_matrix). Essentially I want the solution matrix to deviate the least possible from a given matrix.
However, the command x - ref_matrix is not valid and I don't know how to implement such logic
ref_matrix = np.random.rand(79,682)
m = gp.Model()
x = m.addMVar(ref_matrix.shape, lb=0.0, ub=1.0)
for i in range(x.shape[0]):
m.addConstr(x[i, :].sum() <= 1, name='date' + str(i))
m.setObjective(np.linalg.norm(x - ref_matrix), sense=gp.GRB.MINIMIZE)
m.optimize()
This will yield the following error:
gurobipy.GurobiError: Variable is not a 1D MVar object
0
-
正式なコメント
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
You can introduce auxiliary variables \( Y \) equal to the difference between \( X \) and the reference matrix \( A \), then minimize \( ||Y||_F^2 \) by slicing the \( Y \) variables appropriately. For example:
import gurobipy as gp
from gurobipy import GRB
import numpy as np
n1 = 79
n2 = 682
A = np.random.rand(n1, n2)
m = gp.Model()
X = m.addMVar(shape=(n1, n2), ub=1.0, name='X')
Y = m.addMVar(shape=(n1, n2), lb=-GRB.INFINITY, name='Y')
# set Y = A - X
m.addConstrs((Y[i, :] == X[i, :] - A[i, :] for i in range(n1)), name='set_Y')
# "date" constraints
m.addConstrs((X[i, :].sum() <= 1 for i in range(n1)), name='date')
# minimize ||Y||_F^2
m.setObjective(sum(Y[i, :] @ Y[i, :] for i in range(n1)), GRB.MINIMIZE)
m.optimize()Minimizing \( ||Y||_F^2 \) is equivalent to minimizing \( ||Y||_F \).
0
投稿コメントは受け付けていません。
コメント
2件のコメント