Error "Unable to resolve the name gurobi.Model."
AnsweredHello,
I'm using Gurobi to solve the problem of min-cost flow on graphs with radiality contraints. Here is the code that I'm using :
n = 4;
R = [1];
% Define the isconnected function
isconnected = @(G) numel(unique(conncomp(G))) == 1;
% Set the connectivity ratio of the graph
p = 1;
% Generate a random adjacency matrix
AA = triu(rand(n) < p, 1);
AA = AA + AA';
% Create a connected graph from the adjacency matrix
while ~isconnected(graph(AA))
AA = triu(rand(n) < p, 1);
AA = AA + AA';
end
S = AA;
for i = 1:n
for j = 1:n
if ismember(i, R) == 1
S(n, i) = 1;
S(i, n) = 1;
else
S(n, i) = 0;
S(i, n) = 0;
end
end
end
G = graph(S);
p=plot(G);
% Define other problem data (cost vector c, constraint rho, etc.)
% (Replace these with the actual data)
c = ones(n, n); % Cost vector c (n x n matrix)
for i=1:n-1
rho_in(i)=0;
end
rho_in(n)=n-1-length(R);
for i=1:n-1
if ismember(i,R)==0
rho_fin(i)=1;
else
rho_fin(i)=0;
end
%input(sprintf('rho_fin(%d)=',i));
end
rho_fin(n)=0;
RHO=(rho_in-rho_fin)';
try
% Create Gurobi model
model = gurobi.Model();
% Add variables b_{i,j} and x_{i,j}
b = model.addVars(n, n, 'B'); % Binary variables
x = model.addVars(n, n, 'C'); % Continuous variables
% Set objective function
obj = 0;
for i = 1:n
for j = 1:n
if S(i, j) == 1
obj = obj + c(i, j) * b(i, j) * x(i, j);
end
end
end
model.setObjective(obj, 'minimize');
% Add constraints
% Constraint: \sum_{j\in N(i)} b_{i,j}x_{i,j} - \sum_{j\in N(i)} b_{j,i}x_{j,i} = \rho(i) for all i
for i = 1:n
expr = 0;
for j = 1:n
if S(i, j) == 1
expr = expr + b(i, j) * x(i, j) - b(j, i) * x(j, i);
end
end
model.addConstr(expr == RHO(i));
end
% Additional Radiality Constraints
for j = 1:n
if ~ismember(j, R) % If j is not in the set R (set of sources)
model.addConstr(sum(b(:, j)) == 1);
end
for i = 1:n
if S(i, j) == 1
model.addConstr(b(i, j) + b(j, i) <= 1);
end
if ismember(j, R) % If j is in the set R (set of sources)
model.addConstr(b(i, j) == 0);
end
end
end
% Optimize the model
model.optimize();
% Display the solution
disp('Objective value:');
disp(model.objval);
disp('Binary variables b:');
disp(model.getAttr('x', b));
disp('Continuous variables x:');
disp(model.getAttr('x', x));
catch gurobiError
disp('Error reported by Gurobi');
disp(gurobiError.message);
end
I get always this error message: Error reported by Gurobi
Unable to resolve the name gurobi.Model.
I want to know please what is exactly the probleml?. Thank you.
0
-
Official comment
This post is more than two 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 try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi,
This does not look like the MATLAB API, please check some of the simple examples e.g. mip1.m and revise your code.
Cheers,
David0
Post is closed for comments.
Comments
2 comments