/[ascend]/trunk/base/generic/solver/conopt.c
ViewVC logotype

Contents of /trunk/base/generic/solver/conopt.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1075 - (show annotations) (download) (as text)
Tue Jan 9 03:15:50 2007 UTC (13 years, 6 months ago) by johnpye
File MIME type: text/x-csrc
File size: 9775 byte(s)
Attempts to fix utilities/config.h and ASC_WITH_CONOPT issues with autotools build
1 /* ASCEND modelling environment
2 Copyright (C) 2006 Carnegie Mellon University
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
18 *//**
19 @file
20 This file allows CONOPT to be dlopened at runtime.
21 *//*
22 By John Pye
23 Based on conopt.c by Vicente Rico Ramirez (created 05/97)
24 */
25
26 #include <utilities/config.h>
27 #include <utilities/ascConfig.h>
28 #include <utilities/error.h>
29 #include <utilities/ascEnvVar.h>
30 #include <general/env.h>
31 #include "conopt.h"
32
33 #ifndef ASC_WITH_CONOPT
34 #ifdef __GNUC__
35 # warning "Shouldn't compile this file unless ASC_WITH_CONOPT set"
36 #endif
37 #else
38
39 #ifndef ASC_LINKED_CONOPT
40 # include <ctype.h>
41 # include <utilities/ascMalloc.h>
42 # include <utilities/ascDynaLoad.h>
43
44 /*------------------------------------------------------------------------------
45 DLOPENING CONOPT SUPPORT FUNCTIONS
46 */
47
48 # define INTINT (int*cntvect,int*v)
49 # define INTINT1 (cntvect,v)
50 # define INTDOUBLE (int*cntvect,double*v)
51 # define INTDOUBLE1 (cntvect,v)
52 # define SEMICOLON ;
53 # define SPACE
54
55 /*
56 Typedefs for the various function pointers
57 */
58 # define FN_TYPE_DECL(T,A,V,L) \
59 typedef int COI_CALL (T##_fn_t) A
60
61 CONOPT_FNS(FN_TYPE_DECL,SEMICOLON);
62 # undef FN_TYPE_DECL
63
64 /*
65 Define a struct to hold all the function pointers, then
66 declare it as a global variable.
67 */
68 # define FN_PTR_DECL(T,A,V,L) \
69 T##_fn_t* T##_ptr
70
71 typedef struct{
72 CONOPT_FNS(FN_PTR_DECL,SEMICOLON);
73 } conopt_fptrs_t;
74 # undef FN_PTR_DECL
75
76 conopt_fptrs_t conopt_fptrs;
77
78
79 /*
80 Declare local functions to hook into the DLL
81 */
82 # define FN_PTR_EXEC(T,A,V,L) \
83 int COI_CALL T A{ \
84 if(conopt_fptrs.T##_ptr==NULL){ \
85 return 1; \
86 } \
87 return (* conopt_fptrs.T##_ptr) V ; \
88 }
89
90 CONOPT_FNS(FN_PTR_EXEC,SPACE)
91
92 # undef FN_PTR_EXEC
93
94 /**
95 This funciton will load the DLL and resolve all the required symbols
96 */
97 int asc_conopt_load(){
98 static int loaded=0;
99 char *libpath;
100 int status;
101 char fnsymbol[400], *c;
102 const char *libname=ASC_CONOPT_LIB;
103 const char *envvar;
104
105 if(loaded) {
106 return 0; /* already loaded */
107 }
108
109 /* CONSOLE_DEBUG("LOADING CONOPT..."); */
110
111 envvar = ASC_CONOPT_ENVVAR;
112
113 /* need to import this variable into the ascend 'environment' */
114 if(-1!=env_import(ASC_CONOPT_ENVVAR,getenv,Asc_PutEnv)){
115 CONSOLE_DEBUG("Searching in path '%s' (from env var '%s')",getenv(envvar),envvar);
116 }/*else{
117 CONSOLE_DEBUG("Default conopt search path: %s", ASC_CONOPT_DLPATH);
118 }*/
119
120 libpath = SearchArchiveLibraryPath(libname, ASC_CONOPT_DLPATH, envvar);
121
122 if(libpath==NULL){
123 ERROR_REPORTER_NOLINE(ASC_PROG_ERR
124 , "Library '%s' could not be located (check value of env var '%s' and/or default path '%s')"
125 , libname, envvar, ASC_CONOPT_DLPATH
126 );
127 return 1;
128 }
129
130 status = Asc_DynamicLoad(libpath, NULL);
131 if (status != 0) {
132 ASC_FREE(libpath);
133 return 1; /* failed to load */
134 }
135
136 # if defined(FNAME_UCASE_NODECOR) || defined(FNAME_UCASE_DECOR) || defined(FNAME_UCASE_PREDECOR)
137 # define FNCASE(C) C=toupper(C)
138 # elif defined(FNAME_LCASE_NODECOR) || defined(FNAME_LCASE_DECOR)
139 # define FNCASE(C) C=tolower(C)
140 # else
141 # error "CONOPT case rule not defined"
142 # endif
143
144 # if defined(FNAME_UCASE_DECOR) || defined(FNAME_LCASE_DECOR)
145 # define FNDECOR(S,L) strcat(S,"_")
146 # elif defined(FNAME_UCASE_PREDECOR) /* on windows, precede with _ and append @L (integer value of L) */
147 # define FNDECOR(S,L) strcat(S,L);for(c=S+strlen(S)+1;c>S;--c){*c=*(c-1);} *S='_';
148 # else
149 # define FNDECOR(S,L) (void)0
150 # endif
151
152 # define FN_PTR_GET(T,A,V,L) \
153 sprintf(fnsymbol,"%s",#T); \
154 for(c=fnsymbol;*c!='\0';++c){ \
155 FNCASE(*c); \
156 } \
157 FNDECOR(fnsymbol,L); \
158 conopt_fptrs.T##_ptr = (T##_fn_t *)Asc_DynamicFunction(libpath,fnsymbol); \
159 if(conopt_fptrs.T##_ptr==NULL)status+=1;
160
161 CONOPT_FNS(FN_PTR_GET,SPACE)
162
163 # undef FN_PTR_GET
164 # undef FNDECOR
165 # undef FNCASE
166
167 ASC_FREE(libpath);
168
169 if(status!=0){
170 return 1; /* faile to result all symbols */
171 }
172
173 loaded = 1;
174 return 0;
175 }
176
177 #endif
178
179 /*-----------------------------------------------------------------------------
180 std.c
181
182 This file has some 'standard' implementations for the mandatory
183 callback routines Message, ErrMsg, Status, and Solution.
184 The routines use global file pointers, so they are only intended
185 as examples that can be used for further refinements.
186 */
187
188 #define MAXLINE 133 /* maximum line length plus an extra character
189 for the null terminator */
190
191 int COI_CALL asc_conopt_progress( int* LEN_INT, int* INT
192 , int* LEN_RL, double* RL, double* X, double* USRMEM
193 ){
194 /*(void)CONSOLE_DEBUG("Iteration %d, phase %d: %d infeasible, %d non-optimal; objective = %e"
195 , INT[0], INT[1], INT[2], INT[3], RL[1]
196 );*/
197 /* NEED TO IMPLEMENT SOME KIND OF CALLBACK TO THE SOLVERREPORTER */
198 return 0;
199 }
200
201 int COI_CALL asc_conopt_message( int* SMSG, int* DMSG, int* NMSG, int* LLEN
202 ,double* USRMEM, char* MSGV, int MSGLEN
203 ){
204 /* This implementation is writing the screen file to stdout
205 the documentation file to a file opened in main with the name
206 document.txt and the status file to a file with the name
207 status.txt. */
208 int i,j,k,l;
209 char line[MAXLINE];
210 k = 0;
211 for( i=0; i<*SMSG;i++ ){
212 j = LLEN[i];
213 for( l= 0; l<j; l++ ) line[l] = MSGV[k+l];
214 line[j] = '\0';
215 CONSOLE_DEBUG("%s", line);
216 k += MSGLEN;
217 }
218 /* k = 0;
219 for( i=0; i<*DMSG;i++ ){
220 j = LLEN[i];
221 for( l= 0; l<j; l++ ) line[l] = MSGV[k+l];
222 line[j] = '\0';
223 ERROR_REPORTER_NOLINE(ASC_PROG_NOTE,"%s\n", line);
224 k += MSGLEN;
225 }
226 */
227 k = 0;
228 for( i=0; i<*NMSG;i++ ){
229 j = LLEN[i];
230 for( l= 0; l<j; l++ ) line[l] = MSGV[k+l];
231 line[j] = '\0';
232 ERROR_REPORTER_NOLINE(ASC_USER_NOTE,"%s\n", line);
233 k += MSGLEN;
234 }
235 return 0;
236 }
237
238 int COI_CALL asc_conopt_errmsg( int* ROWNO, int* COLNO, int* POSNO, int* MSGLEN
239 , double* USRMEM, char* MSG, int LENMSG
240 ){
241 /* Standard ErrMsg routine. Write to Documentation and Status file*/
242 int j,l;
243 char line[MAXLINE];
244 ERROR_REPORTER_START_NOLINE(ASC_PROG_ERR);
245 if ( *ROWNO == -1 ) {
246 FPRINTF(ASCERR,"Variable %d : ",*COLNO); }
247 else if ( *COLNO == -1 ) {
248 FPRINTF(ASCERR,"Equation %d : ",*ROWNO); }
249 else {
250 FPRINTF(ASCERR,"Variable %d appearing in Equation %d : ",*COLNO, *ROWNO); }
251 j = *MSGLEN;
252 for( l= 0; l<j; l++ ) line[l] = MSG[l];
253 line[j] = '\0';
254 FPRINTF(ASCERR,"%s\n", line);
255 error_reporter_end_flush();
256 return 0;
257 }
258
259 int COI_CALL asc_conopt_status(int* MODSTA, int* SOLSTA
260 , int* ITER, double* OBJVAL, double* USRMEM
261 ){
262 /* Standard Status routine. Write to all files */
263 CONSOLE_DEBUG("CONOPT has finished Optimizing");
264 CONSOLE_DEBUG("Model status = %8d", *MODSTA);
265 CONSOLE_DEBUG("Solver status = %8d", *SOLSTA);
266 CONSOLE_DEBUG("Iteration count = %8d", *ITER);
267 CONSOLE_DEBUG("Objective value = %10f", *OBJVAL);
268
269 const char *modsta;
270 error_severity_t t = ASC_USER_SUCCESS;
271 switch(*MODSTA){
272 case 1: modsta = "optimal"; break;
273 case 2: modsta = "locally optimal"; break;
274 case 3: t = ASC_USER_ERROR; modsta = "unbounded"; break;
275 case 4: t = ASC_USER_ERROR; modsta = "infeasible"; break;
276 case 5: modsta = "locally infeasible"; break;
277 case 6: modsta = "intermediate infeasible"; break;
278 case 7: modsta = "intermediate non-optimal"; break;
279 case 12: modsta = "unknown type of error"; break;
280 case 13: modsta = "error no solution"; break;
281 case 15: modsta = "solved unique"; break;
282 case 16: modsta = "solved"; break;
283 case 17: modsta = "solved singular"; break;
284 default: t = ASC_PROG_ERR; modsta = "UNKNOWN MODSTA";
285 }
286 const char *solsta;
287 switch(*SOLSTA){
288 case 1: solsta = "normal completion"; break;
289 case 2: t = ASC_USER_NOTE; solsta = "iteration interrupted"; break;
290 case 3: t = ASC_PROG_NOTE; solsta = "time limit exceeded"; break;
291 case 4: t = ASC_PROG_ERR; solsta = "failed (terminated by solver)"; break;
292 case 5: t = ASC_PROG_ERR; solsta = "Error evaluation limit"; break;
293 case 8: t = ASC_USER_NOTE; solsta = "User interrupt"; break;
294 case 9: t = ASC_PROG_ERR; solsta = "Error: setup failure"; break;
295 case 10:t = ASC_PROG_ERR; solsta = "Error: solver failure"; break;
296 case 11:t = ASC_PROG_ERR; solsta = "Error: internal solver error"; break;
297 case 15:t = ASC_PROG_ERR; solsta = "Terminated by Quick Mode"; break;
298 default: t = ASC_PROG_ERR; solsta = "UNKNOWN SOLSTA";
299 }
300
301 CONSOLE_DEBUG("CONOPT %s (%d): %s (%d)", solsta, *SOLSTA, modsta, *MODSTA);
302 ERROR_REPORTER_NOLINE(t,"CONOPT %s: %s", solsta, modsta);
303
304 return 0;
305 }
306
307 int COI_CALL asc_conopt_solution( double* XVAL, double* XMAR, int* XBAS
308 , int* XSTA, double* YVAL, double* YMAR, int* YBAS, int* YSTA
309 , int* N, int* M, double* USRMEM
310 ){
311 /* Standard Solution routine */
312 int i;
313 char *status[4] = {"Lower","Upper","Basic","Super"};
314 FILE *fd = stderr;
315
316 fprintf(fd,"\n Variable Solution value Reduced cost Status\n\n");
317 for ( i=0; i<*N; i++ )
318 fprintf(fd,"%6d%18f%18f%10s\n", i, XVAL[i], XMAR[i], status[XBAS[i]] );
319 fprintf(fd,"\n Constrnt Activity level Marginal cost Status\n\n");
320 for ( i=0; i<*M; i++ )
321 fprintf(fd,"%6d%18f%18f%10s\n", i, YVAL[i], YMAR[i], status[YBAS[i]] );
322
323 return 0;
324 }
325
326 #endif /* ASC_WITH_CONOPT */

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22