-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriteAMPLRunOptions.m
More file actions
250 lines (217 loc) · 7.44 KB
/
Copy pathwriteAMPLRunOptions.m
File metadata and controls
250 lines (217 loc) · 7.44 KB
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
function writeAMPLRunOptions(fid,formulation,generalOptions,solveOptions)
% writes general AMPL .run file beginning (including solver options)
% (following this - model, data, solve, output options)
%
% writeAMPLRunOptions(fid,formulation,generalOptions,solveOptions)
%
% fid = file handle ID to write to (from previous fopen call)
%
% formulation: 'LP' or 'Int' (each req specialized options)
%
% generalOptions (all defaults = 0)
% struct with
% '.noAMPLPresolve'
% '.noCPLEXPresolve'
% '.AMPLtimes'
% '.parallelmode'
% '.threads'
% '.timeoutCPLEX'
%
% NOTE: solveOptions can have any combo of fields
% correct fields for formulation are checked
%
% if formulation=='LP': solveOptions (OPTIONAL)
% struct with (any combination of the following)
% '.netopt' (0,1,2)
% '.alg' ('primal','dual','barrier','auto')
% '.PD' ('primal','dual')
%
% if formulation=='Int': solveOptions (OPTIONAL)
% struct with MIP solver options
% '.<CPLEX directive/' = value
% (for example)
% '.solutionlim' = %i
% '.timeoutBB' = %i
% '.mipemphasis' = {0,1,2}
% '.relobjdiff' (B&B branch throw-away relaxation)
% '.varselect'
% '.probe'
% '.mipgap'
% '.mipcuts'
% '.mipsearch'
% '.mipalgorithm'
%
% EXTENDED GENERAL OPTIONS DESCRIPTION (from AMPL/CPLEX user manual)
%
% timeoutCPLEX: in seconds (overridden if solutionlim set)
%
% parallelmode: deterministic or opportunistic
% parmode=0; % 0 for default (auto - depends on threads)
% parmode=-1; % -1 for opportunistic mode
% parmode = 1; % 1 for deterministic mode
%
% threads
% value 0 tells CPLEX to use as many threads as possible, subj to:
% - allowed by license when parallelmode = -1
% - maintaining deterministic algorithm when parallelmode = 0
% positive value specifies that number of threads should be used
% BR 1/30/2012
% changelog: author,date,change
%{
- added CPLEX memoryemphasis hardcorded in (applies to LP and MIP)
- 2/7 - added auxfiles from ampl to temp dir
- 2/14/2012 - added capability to set fields with no '='
(no '=' if field is empty)
- 2/27/2012 - changed so CPLEX timeout enforced ALWAYS
(not overridden by solutionlimit...)
- 3/4/2013: fixed bug with LP solver options (ignoring alg)
%}
if(nargin<4)
solveOptions=[]; % all defaults
generalOptions=[];
end
if(nargin<3)
solveOptions=[];
end
% DEFAULTS (NO SPECIFICATION) USED FOR ANY UNSET PARAMETERS
% generalOptions have defaults...
if(~isfield(generalOptions,'noAMPLPresolve'))
generalOptions.noAMPLPresolve=0;
end
if(~isfield(generalOptions,'noCPLEXPresolve'))
generalOptions.noCPLEXPresolve=0;
end
if(~isfield(generalOptions,'AMPLtimes'))
generalOptions.AMPLtimes=0;
end
if(~isfield(generalOptions,'parallelmode'))
generalOptions.parallelmode=0;
end
if(~isfield(generalOptions,'threads'))
generalOptions.threads=0;
end
if(~isfield(generalOptions,'timeoutCPLEX'))
generalOptions.timeoutCPLEX=0;
end
% Write AMPL options
fprintf(fid, 'option solver cplexamp;\n'); % this calls academic (full)
fprintf(fid, 'option show_stats 1;\n');
if(generalOptions.AMPLtimes)
fprintf(fid, 'option times 1;\n');
end
%fprintf(fid,' option gentimes 1;\n');
%fprintf(fid, 'option auxfiles ''acfrsu'';\n');
% disable presolve
if(generalOptions.noAMPLPresolve)
fprintf(fid, 'option presolve 0;\n');
end
% CPLEX OPTIONS (settings from start of fcn):
fprintf(fid, '\noption cplex_options ''timing 1 ''\n');
fprintf(fid, '''parallelmode=%d ''\n',generalOptions.parallelmode);
fprintf(fid, '''threads=%d ''\n',generalOptions.threads);
if(isfield(solveOptions,'memoryemphasis'))
fprintf(fid, '''memoryemphasis=%i ''\n',solveOptions.memoryemphasis);
end
% CPLEX Timeout (solutionlim overrides)
%if((generalOptions.timeoutCPLEX>0) ...
% && (~isfield(solveOptions,'solutionlim')))
if(generalOptions.timeoutCPLEX>0)
fprintf(fid, '''timelimit=%d ''\n',generalOptions.timeoutCPLEX);
%fprintf('CPLEX TIMEOUT: %d \n',generalOptions.timeoutCPLEX);
end
% get fields from solveOptions
% print options...(generic template)
if(~isempty(solveOptions))
names = fieldnames(solveOptions)
else
names=[];
end
check={'netopt','alg','PD'};
for i = 1:length(check)
ind = find(strcmp(names,check{i}));
if(ind)
names{ind}='ignore';
end
end
switch formulation
case 'int'
% MIP solver options
% WARNINGS WHEN PARAMETER NOT RECOGNIZED...
% (could just not be hard-coded yet)
% test that LP options not set for int...
% if(...
% max([max(strcmp(names,'alg'));
% max(strcmp(names,'PD'));
% max(strcmp(names,'netopt'))]))
% fprintf('\nWarning - LP options set with Int formulation\n')
% end
if(~isfield(solveOptions,'mipdisplay'))
fprintf(fid, '''mipdisplay=1 ''\n'); % display LP iterations
end
for i = 1:length(names)
if(~strcmp(names{i},'ignore'))
field = getfield(solveOptions,names{i});
if(~isempty(field))
fprintf(fid, '''%s=%i ''\n',names{i},field);
else
fprintf(fid, '''%s ''\n',cmd,names{i});
end
%fprintf(fid, '''%s = %i ''\n',names{i},...
% getfield(solveOptions,names{i}));
end
end
case 'LP'
% test that only LP options set
% if none of names = an LP option & names isn't empty, warning
testVec=[max(strcmp(names,'alg'));
max(strcmp(names,'PD'));
max(strcmp(names,'netopt'))];
if((~max(testVec)) && (~isempty(names)))
fprintf('\nWarning - no LP options set\n')
end
% LP solver choices here
if(isfield(solveOptions,'netopt'))
fprintf(fid, '''netopt=%i ''\n',getfield(solveOptions,'netopt'));
end
% tell CPLEX which algorithm to use
if(isfield(solveOptions,'alg'))
alg=solveOptions.alg;
switch alg
case 'primal'
fprintf(fid, '''primalopt ''\n');
disp('Forced Primal Opt')
case 'dual'
fprintf(fid, '''dualopt ''\n');
disp('Forced Dual Opt')
case 'barrier'
fprintf(fid, '''baropt ''\n');
disp('Forced Barrier Opt')
case 'auto'
%fprintf(fid, '''autoopt ''\n');
disp('Auto Opt')
end
end
% tell CPLEX which formulation to solve
if(isfield(solveOptions,'PD'))
PD=solveOptions.PD;
switch PD
case 'primal'
fprintf(fid, '''primal ''\n');
case 'dual'
fprintf(fid, '''dual ''\n');
end
end
% print other options as specified...
% (I think CPLEX ignores MIP options for LP)
for i = 1:length(names)
if(~strcmp(names{i},'ignore'))
field = getfield(solveOptions,names{i});
if(~isempty(field))
fprintf(fid, '''%s=%i ''\n',names{i},field);
else
fprintf(fid, '''%s ''\n',cmd,names{i});
end
end
end
end
fprintf(fid,';\n'); % finish options line