source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/opt/yalmip/extras/saveampl.m @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 6.4 KB
Line 
1function solution = saveampl(varargin)
2%SAVEAMPL Saves a problem definition in AMPL format
3%
4%    SAVEAMPL(F,h,'filename')    Saves the problem min(h(x)), F(x)>0 to the file filename
5%    SAVEAMPL(F,h)               A "Save As"- box will be opened
6%
7% YALMIP is currently able to save problems with linear and non-linear
8% element-wise inequality and equality constraints. Integer and binary
9% variables are also supported.
10%
11% Note that YALMIP changes the variable names. Continuous variables
12% are called x, binary are called y while z denotes integer variables.
13
14% Author Johan Löfberg
15% $Id: saveampl.m,v 1.7 2006/06/02 12:18:07 joloef Exp $
16
17F = varargin{1};
18h = varargin{2};
19
20% Expand nonlinear operators
21[F,failure,cause] = expandmodel(F,h,sdpsettings);
22if failure % Convexity propgation failed
23    interfacedata = [];
24    recoverdata = [];
25    solver = '';
26    diagnostic.solvertime = 0;
27    diagnostic.problem = 14;
28    diagnostic.info = yalmiperror(14,cause);
29    return
30end
31
32nvars = yalmip('nvars');
33
34vars = depends(F);
35vars = unique([vars depends(h)]);
36
37binvars = yalmip('binvariables');
38integervars = yalmip('intvariables');
39
40for i = 1:length(F)
41    if is(F(i),'binary')
42        binvars = [binvars depends(F(i))];
43    elseif  is(F(i),'integer')
44        integervars = [integervars depends(F(i))];
45    end
46end
47
48binvars = intersect(binvars,vars);
49integervars = intersect(integervars,vars);
50
51%binvars = setdiff(binvars,vars);
52%integervars = setdiff(integervars,vars);
53vars = setdiff(vars,union(integervars,binvars));
54integervars = setdiff(integervars,binvars);
55obj = amplexpr(h,vars,binvars,integervars);
56constraints = {};
57
58if ~isempty(F)
59    for i = 1:length(F)
60        if is(F(i),'element-wise')
61            C = sdpvar(F(i));C=C(:);
62            dummy = amplexpr(C,vars,binvars,integervars);
63            for j = 1:length(C)
64                constraints{end+1} = ['0 <= ' dummy{j}];
65            end
66        elseif is(F(i),'socp')
67            C = sdpvar(F(i));C=C(:);
68            dummy = amplexpr(C(1)^2-C(2:end)'*C(2:end),vars,binvars,integervars);
69            constraints{end+1} = ['0 <= ' dummy{1}];
70            dummy = amplexpr(C(1),vars,binvars,integervars);
71            constraints{end+1} = ['0 <= ' dummy{1}];
72           
73        elseif is(F(i),'equality')
74            C = sdpvar(F(i));C=C(:);
75            dummy = amplexpr(C,vars,binvars,integervars);
76            for j = 1:length(C)
77                constraints{end+1} = ['0 == ' dummy{j}];
78            end
79        end
80    end
81end
82
83% Is a filename supplied
84if nargin<3
85    [filename, pathname] = uiputfile('*.mod', 'Save AMPL format file');
86    if isa(filename,'double')
87        return % User cancelled
88    else
89        % Did the user change the extension
90        if isempty(findstr(filename,'.'))
91            filename = [pathname filename '.mod'];
92        else
93            filename = [pathname filename];
94        end
95    end
96else
97    filename = varargin{3};
98end
99
100fid = fopen(filename,'w');
101try
102
103    %  fprintf(fid,['option randseed 0;\r\n']);
104    if length(vars)>0
105        fprintf(fid,['var x {1..%i};\r\n'],length(vars));
106    end
107    if length(binvars)>0
108        fprintf(fid,['var y {1..%i} binary ;\r\n'],length(binvars));
109    end
110    if length(integervars)>0
111        fprintf(fid,['var z {1..%i} integer ;\r\n'],length(integervars));
112    end
113
114
115    fprintf(fid,['minimize obj:  ' obj{1} ';'],max(vars));
116    fprintf(fid,'\r\n');
117
118    if length(constraints)>0
119        for i = 1:length(constraints)
120            fprintf(fid,['subject to constr%i: ' constraints{i} ';'],i);
121            fprintf(fid,'\r\n');
122        end
123    end
124
125    fprintf(fid,'solve;\r\n');
126    if length(vars)>0
127        fprintf(fid,'display x;\r\n');
128    end
129    if length(binvars)>0
130        fprintf(fid,'display y;\r\n');
131    end
132    if length(integervars)>0
133        fprintf(fid,'display z;\r\n');
134    end
135
136    fprintf(fid,'display obj;\r\n');
137catch
138    fclose(fid);
139end
140fclose(fid);
141
142
143function symb_pvec = amplexpr(pvec,vars,binvars,integervars)
144
145for pi = 1:size(pvec,1)
146    for pj = 1:size(pvec,2)
147        p = pvec(pi,pj);
148
149        if isa(p,'double')
150            symb_p = num2str(p,12);
151        else
152            LinearVariables = depends(p);
153            x = recover(LinearVariables);
154            exponent_p = full(exponents(p,x));
155            names = cell(length(LinearVariables),1);
156            for i = 1:length(LinearVariables)
157                v1 = find(vars==LinearVariables(i));
158                if ~isempty(v1)
159                    names{i}=['x[' num2str(find(vars==LinearVariables(i))) ']'];
160                else
161                    v1 = find(binvars==LinearVariables(i));
162                    if ~isempty(v1)
163                        names{i}=['y[' num2str(find(binvars==LinearVariables(i))) ']'];
164                    else
165                        names{i}=['z[' num2str(find(integervars==LinearVariables(i))) ']'];
166                    end
167                end
168            end
169
170            symb_p = '';
171            if all(exponent_p(1,:)==0)
172                symb_p = num2str(full(getbasematrix(p,0)),12);
173                exponent_p = exponent_p(2:end,:);
174            end
175
176            for i = 1:size(exponent_p,1)
177                coeff = getbasematrixwithoutcheck(p,i);
178                switch full(coeff)
179                    case 1
180                        coeff='+';
181                    case -1
182                        coeff = '-';
183                    otherwise
184                        if coeff >0
185                            coeff = ['+' num2str2(coeff)];
186                        else
187                            coeff=[num2str2(coeff)];
188                        end
189                end
190                if strcmp(symb_p,'') & (strcmp(coeff,'+') | strcmp(coeff,'-'))
191                    symb_p = [symb_p coeff symbmonom(names,exponent_p(i,:))];
192                else
193                    symb_p = [symb_p coeff '*' symbmonom(names,exponent_p(i,:))];
194                end
195            end
196            if symb_p(1)=='+'
197                symb_p = symb_p(2:end);
198            end
199        end
200
201        symb_p = strrep(symb_p,'+*','+');
202        symb_p = strrep(symb_p,'-*','-');
203        symb_pvec{pi,pj} = symb_p;
204    end
205end
206
207function s = symbmonom(names,monom)
208s = '';
209for j = 1:length(monom)
210    if monom(j)>0
211        if strcmp(s,'')
212            s = [s names{j}];
213        else
214            s = [s '*' names{j}];
215        end
216    end
217    if monom(j)>1
218        s = [s '^' num2str(monom(j))];
219    end
220end
221
222function s = num2str2(x)
223s = num2str(full(x),12);
224if isequal(s,'1')
225    s = '';
226end
227if isequal(s,'-1')
228    s = '-';
229end
230 
Note: See TracBrowser for help on using the repository browser.