wozaimoyu
/
IRS_Enhanced-Wireless-Network_Joint-Active-and-Passive-BeamformingDesign_Qingqing-Wu-and-Rui-Zhang
Public
forked from jhan-04/IRS_Enhanced-Wireless-Network_Joint-Active-and-Passive-BeamformingDesign_Qingqing-Wu-and-Rui-Zhang
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcvx_grbgetkey.m
495 lines (464 loc) · 18.6 KB
/
cvx_grbgetkey.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
function success = cvx_grbgetkey( kcode, overwrite )
% CVX_GRBGETKEY Retrieves and saves a Gurobi/CVX license.
%
% This function is used to install Gurobi license keys for use in CVX. It
% is called with your Gurobi license code as a string argument; e.g.
%
% cvx_grbgetkey xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
% cvx_grbgetkey( 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' )
%
% If you have not yet obtained a Gurobi license code, please visit the page
%
% <a href="matlab: web('http://www.gurobi.com/documentation/5.5/quick-start-guide/node5','-browser');">http://www.gurobi.com/documentation/5.5/quick-start-guide/node5</a>
%
% for information on your various options (trial, academic, commercial).
% Once you have received notice that your license has been created, visit
% the Gurobi license page
%
% <a href="matlab: web('http://www.gurobi.com/download/licenses/current','-browser');">http://www.gurobi.com/download/licenses/current</a>
%
% to retrieve the 36-character license code.
%
% The retrieved Gurobi license will be stored in your MATLAB preferences
% directory (see the PREFDIR command) on Mac and Linux, and your home
% directory on Windows. If a license file already exists at this location,
% and its expiration date has not yet passed, CVX_GRBGETKEY will refuse to
% retrieve a new license. To override this behavior, call the function
% with an -OVERWRITE argument:
%
% cvx_grbgetkey xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx -overwrite
% cvx_grbgetkey( 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', '-overwrite' )
%
% Note that this utility is meant to retrieve licenses for the version of
% Gurobi that is bundled with CVX. While it will retrieve full licenses as
% well, it is strongly recommended that you move such licenses to one of the
% standard Gurobi locations, discussed here:
%
% <a href="matlab: web(''http://www.gurobi.com/documentation/5.5/quick-start-guide/node8'',''-browser'');">http://www.gurobi.com/documentation/5.5/quick-start-guide/node8</a>
%
% Note that in order to use Gurobi with CVX, *both* a Gurobi license and a
% CVX Professional license are required.
if exist( 'OCTAVE_VERSION', 'builtin' ),
error( 'CVX:NoOctave', 'CVX_GRBGETKEY cannot be used with Octave.' );
end
fs = filesep;
mpath = mfilename('fullpath');
temp = strfind( mpath, fs );
mpath = mpath( 1 : temp(end) - 1 );
mext = mexext;
success = true;
line = '---------------------------------------------------------------------------';
jprintf({
''
line
'CVX/Gurobi license key installer'
line
});
%%%%%%%%%%%%%%%%%%%%%%%%%
% Process the arguments %
%%%%%%%%%%%%%%%%%%%%%%%%%
if nargin == 2 && ischar( kcode ) && size( kcode, 1 ) == 1 && kcode(1) == '-',
tmp = kcode;
kcode = overwrite;
overwrite = tmp;
end
emsg = [];
if nargin < 1,
emsg = '';
elseif isempty( kcode ) || ~ischar( kcode ) || ndims( kcode ) > 2 || size( kcode, 1 ) ~= 1, %#ok
emsg = 'Invalid license code: must be a string.';
elseif ~regexp( kcode, '^[0-9a-f]{8,8}-[0-9a-f]{4,4}-[0-9a-f]{4,4}-[0-9a-f]{4,4}-[0-9a-f]{12,12}$', 'once' )
emsg = sprintf( 'Invalid license code: %s', kcode );
elseif nargin < 2 || isempty( overwrite ),
overwrite = false;
elseif isequal( overwrite, '-overwrite' ),
overwrite = true;
elseif ischar( overwrite ) && size( overwrite, 1 ) == 1,
emsg = sprintf( 'Invalid argument: %s', overwrite );
else
emsg = 'Invalid second argument.';
end
if ischar( emsg ),
if ~isempty( emsg ),
fprintf( '*** %s\n\n', emsg );
end
jprintf({
'This function is used to install Gurobi license keys for use in CVX. It'
'is called with your license code as an argument; e.g.'
''
' %s xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
' %s( ''xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'' )'
''
'If you have not yet obtained a license code, please visit the page'
''
' <a href="matlab: web(''http://www.gurobi.com/documentation/5.5/quick-start-guide/node5'',''-browser'');">http://www.gurobi.com/documentation/5.5/quick-start-guide/node5</a>'
''
'for information on your various options (trial, academic, commercial). Once'
'a license has been created, you may retrieve its 36-character code by'
'logging into your Gurobi account and visiting the page'
''
' <a href="matlab: web(''http://www.gurobi.com/download/licenses/current'',''-browser'');">http://www.gurobi.com/download/licenses/current</a>'
''
}, mfilename, mfilename );
success = false;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Check to see if Gurobi is present %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if success,
if fs == '\', fsre = '\\'; else fsre = fs; end
gname = [ mpath, fs, 'gurobi' ];
if ~exist( gname, 'dir' ),
jprintf({
'This function is meant to be used only with the version of Gurobi that is'
'bundled with CVX; but your CVX installation does not include Gurobi.'
'To rectify this, you may either download a CVX/Gurobi bundle from'
''
' <a href="matlab: web(''http://cvxr.com/cvx/download'',''-browser'');">http://cvxr.com/cvx/download</a>'
''
'or download the full Gurobi package from Gurobi directly:'
''
' <a href="matlab: web(''http://www.gurobi.com'',''-browser'');">http://www.gurobi.com</a>'
''
'In either case, you will need both a CVX Professional license and a Gurobi'
'license to proceed.'
});
success = false;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ensure that this platform is compatible with the bundled Gurobi solver %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if success,
gname = [ gname, fs, mext(4:end) ];
if ~exist( gname, 'dir' ),
mismatch = false;
switch mext,
case 'mexmaci', pform = '32-bit OSX';
case 'glx', pform = '32-bit Linux';
case 'a64', pform = '64-bit Linux'; mismatch = true;
case 'maci64', pform = '64-bit OSX'; mismatch = true;
case 'w32', pform = '32-bit Windows'; mismatch = true;
case 'w64', pform = '64-bit Windows'; mismatch = true;
otherwise, pform = [];
end
if mismatch,
jprintf({
'The %s version of Gurobi is missing, perhaps because you downloaded a CVX'
'package for a different MATLAB platform. Please visit'
''
' <a href="matlab: web(''http://cvxr.com/cvx/download'',''-browser'');">http://cvxr.com/cvx/download</a>'
''
'and download and install the correct package.'
}, pform );
elseif isempty( pform ),
fprintf( 'CVX/Gurobi is not supported on the %s platform.\n', computer );
else
fprintf( 'CVX/Gurobi is not supported on %s. Consider using the 64-bit version of MATLAB.\n', pform );
end
success = false;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Confirm the existence of the grbgetkey utility %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if success,
gname = [ gname, fs, 'grbgetkey' ];
if fs == '\',
gname = [ gname, '.exe' ];
end
if ~exist( gname, 'file' ),
jprintf({
'Your CVX package is missing the file'
''
' %s'
''
'which is necessary to complete this task. Please reinstall CVX.'
}, gname );
success = false;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create the preferences directory, if necessary %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if success,
fdir = regexprep( prefdir, [ fsre, 'R\d\d\d\d\w*$' ], '' );
if ~exist( fdir, 'dir' ),
[success,msg] = mkdir( fdir );
if ~success,
jprintf({
'This function needs to write to the directory'
''
' %s'
''
'which does not exist. An attempt to create it resulted in this error:'
''
' %s'
''
'Please rectify this problem and try again.'
}, fdir, msg );
success = false;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Guard against overwriting an existing license %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if success,
% We actually use the parent directory of 'prefdir' if that directory has
% the standard version format: e.g., if prefdir is ~/joe/.matlab/R2012b,
% we store our data in ~/joe/.matlab, so that all versions of MATLAB can
% see the same CVX data.
fname = [ fdir, fs, 'cvx_gurobi.lic' ];
if exist( fname, 'file' ),
if overwrite,
msg = [];
else
msg = 'This license may or may not be current.';
fid = fopen( fname );
if fid ~= 0,
fstr = fread( fid, Inf, 'uint8=>char' )';
fclose( fid );
matches = regexp( fstr, 'EXPIRATION=\d\d\d\d-\d\d-\d\d', 'once' );
if matches && floor(datenum(fstr(matches+11:matches+20),'yyyy-mm-dd'))<floor(datenum(clock))
msg = [];
else
msg = 'This license has not yet expired.';
end
end
end
if ~isempty( msg )
jprintf({
'An existing license file has been found at location:'
''
' %s'
''
'%s If you wish to overwrite this file,'
'please re-run this function with an "-overwrite" argument: that is,'
''
' %s %s -overwrite'
' %s( ''%s'', ''-overwrite'' )'
''
'Otherwise, please move the existing license and try again.'
}, fname, msg, mfilename, kcode, mfilename, kcode );
success = false;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create a temporary destination directory %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tdir = [];
if success,
for k = 1 : 10,
tdir = tempname;
if ~exist( tdir, 'file' ), break; end
end
[success,msg] = mkdir(tdir);
if ~success,
jprintf({
'This function attempted to create the temporary directory'
''
' %s'
''
'but the following error occurred:'
''
' %s'
''
'Please rectify the problem and try again.';
}, tdir, msg );
success = false;
tdir = [];
end
end
%%%%%%%%%%%%%%%%%%%%%%%%
% Download the license %
%%%%%%%%%%%%%%%%%%%%%%%%
if success
fprintf( 'Contacting the Gurobi Optimization license server...' );
[status,result]=system( sprintf( '%s --path=%s %s', gname, tdir, kcode ) ); %#ok
fprintf( 'done.\n' );
if any( strfind( result, 'Unable to determine hostname' ) ) || any( strfind( result, 'not recognized as belonging to an academic domain' ) ),
jprintf({
'The attempt to retrieve the license key failed with the following error'
'while trying to verify your academic license eligibility:'
''
' %s'
'For information about this error, please consult the Gurobi documentation'
''
' <a href="matlab: web(''http://www.gurobi.com/documentation/5.5/quick-start-guide/node5'',''-browser'');">http://www.gurobi.com/documentation/5.5/quick-start-guide/node5</a>'
''
'Once you have rectified the error, please try again.'
}, regexprep(result,'.*---------------------\n+(.*?)\n+$','$1') );
success = false;
elseif any( strfind( result, 'already issued for host' ) )
matches = regexp( fstr, 'already issued for host ''([^'']+)''', 'once' );
if ~isempty( matches ),
matches = sprintf( ' (%s)', matches{1}{1} );
else
matches = '';
end
jprintf({
'This license has already been issued for a different host%s.'
'Please acquire a new license for this host from Gurobi.'
}, matches );
success = false;
elseif ~any( strfind( result, 'License key saved to file' ) ),
jprintf({
'The attempt to retrieve the license key failed with the following error:'
''
' %s'
'For information about this error, please consult the Gurobi documentation'
''
' <a href="matlab: web(''http://www.gurobi.com/documentation/5.5/quick-start-guide/node5'',''-browser'');">http://www.gurobi.com/documentation/5.5/quick-start-guide/node5</a>'
''
'Once you have rectified the error, please try again.'
}, regexprep(result,'.*---------------------\n+(.*?)\n+$','$1') );
fprintf( '%s\n', line );
fprintf( result(1+(result(1)==10):end) );
success = false;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Read the license file to determine its expiration %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if success,
tname = [ tdir, fs, 'gurobi.lic' ];
[fid,msg] = fopen( tname, 'r' );
if fid == 0,
jprintf({
'An unexpected error occured: the Gurobi license file could not be'
'opened. The file was expected to be in the temporary location'
''
' %s'
''
'but the attempt to read it resulted in the following error:'
''
' %s'
''
'Please attempt to rectify the problem and try again; if necessary,'
'please contact <a href="mailto:cvx@cvxr.com">CVX support</a>.'
}, tname, msg );
success = false;
tdir = [];
else
fstr = fread( fid, Inf, 'uint8=>char' )';
fclose( fid );
matches = regexp( fstr, 'EXPIRATION=\d\d\d\d-\d\d-\d\d', 'once' );
if matches,
if floor(datenum(fstr(matches+11:matches+20),'yyyy-mm-dd'))<floor(datenum(clock))
jprintf({
'The license was successfully downloaded, but it expired on %s.'
'Please contact Gurobi for a new license.'
}, fstr(matches+11:matches+20) );
success = false;
else
jprintf({
'Download successful. The license can be found at'
''
' %s'
''
'The license will expire at the end of the day on %s.'
}, fname, fstr(matches+11:matches+20) );
end
end
if success,
matches = regexp( fstr, '\nAPPNAME=CVX\n', 'once' );
if ~any( matches ),
[ matches, mends ] = regexp( fstr, '\nAPPNAME=\w+', 'start', 'end' );
if any( matches ),
jprintf({
line
'ERROR: This license is reserved for the application "%s" and will'
'*not* work with CVX. We strongly recommend that you move this license to'
'another location, in accordance with this documentation:'
''
' <a href="matlab: web(''http://www.gurobi.com/documentation/5.5/quick-start-guide/node8'',''-browser'');">http://www.gurobi.com/documentation/5.5/quick-start-guide/node8</a>'
''
'To use Gurobi with CVX, you must either obtain a full Gurobi license or a'
'CVX specific license.'
}, fstr(matches(1)+9:mends) );
success = false;
else
if ispc,
cmd = ' movefile(''%s'',''%sgurobi.lic'')';
fdr = getenv('USERPROFILE');
if fdr(end) ~= '\', fdr(end+1) = '\'; end
else
cmd = ' movefile %s %sgurobi.lic';
fdr = '~/';
end
jprintf({
line
'WARNING: This license is not a CVX-specific license. It will still work with'
'CVX; however, if you also wish to use it with other applications, then we'
'strongly recommend that you copy it to a standard Gurobi location. Consult'
'the following Gurobi documentation for more information:'
''
' <a href="matlab: web(''http://www.gurobi.com/documentation/5.5/quick-start-guide/node8'',''-browser'');">http://www.gurobi.com/documentation/5.5/quick-start-guide/node8</a>'
''
'For instance, to move this file to the standard home directory location,'
'copy and paste this command into your MATLAB command line:'
''
cmd
}, fname, fdr );
end
end
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Move the license to its proper location %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if success,
[success,msg] = movefile( tname, fname, 'f' );
if ~success,
jprintf({
'The attempt to move the Gurobi license file from its temporary location'
''
' %s'
''
'to its final location'
''
' %s'
''
'resulted in the following error:'
''
' %s'
''
'Please rectify the problem and try again; or move the file manually. Once'
'the license is in place, please run CVX_SETUP to configure CVX to use the'
'Gurobi solver with the new license.'
}, tname, fname, msg );
success = false;
end
end
%%%%%%%%%%%%
% Clean up %
%%%%%%%%%%%%
if ~isempty(tdir),
[success,message] = rmdir( tdir, 's' ); %#ok
end
if success,
jprintf({
line
'Now that the license has been retrieved, please run CVX_SETUP to configure'
'CVX to use the Gurobi solver with the new license.'
});
end
jprintf({
line
''
});
if nargout == 0,
clear success
end
function jprintf( strs, varargin )
strs = strs(:)';
[ strs{2,:} ] = deal( '\n' );
fprintf( cat(2,strs{:}), varargin{:} );
% Copyright 2014 CVX Research, Inc.
% This file is governed by the terms of the CVX Professional license;
% redistribution is *not* permitted. Please see the file LICENSE.txt for
% full copyright information.
% The command 'cvx_where' will show where this file is located.