-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreadGMS.m
1298 lines (1244 loc) · 38.8 KB
/
readGMS.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function [objPoly,ineqPolySys,lbd,ubd, minOrmax] = readGMS(fileName,symbolicMath)
%
% readGMS
% converts GAMS scalar format into SparsePOP format
%
% Usage:
% [objPoly,ineqPolySys,lbd,ubd] = readGMS(fileName,symbolicMath);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Inputs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% fileName: the file name written in GAMS scalar format.
% symbolicMath: 1 if you have the Symbolic Math Toolbox provieded by
% MathWorks. With this option, parentheses in the file are
% expanded automatically. Default value is 0.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Outputs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% objPoly, inEqPolySys, lbd, ubd form the SparsePOP format.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This file is a component of SparsePOP
% Copyright (C) 2007 SparsePOP Project
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
if nargin < 2
symbolicMath = 0;
end
if nargout < 5
minOrmax = 'min';
end
if exist(fileName, 'file') ~= 2
error('## Input file does not exist.');
end
minOrmax = 'min';
eqTo2ineqSW = 0;
eqTolerance = 0.0;
% Input %%%%%
% filename: GAMS data file name with .gms, for example "ex2_1_2.gms".
% eqTo2ineqSW
% = 0; to keep equalities as they are.
% = 1; to convert an equality f(x) = 0 into f(x) >= 0
% and f(x) <= eqTolerance.
% eqTolerance = 1e-4;
%%%%%%%%%%%%%
% Restriction:
% 1. A line starting with '*' in the first column is regarded as a comment
% line.
% 2. At most one item of "Variables", "Positive Variables", "Equations",
% constraints and bounds is contained in one line.
% The end of one item is marked with ';'.
% One item can be written in more than one line;
% The first letter of each line can not be '*'.
% For example,
% Positive Variables x1,x2,x3,x4,
% x5,x6;
% e1.. +0.5*x1*x1 +0.5*x2*x2 +0.5*x3*x3 +0.5*x4*x4
% +0.5*x5*x5 +10.5*x1 +7.5*x2
% +3.5*x3 +2.5*x4 +1.5*x5 +10*x6 + objvar =E= 0;
% x1.up = 1;
% are allowed. But
% x1.lo = 1; x1.up = 2;
% e1.. +0.5*x1*x1 +0.5*x2*x2 +0.5*x3*x3 +0.5*x4*x4 +3.5
% *x3 +2.5*x4 +1.5*x5 +10*x6 + objvar =E= 0;
% are not allowed.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Output %%%%%
% objPoly
% ineqPolySys
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Variables
% noOfVariables <--- "Variables" in the GAMS file;
% varNames{k} (k=1,2,?ldots,noOfVariables)
% <--- "Variables" in the GAMS file;
% posVarNames (k=1,2,?ldots,noOfVariables)
% <--- "Positive Variables" in the GAMS file;
% noOfEquations
% <--- "Equations" in the GAMS file;
% equationNames (i=1,2,?ldots,noOfEquations);
% <--- "Equations" in the GAMS file;
% lbd(1,k) (k=1,2,?ldots,noOfVariables);
% ubd(1,k) (k=1,2,?ldots,noOfVariables);
% listOfTerms{i} (i=1,2,?ldots,noOfEquations);
% --- the list of monomials of each equation.
%
% 2010-01-11 H.Waki
% Convert file into one line separated by ';'.
%
allStatements = fromFile(fileName);
%allStatements
minOrmaxIdx = strfind(allStatements, 'maximizing objvar');
if ~isempty(minOrmaxIdx)
fprintf('## The inputted problem is the maximization problem.\n');
fprintf('## We multiply -1 into the objective fuction for \n');
fprintf('## converting it into the minimization problem. \n');
minOrmax ='max';
else
minOrmax ='min';
end
% Remove white spaces at the head and tail in all lines.
%
allStatements = removeWhiteSpaces(fileName, allStatements);
%allStatements
% Remove the line whose head is an asterisk.
%
allStatements = removeStar(allStatements);
%allStatements
% Get the name of variables
%
[varNames, allStatements] = getVarName(fileName, allStatements);
noOfVariables = size(varNames,2);
%allStatements
%varNames
% Get the name of nonnegative variables
%
[posVarNames, allStatements] = getPosvarName(fileName, allStatements);
checkPosVar(varNames, posVarNames);
%allStatements
%posVarNames
% Get the name of binary variables
%
[binVarNames, allStatements] = getBinaryName(fileName, allStatements);
checkBinVar(varNames, binVarNames);
%allStatements
%binVarNames
% Get the name of constraints
% Get the name of constraints
%
[equationNames, allStatements] = getEquationName(fileName, allStatements);
noOfEquations = size(equationNames,2);
%equationNames
%allStatements
% Get the constraints
%
%allStatements
[equationNames,listOfEquations, allStatements] = getEquation(fileName, allStatements, equationNames, symbolicMath);
%allStatements
% Get lower and upper bounds
%
[lbd, ubd, fixed, allStatements] = getLowerUpper(fileName, allStatements, varNames);
% allStatements
% varNames
% posVarNames
% equationNames
% listOfEquations
% lbd
% ubd
% fixed
clear allStatements
% finding the objective row
objRow = getObeRow(listOfEquations, noOfEquations);
% analyzing list of constraints.
% separating each line of polynomial equation into the list of monomials.
listOfTerms = cell(1,noOfEquations);
eqOrIneq = cell(1,noOfEquations);
rightValue = cell(1,noOfEquations);
for i=1:noOfEquations
idx = strfind(listOfEquations{i},'=');
if isempty(idx) || length(idx) ~= 2
error('## The constaint of ''%s'' should have ''=E='', ''=G='' or ''=L=''.\n## Should check the kind of the constriant and/or the position of '';''. ', equationNames{i});
%elseif length(idx) > 2
% listOfEquations{i}
% error('## The constaint of ''%s'' have more than one ''=''.', equationNames{i});
end
%listOfEquations{i}
[listOfTerms{i},eqOrIneq{i},rightValue{i}] = separate(listOfEquations{i}, symbolicMath);
%listOfTerms{i}
%eqOrIneq{i}
%rightValue{i}
checkConstraints(listOfTerms{i}, eqOrIneq{i}, rightValue{i}, equationNames{i});
end
% finding the objective term in the objective row
noOfTerms = size(listOfTerms{objRow},2);
p = 1;
temp = [];
while (p <= noOfTerms) && isempty(temp)
temp = strfind(listOfTerms{objRow}{p},'objvar');
if ~isempty(temp)
objTerm = p;
end
p = p+1;
end
% eliminating objvar from varNames
p=0;
idx = 0;
for i=1:noOfVariables
if strcmp('objvar',varNames{i}) ~= 1
p = p+1;
varNames{p} = varNames{i};
else
idx = i;
end
end
if idx == 0
error('## Should write ''objvar'' at the objective function in your problem.');
end
noOfVariables = noOfVariables -1;
%fixed = fixed(1:noOfVariables);
lbd(idx) = [];
ubd(idx) = [];
fixed(idx) = [];
printSW = 0;
if printSW == 1
nnn = size(lbd,2);
for i=1:nnn
fprintf('%+6.2e ',lbd(1,i));
end
fprintf('\n');
for i=1:nnn
fprintf('%+6.2e ',ubd(1,i));
end
fprintf('\n');
end
q = 0;
if listOfTerms{objRow}{objTerm}(1) == '-'
objConstant = -rightValue{objRow};
objFunction = cell(1,noOfTerms);
for p=1:noOfTerms
if p ~= objTerm
q = q+1;
objFunction{q} = listOfTerms{objRow}{p};
end
end
else
objConstant = rightValue{objRow};
objFunction = cell(1,noOfTerms);
for p=1:noOfTerms
if p ~= objTerm
q = q+1;
ll = length(listOfTerms{objRow}{p});
if listOfTerms{objRow}{p}(1) == '-'
objFunction{q} = strcat('+',listOfTerms{objRow}{p}(2:ll));
else
objFunction{q} = strcat('-',listOfTerms{objRow}{p}(2:ll));
end
end
end
end
% eliminating the objective row from listOfTerms
q = 0;
for p=1:noOfEquations
if p ~=objRow
q = q+1;
listOfTerms{q} = listOfTerms{p};
eqOrIneq{q} = eqOrIneq{p};
rightValue{q} = rightValue{p};
end
end
noOfEquations = noOfEquations - 1;
debug = 0;
if debug == 1
fprintf('varNames: ')
for i=1:noOfVariables
fprintf('%5s ',varNames{i});
end
fprintf('\n');
if isempty(posVarNames) ~= 1
ll = size(posVarNames,2);
fprintf('posVarNames:')
for i=1:ll
fprintf('%5s ',posVarNames{i});
end
fprintf('\n');
end
if isempty(binVarNames) ~= 1
ll = size(binVarNames,2);
fprintf('binVarNames:')
for i=1:ll
fprintf('%5s ',binVarNames{i});
end
fprintf('\n');
end
fprintf('lbd : ');
for i=1:noOfVariables
fprintf('%+7.2e ',lbd(i));
end
fprintf('\n');
fprintf('ubd : ');
for i=1:noOfVariables
fprintf('%+7.2e ',ubd(i));
end
fprintf('\n');
fprintf('objFunction : ');
ll = size(objFunction,2);
for j=1:ll
fprintf('%s ',objFunction{j});
end
fprintf('\n');
for i=1:noOfEquations
fprintf('%2d : ',i);
ll = size(listOfTerms{i},2);
for j=1:ll
fprintf('%s ',listOfTerms{i}{j});
end
fprintf(' %s ',eqOrIneq{i});
fprintf(' %+8.3e \n',rightValue{i});
end
end
objPoly = getObjPoly(noOfVariables, varNames, objFunction, objConstant, fileName);
if strcmp(minOrmax, 'max')
objPoly.coef = -objPoly.coef;
end
objPoly = simplifyPolynomial(objPoly);
% ineqPolySys
if ~isempty(binVarNames)
ineqPolySys = cell(1,noOfEquations+size(binVarNames, 2));
else
ineqPolySys = cell(1,noOfEquations);
end
if eqTo2ineqSW == 0
for i=1:noOfEquations
pointer = i;
[statusSW, Poly, msg] = convToPolynomial(noOfVariables,varNames,...
listOfTerms{i},eqOrIneq{i},rightValue{i});
ineqPolySys{i} = Poly;
if statusSW ~= 0
error('%s## Should check the %d%s constraint.', msg, i, thWord(i));
end
ineqPolySys{i} = simplifyPolynomial(ineqPolySys{i});
%size(ineqPolySys{i}.supports)
%full(ineqPolySys{i}.supports)
%size(ineqPolySys{i}.coef)
%full(ineqPolySys{i}.coef')
end
pointer = noOfEquations;
else % eqTo2ineqSW == 1
pointer = 0;
for i=1:noOfEquations
if (eqOrIneq{i} == 'G')
pointer = pointer + 1;
[statusSW, Poly, msg] = convToPolynomial(noOfVariables,varNames,...
listOfTerms{i},eqOrIneq{i},rightValue{i});
ineqPolySys{pointer} = Poly;
elseif (eqOrIneq{i} == 'L')
pointer = pointer + 1;
[statusSW, Poly, msg] = convToPolynomial(noOfVariables,varNames,...
listOfTerms{i},eqOrIneq{i},rightValue{i});
ineqPolySys{pointer} = Poly;
else
pointer = pointer + 1;
[statusSW, Poly, msg] = convToPolynomial(noOfVariables,varNames,...
listOfTerms{i},'G',rightValue{i});
ineqPolySys{pointer} = Poly;
pointer = pointer + 1;
[statusSW, Poly, msg] = convToPolynomial(noOfVariables,varNames,...
listOfTerms{i},'L',rightValue{i}+eqTolerance);
ineqPolySys{pointer} = Poly;
end
if statusSW ~= 0
error('%s## Should check the %d%s constraint.', msg, i, thWord(i));
end
end
end
%tmpL = cell(1,1);
tmpL = cell(1,2);
for i=1:size(binVarNames,2)
%x^2 = 1
%tmpL{1} = strcat('1*',binVarNames{i}, '*', binVarNames{i});
%tmpE = 'E';
%tmpR = 1;
%x(x-1) = 0
tmpL{1} = strcat('1*',binVarNames{i}, '*', binVarNames{i});
tmpL{2} = strcat('-1*',binVarNames{i});
tmpE = 'E';
tmpR = 0;
[statusSW, Poly, msg] = convToPolynomial(noOfVariables,varNames,tmpL,tmpE,tmpR);
ineqPolySys{i+noOfEquations} = Poly;
if statusSW ~= 0
error('%s## Should check the %d%s constraint.', msg, i, thWord(i+noOfEquations));
end
%full(ineqPolySys{i+noOfEquations}.supports)
%ineqPolySys{i+noOfEquations}
end
% pointer = noOfEquations;
% iEqPolySys --- nonnegativity
if isempty(posVarNames) ~= 1
ll = size(posVarNames,2);
for i=1:ll
lbd = posToInEqPolySys(noOfVariables,varNames,posVarNames{i},lbd);
end
end
% Print for debug
debug = 0;
if debug
writePOP(1, objPoly, ineqPolySys, lbd, ubd);
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function allStatements = fromFile(fileName)
fileIDX = fopen(fileName, 'r');
allStatements = '';
nextSW = 0;
while 1
oneLine = fgetl(fileIDX);
if ~ischar(oneLine)
break;
end
[nextSW, statements] = fromOneLine(oneLine, nextSW);
if nextSW == 0
if ~isempty(statements)
statements = deblank(statements);
end
if ~isempty(statements)
statements = strtrim(statements);
end
end
allStatements = [allStatements, statements];
end
allStatements = deblank(allStatements);
allStatements = strtrim(allStatements);
%allStatements
%error();
fclose(fileIDX);
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [nextSW, statements] = fromOneLine(oneLine, nextSW)
statements = '';
if nextSW == 0
oneLine = deblank(oneLine);
oneLine = strtrim(oneLine);
end
if isempty(oneLine)
return
end
if nextSW == 0 && ~isempty(oneLine) && strcmp(oneLine(1), '*')
return
end
idx = strfind(oneLine, ';');
disp(statements);
if isempty(idx)
statements = [statements, oneLine];
nextSW = 1;
else
while ~isempty(idx)
if idx(1) == 1
oneLine(1) = [];
else
statements = [statements, oneLine(1:idx(1))];
if idx(1) <= length(oneLine)
oneLine(1:idx(1)) = [];
else
nextSW = 0;
break;
end
end
if ~isempty(oneLine)
oneLine = deblank(oneLine);
end
if ~isempty(oneLine)
oneLine = strtrim(oneLine);
end
if isempty(oneLine)
nextSW = 0;
return
elseif strcmp(oneLine(1), '*')
nextSW = 0;
return
end
idx = strfind(oneLine, ';');
end
if ~isempty(oneLine)
statements = [statements, oneLine];
nextSW = 1;
end
end
%nextSW
%statements
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function allStatements = removeStar(allStatements)
idx = strfind(allStatements, ';');
sidx = 1;
removeIdx = [];
for i=1:length(idx)
eidx = idx(i);
onestate = allStatements(sidx:eidx);
if strcmp(onestate(1), '*')
removeIdx = [removeIdx; sidx, eidx];
end
sidx = eidx + 1;
end
if sidx <= length(allStatements) && strcmp(allStatements(sidx),'*')
eidx = length(allStatements);
removeIdx = [removeIdx; sidx, eidx];
end
p = size(removeIdx,1);
for i=1:p
sidx = removeIdx(p-i+1,1);
eidx = removeIdx(p-i+1,2);
allStatements(sidx:eidx) = [];
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function allStatements = removeWhiteSpaces(fileName, allStatements)
idx = strfind(allStatements, ';');
if isempty(idx)
error('## ''%s'' does not have any '';''.', fileName);
elseif idx(end) ~= length(allStatements)
error('## '';'' does not exist at the end of the last statement of ''%s''.', fileName);
end
NewAllStatements = '';
sidx = 1;
for i=1:length(idx)
eidx = idx(i);
oneLine = allStatements(sidx:eidx);
sidx = eidx + 1;
oneLine = deblank(oneLine);
oneLine = strtrim(oneLine);
NewAllStatements = [NewAllStatements, oneLine];
end
allStatements = NewAllStatements;
% Remove lines which are undefined in SparsePOP.
%
idx = strfind(allStatements, 'Model');
if ~isempty(idx)
Lines = allStatements(idx(1):end);
aidx = strfind(Lines, 'all');
sidx = strfind(Lines, '/');
if ~isempty(aidx) && ~isempty(sidx)
allStatements(idx(1):end) = [];
end
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [varNames, allStatements] = getVarName(fileName, allStatements)
idx = strfind(allStatements, ';');
if isempty(idx)
error('## ''%s'' does not have any '';''.', fileName);
elseif idx(end) ~= length(allStatements)
error('## '';'' does not exist at the end of the last statement of ''%s''.', fileName);
end
% get the definition of Variables
sidx = 1;
varNames = [];
for i=1:length(idx)
eidx = idx(i);
oneLine = allStatements(sidx:eidx);
%oneLine
Vidx = strfind(oneLine, 'Variables');
if ~isempty(Vidx) && length(Vidx) == 1 && Vidx(1) == 1
[tmp,oneLine] = strtok(oneLine);
if ~isempty(oneLine)
wsidx = strfind(oneLine, ' ');
oneLine(wsidx) = [];
%oneLine
p = 0;
[varNames,p,moreSW] = getListOfNames(oneLine,varNames,p);
while moreSW == 1
[varNames,p,moreSW] = getListOfNames(oneLine,varNames,p);
end
allStatements(sidx:eidx) = [];
break;
end
end
sidx = eidx + 1;
end
if isempty(varNames)
error('## ''%s'' does not have the line of ''Variables''.',fileName);
end
objflag = 0;
for j=1:size(varNames,2)
if strcmp(varNames{j}, 'objvar')
objflag = 1;
break;
end
end
if objflag == 0
error('## The reserved keyword ''%s'' is not defined.\n## Should check the line of ''Variables'' and the position of '';''.','objvar');
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [posVarNames, allStatements] = getPosvarName(fileName, allStatements)
% get the definition of Positive Variables
idx = strfind(allStatements, ';');
sidx = 1;
posVarNames = [];
for i=1:length(idx)
eidx = idx(i);
oneLine = allStatements(sidx:eidx);
Pidx = strfind(oneLine, 'Positive');
if ~isempty(Pidx)
[tmp,oneLine] = strtok(oneLine);
oneLine = strtrim(oneLine);
Vidx = strfind(oneLine, 'Variables');
if ~isempty(Vidx)
oneLine = oneLine(Vidx+9:end);
while true
if strcmp(oneLine(1), blanks(1))
oneLine = oneLine(2:end);
else
break;
end
end
%[tmp,oneLine] = strtok(oneLine);
if ~isempty(oneLine)
wsidx = strfind(oneLine, ' ');
oneLine(wsidx) = [];
p = 0;
[posVarNames,p,moreSW] = getListOfNames(oneLine,posVarNames,p);
while moreSW == 1
[posVarNames,p,moreSW] = getListOfNames(oneLine,posVarNames,p);
end
end
%posVarNames
allStatements(sidx:eidx) = [];
break;
end
end
sidx = eidx + 1;
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function checkPosVar(varNames, posVarNames)
for i=1:size(posVarNames,2)
eqflag = 0;
for j=1:size(varNames,2)
if strcmp(posVarNames{i}, varNames{j})
eqflag = 1;
break;
end
end
if eqflag == 0
error('## ''%s'' is not defined in the line of ''Variables''.\n## Should check the line of ''Positive Variables'' and the position of '';''.', posVarNames{i});
end
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [binVarNames, allStatements] = getBinaryName(fileName, allStatements)
% get the definition of Binary Variables
idx = strfind(allStatements, ';');
sidx = 1;
binVarNames = [];
for i=1:length(idx)
eidx = idx(i);
oneLine = allStatements(sidx:eidx);
Pidx = strfind(oneLine, 'Binary');
if isempty(Pidx)
Pidx = strfind(oneLine, 'Binaries');
end
if ~isempty(Pidx)
[tmp,oneLine] = strtok(oneLine);
oneLine = strtrim(oneLine);
Vidx = strfind(oneLine, 'Variables');
if isempty(Vidx)
Vidx = strfind(oneLine, 'variables');
end
if ~isempty(Vidx)
oneLine = oneLine(Vidx+9:end);
while true
if strcmp(oneLine(1), blanks(1))
oneLine = oneLine(2:end);
else
break;
end
end
if ~isempty(oneLine)
wsidx = strfind(oneLine, ' ');
oneLine(wsidx) = [];
p = 0;
[binVarNames,p,moreSW] = getListOfNames(oneLine,binVarNames,p);
while moreSW == 1
[binVarNames,p,moreSW] = getListOfNames(oneLine,binVarNames,p);
end
end
%binVarNames
allStatements(sidx:eidx) = [];
break;
end
end
sidx = eidx + 1;
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function checkBinVar(varNames, binVarNames)
for i=1:size(binVarNames,2)
eqflag = 0;
for j=1:size(varNames,2)
if strcmp(binVarNames{i}, varNames{j})
eqflag = 1;
break;
end
end
if eqflag == 0
error('## ''%s'' is not defined in the line of ''Variables''.\n## Should check the line of ''Binary Variables'' and the position of '';''.', binVarNames{i});
end
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [equationNames, allStatements] = getEquationName(fileName, allStatements)
% get the definition of Equations
idx = strfind(allStatements, ';');
if isempty(idx)
error('## ''%s'' does not have the line of ''Equations''.',fileName);
elseif idx(end) ~= length(allStatements)
error('## '';'' does not exist at the end of the last statement of ''%s''.', fileName);
end
sidx = 1;
equationNames = [];
for i=1:length(idx)
eidx = idx(i);
oneLine = allStatements(sidx:eidx);
Eidx = strfind(oneLine, 'Equations');
if ~isempty(Eidx)
[tmp,oneLine] = strtok(oneLine);
if ~isempty(oneLine)
wsidx = strfind(oneLine, ' ');
oneLine(wsidx) = [];
p = 0;
[equationNames,p,moreSW] = getListOfNames(oneLine,equationNames,p);
while moreSW == 1
[equationNames,p,moreSW] = getListOfNames(oneLine,equationNames,p);
end
allStatements(sidx:eidx) = [];
break;
end
end
sidx = eidx + 1;
end
if isempty(equationNames)
error('## ''%s'' does not have the line of ''Equations''.',fileName);
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [NewEquationNames, listOfEquations, allStatements] = getEquation(fileName, allStatements, equationNames, symbolicMath)
% get the objective function and all the constraints
idx = strfind(allStatements, ';');
if isempty(idx)
error('## ''%s'' does not have the line of ''Equations''.',fileName);
elseif idx(end) ~= length(allStatements)
error('## '';'' does not exist at the end of the last statement of ''%s''.', fileName);
end
sidx = 1;
eqIdx = ones(1, size(equationNames,2));
pp = 1;
listOfEquations = [];
NewEquationNames = [];
for i=1:length(idx)
eidx = idx(i);
oneLine = allStatements(sidx:eidx);
eNameidx = strfind(oneLine, '..');
usedflag = 0;
if ~isempty(eNameidx)
if eNameidx(1) == 1
error('## ''%s'' has a line which consists of only ''..''.', fileName);
end
eName=oneLine(1:eNameidx-1);
while true
if strcmp(eName(end), blanks(1))
eName = eName(1:end-1);
else
break;
end
end
%eName
for j=1:size(equationNames,2)
if strcmp(equationNames{j}, eName)
%oneLine
tmpidx = strfind(oneLine, '..');
tmpidx = tmpidx + 1;
%equationNames{j}
%tmpidx = length(eName) + 2;
if tmpidx >= length(oneLine)
error('## The %d%s constraint does not have any statemets.', i, thWord(i));
end
statement = oneLine(tmpidx+1:end);
statement = strtrim(statement);
%statement
eqIdx(j) = 0;
NewEquationNames{pp} = eName;
[listOfEquations, pp] = getlistOfEquations(statement, listOfEquations, pp, symbolicMath);
%allStatements(sidx:eidx) = [];
usedflag = 1;
break;
end
end
if usedflag == 0
error('## ''%s'' is not defined in the line of ''Equations''.', eName);
end
end
sidx = eidx + 1;
end
%listOfEquations
%eqIdx
if any(eqIdx) == 1
eqNo = find(eqIdx==1);
error('## The constraint of ''%s'' is not defined in ''%s''.\n## Should check the line of ''Equations'' and the position of '';''.',equationNames{eqNo(1)}, fileName);
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [lbd, ubd, fixed, allStatements] = getLowerUpper(fileName, allStatements, varNames)
% get all upper and lower bounds.
idx = strfind(allStatements, ';');
sidx = 1;
noOfVariables = size(varNames,2);
lbd = -1.0e10* ones(1,noOfVariables);
ubd = 1.0e10* ones(1,noOfVariables);
fixed = lbd;
for i=1:length(idx)
eidx = idx(i);
oneLine = allStatements(sidx:eidx);
sidx = eidx + 1;
pidx = strfind(oneLine, '.');
dpidx= strfind(oneLine,'..');
usedflag = 0;
if ~isempty(pidx) && isempty(dpidx)
if pidx(1) == 1
error('## The variable in the line ''%s'' is undefined.', oneLine);
end
oneVar = oneLine(1:pidx(1)-1);
if pidx(1) == length(oneLine)
error('## The lower or upper bound of ''%s'' is undefined.', onevar);
end
bound = oneLine(pidx(1)+1:end);
for j=1:noOfVariables
if strcmp(oneVar, varNames{j})
eqidx = strfind(bound, '=');
scidx = strfind(bound,';');
if isempty(eqidx)
error('## The line ''%s'' does not have ''=''.', oneLine);
elseif eqidx(1) >= length(bound)
error('## The value of the lower or uppper bound of ''%s'' is not defined.', oneVar);
elseif isempty(scidx)
error('## The line ''%s'' does not have '';''.', oneLine);
elseif scidx(1) < eqidx(1)+2
%scidx(1)
%eqidx(1)+2
error('## The line ''%s'' does not have '';''.', oneLine);
end
asciiVal = bound(eqidx(1)+1:scidx(1)-1);
usedflag = 1;
if isempty(asciiVal)
error('## The value of the lower bound of ''%s'' is undefined.', oneVar);
end
val = str2double(asciiVal);
if isnan(val)
error('## The bound of ''%s'' is ''%s''.\n## This is not a numerical value.',oneVar, asciiVal);
elseif ~isfinite(val)
error('## The bound of ''%s'' should be finite.', oneVar);
end
if strcmp(bound(1:2),'lo')
lbd(1,j) = val;
break;
elseif strcmp(bound(1:2),'up')
ubd(1,j) = val;
break;
elseif strcmp(bound(1:2),'fx')
fixed(1,j) = val;
lbd(1,j) = val;
ubd(1,j) = val;
break;
%else
% error('## ''%s'' is not a reserved keyword.',bound(1:2));
end
end
end
if usedflag == 0
%allStatements
error('## ''%s'' is not defined in the line of ''Variables''.', oneVar);
end
end
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [listOfEquations, pp] = getlistOfEquations(oneLine, listOfEquations, pp, symbolicMath)
%
% 2011-11-18 H.Waki
% Fixed a bug in this function.
idx = find(isspace(oneLine));
oneLine(idx) = [];
if symbolicMath == 1
% new version
if ~isempty(strfind(oneLine,'('))
%x1 = sym('x1');
%x1 = sym('objvar');
%loca = strfind(oneLine,'objvar');
%if isempty(loca)
loca = strfind(oneLine,'=');
loca = loca(1) -1;
%else
%loca = loca(1) -3; % for objvar
% loca = loca(1) -2; % for objvar
%end
%oneLine(1:loca)
if exist('OCTAVE_VERSION','builtin');
tempf = collect(sym(oneLine(1:loca)), 'objvar');
else
%20180911 H. Waki modified
if verLessThan('matlab','9.4')
tempf = collect(sym(oneLine(1:loca)), 'objvar');
else
syms objvar
tempf = collect(str2sym(oneLine(1:loca)), objvar);
end
end
oneLinetmp = char(vpa(expand(tempf),20));
oneLine = strcat(oneLinetmp, oneLine(loca+1:end));
end
% old version
%{
if ~isempty(strfind(oneLine,'('))
%x1 = sym('x1');
x1 = sym('objvar');
%loca = strfind(oneLine,'objvar');
%if isempty(loca)
loca = strfind(oneLine,'=');
loca = loca(1) -1;
%else
%loca = loca(1) -3; % for objvar
% loca = loca(1) -2; % for objvar
%end
%oneLine(1:loca)
oneLinetmp = char(vpa(expand(collect(oneLine(1:loca),x1)),50));
oneLine = strcat(oneLinetmp, oneLine(loca+1:end));
end
%}
else
if ~isempty(strfind(oneLine,'('))
error('Please expand parenthesises by your hand.');
end
end
idx = find(isspace(oneLine));
oneLine(idx) = [];
%oneLine
listOfEquations{pp} = oneLine;
pp = pp+1;
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [varNames,p,moreSW] = getListOfNames(oneLine,varNames,p)
while (~isempty(oneLine))
[oneName,remLine] = strtok(oneLine,' ,');
if ~isempty(oneName)
p = p+1;
varNames{p} = oneName;
end
oneLine = remLine;
end
lenLastVar = length(varNames{p});
if varNames{p}(lenLastVar) == ';'
moreSW = 0;
if lenLastVar == 1
p = p-1;
else
varNames{p} = varNames{p}(1:lenLastVar-1);
end