Thank you for your guidance.
I downloaded Gurobi 12.0.2 and activated it with license code, I could run the example offered by Gurobi with matlab (but not with cvx) properly.
function mip1()
% Copyright 2025, Gurobi Optimization, LLC
% This example formulates and solves the following simple MIP model:
% maximize
% x + y + 2 z
% subject to
% x + 2 y + 3 z <= 4
% x + y >= 1
% x, y, z binary
names = {'x'; 'y'; 'z'};
model.A = sparse([1 2 3; 1 1 0]);
model.obj = [1 1 2];
model.rhs = [4; 1];
model.sense = '<>';
model.vtype = 'B';
model.modelsense = 'max';
model.varnames = names;
gurobi_write(model, 'mip1.lp');
params.outputflag = 0;
result = gurobi(model, params);
disp(result);
for v=1:length(names)
fprintf('%s %d\n', names{v}, result.x(v));
end
fprintf('Obj: %e\n', result.objval);
end
But when I ran cvx_grbgetkey, it showed :
---------------------------------------------------------------------------
CVX/Gurobi license key installer
---------------------------------------------------------------------------
Contacting the Gurobi Optimization license server...done.
The attempt to retrieve the license key failed with the following error:
info : grbgetkey version 9.0.0, build v9.0.0rc2
info : Contacting Gurobi key server...
info : Key for license ID 2672699 was successfully retrieved
info : License expires at the end of the day on 2026-06-09
info : Saving license key...
info : License 2672699 written to file C:\Users\lenovo\AppData\Local\Temp\tp899b6f22_0eb7_4521_9b7b_2ec2a4c4f9d9\gurobi.lic
For information about this error, please consult the Gurobi documentation
http://www.gurobi.com/documentation/5.5/quick-start-guide/node5
Once you have rectified the error, please try again.
---------------------------------------------------------------------------
警告: 转义字符 '\U' 无效。有关支持的特殊字符,请参阅 'doc sprintf'。
> 位置:cvx_grbgetkey (第 338 行)
info : grbgetkey version 9.0.0, build v9.0.0rc2
info : Contacting Gurobi key server...
info : Key for license ID 2672699 was successfully retrieved
info : License expires at the end of the day on 2026-06-09
info : Saving license key...
info : License 2672699 written to file C:---------------------------------------------------------------------------
Now that the license has been retrieved, please run CVX_SETUP to configure
CVX to use the Gurobi solver with the new license.
---------------------------------------------------------------------------
According to Problem with grbgetkey - #5 by krishnasandeep, I tried to run Gurobi outside the CVX and ran cvx_setup, but the Gurobi solver initialized is still 9.0.0 :
>> cvx_setup
---------------------------------------------------------------------------
CVX: Software for Disciplined Convex Programming (c)2014 CVX Research
Version 2.2, Build 1148 (62bfcca) Tue Jan 28 00:51:35 2020
---------------------------------------------------------------------------
Installation info:
Path: C:\Users\lenovo\Documents\MATLAB\cvx
MATLAB version: 9.14 (R2023a)
OS: Windows 10 amd64 version 10.0
Java version: 1.8.0_202
Verfying CVX directory contents:
WARNING: The following files/directories are missing:
C:\Users\lenovo\Documents\MATLAB\cvx\sedumi\.travis.yml
These omissions may prevent CVX from operating properly.
Preferences:
Path: C:\Users\lenovo\AppData\Roaming\MathWorks\MATLAB\cvx_prefs.mat
License host:
Username: lenovo
Host ID: 745d226b91db (eth0)
Installed license:
No license installed.
No valid licenses found.
Click here to fill out an academic license request
for the username and first hostid listed above.
---------------------------------------------------------------------------
Setting CVX paths...already set!
Searching for solvers...5 shims found.
3 solvers initialized (* = default):
Gurobi 9.00 {cvx}\gurobi\w64
* SDPT3 4.0 {cvx}\sdpt3
SeDuMi 1.3.4 {cvx}\sedumi
4 solvers skipped:
GLPK
Could not find a GLPK installation.
Gurobi_2 unknown C:\gurobi1202\win64\matlab
result = gurobi(model, params)
Gurobi_3 unknown C:\gurobi1202\win64\matlab
result = gurobi(model, params)
Mosek 9.1.9 {cvx}\mosek\w64
Using MOSEK with CVX requires an academic MOSEK license
or a MOSEK-enabled CVX Professional license.
1 solver issued warnings:
Gurobi 9.00 {cvx}\gurobi\w64
Using CVX with a commercial Gurobi license also requires
a Gurobi-enabled CVX Professional license. Commercial
users with Gurobi licenses should contact sales@cvxr.com.
Saving updated preferences...done.
Testing with a simple model...done!
---------------------------------------------------------------------------
To change the default solver, type "cvx_solver <solver_name>".
To save this change for future sessions, type "cvx_save_prefs".
Please consult the users' guide for more information.
---------------------------------------------------------------------------
and when I ran a test code:
clear all;
cvx_clear
cvx_solver Gurobi
cvx_begin
variable z(3) binary
minimize( sum(z) )
subject to
z(1) + z(2) >= 1
z(3) - z(1) <= 0
cvx_end
disp(z);
an error occurred :
Calling Gurobi 9.00: 5 variables, 2 equality constraints
------------------------------------------------------------
------------------------------------------------------------
Status: Error
Optimal value (cvx_optval): NaN
错误使用 cvx_end
model.quadcon must be a struct array with fields q, and rhs
出错 test1 (第 11 行)
cvx_end
Now what could I do to fix it ?