CONOPT
Loading...
Searching...
No Matches
std.c
Go to the documentation of this file.
1/* std.c
2 This file has some 'standard' implementations for the mandatory
3 callback routines Message, ErrMsg, Status, and Solution.
4 The routines use global file pointers, defined in the header file
5 comdecl.h, so they are only intended as examples that can be used
6 for further refinements. */
7
8int COI_CALLCONV Std_Message( int SMSG, int DMSG, int NMSG, char* MSGV[], void* USRMEM )
9{
10/* This implementation is writing the screen file to stdout
11 the documentation file to a file opened in main with the name
12 document.txt and the status file to a file with the name
13 status.txt. */
14 int i;
15
16 for( i=0; i<SMSG;i++ ) printf( "%s\n", MSGV[i]);
17 for( i=0; i<DMSG;i++ ) fprintf(fd,"%s\n", MSGV[i]);
18 for( i=0; i<SMSG;i++ ) fprintf(fs,"%s\n", MSGV[i]);
19
20#ifdef flush
21 fflush(fd); fflush(fs);
22#endif
23 return 0;
24}
25
26int COI_CALLCONV Std_ErrMsg( int ROWNO, int COLNO, int POSNO, const char* MSG, void* USRMEM )
27{
28/* Standard ErrMsg routine. Write to Documentation and Status file*/
29 if ( ROWNO == -1 ) fprintf(fd,"Variable %d : ",COLNO);
30 else if ( COLNO == -1 ) fprintf(fd,"Equation %d : ",ROWNO);
31 else fprintf(fd,"Variable %d appearing in Equation %d : ", COLNO, ROWNO);
32 fprintf(fd,"%s\n", MSG);
33
34 if ( ROWNO == -1 ) fprintf(fs,"Variable %d : ",COLNO);
35 else if ( COLNO == -1 ) fprintf(fs,"Equation %d : ",ROWNO);
36 else fprintf(fs,"Variable %d appearing in Equation %d : ", COLNO, ROWNO);
37 fprintf(fs,"%s\n", MSG);
38
39#ifdef flush
40 fflush(fd); fflush(fs);
41#endif
42 return 0;
43}
44
45int COI_CALLCONV Std_Status( int MODSTA, int SOLSTA, int ITER, double OBJVAL, void* USRMEM )
46{
47/* Standard Status routine. Write to all files */
48 printf("\n");
49 printf("CONOPT has finished Optimizing\n");
50 printf("Model status = %8d\n", MODSTA);
51 printf("Solver status = %8d\n", SOLSTA);
52 printf("Iteration count = %8d\n", ITER);
53 printf("Objective value = %10f\n", OBJVAL);
54
55 fprintf(fd,"\n");
56 fprintf(fd,"CONOPT has finished Optimizing\n");
57 fprintf(fd,"Model status = %8d\n", MODSTA);
58 fprintf(fd,"Solver status = %8d\n", SOLSTA);
59 fprintf(fd,"Iteration count = %8d\n", ITER);
60 fprintf(fd,"Objective value = %10f\n", OBJVAL);
61
62 fprintf(fs,"\n");
63 fprintf(fs,"Model status = %8d\n", MODSTA);
64 fprintf(fs,"Solver status = %8d\n", SOLSTA);
65 fprintf(fs,"Objective value = %10f\n", OBJVAL);
66
67 stacalls++;
68 OBJ = OBJVAL;
69 mstat = MODSTA;
70 sstat = SOLSTA;
71#ifdef flush
72 fflush(fd); fflush(fs);
73#endif
74 return 0;
75}
76
77int COI_CALLCONV Std_Solution( const double XVAL[], const double XMAR[], const int XBAS[], const int XSTA[],
78 const double YVAL[], const double YMAR[], const int YBAS[], const int YSTA[],
79 int NUMVAR, int NUMCON, void* USRMEM )
80{
81/* Standard Solution routine */
82 int i;
83 char *status[4] = {"Lower","Upper","Basic","Super"};
84 fprintf(fd,"\n Variable Solution value Reduced cost Status\n\n");
85 for ( i=0; i<NUMVAR; i++ )
86 fprintf(fd,"%6d%18f%18f%10s\n", i, XVAL[i], XMAR[i], status[XBAS[i]] );
87 fprintf(fd,"\n Constrnt Activity level Marginal cost Status\n\n");
88 for ( i=0; i<NUMCON; i++ )
89 fprintf(fd,"%6d%18f%18f%10s\n", i, YVAL[i], YMAR[i], status[YBAS[i]] );
90
91 solcalls++;
92#ifdef flush
93 fflush(fd);
94#endif
95 return 0;
96}
97
98int COI_CALLCONV Std_TriOrd( int mode, int type, int status, int irow, int icol, int inf, double value, double resid, void* USRMEM )
99/*
100 Simple implementation of the TriOrd callback routine.
101*/
102{
103 enum types { Pretriangular = 1, /* Fix a structural variable from an equality equation */
104 FixedColumn = 2, /* Fix a structural variable from equal lower and upper bouunds */
105 DependentRow = 3, /* All structural variables are fixed and the row is feasible */
106 RedundantRow = 4, /* The row is feasible due to bounds on all variables. */
107 ImpliedLower = 5, /* Change a simple inequality to a lower bound */
108 ImpliedUpper = 6, /* Change a simple inequality to an upper bound */
109 ImpliedRange = 7, /* Change a ranged constraint into both a lower and an upper bound
110 This routine is called twice, first with lower then with upper
111 bound */
112 ForcedLower = 8, /* A variable is forced to the lower bound by a ForcingLower or */
113 /* ForcingUpper constraint. */
114 ForcedUpper = 9, /* A variable is forced to the upper bound by a ForcingLower or */
115 /* ForcingUpper constraint. */
116 ForcedValue = 10, /* A variable if forced to a value equal to an implied but not */
117 /* original bound by a ForcingLower or ForcingUpper constraint */
118 ForcingLower = 11, /* A constraint is forcing all variables in it to a bound */
119 /* and it works as an =L= constraint. */
120 ForcingUpper = 12, /* A constraint is forcing all variables in it to a bound */
121 /* and it works as an =G= constraint. */
122/*
123 Any of the next actions will terminate the pre-processor and there can therefore only be one of them
124 and only in an infeasible model.
125*/
126 PreTriaInfeasB = 13, /* An equation has only one nonfixed variable, but it cannot */
127 /* be solved either because the variable will exceed a bound. */
128 PreTriaInfeasP = 14, /* An equation has only one nonfixed variable, but it cannot */
129 /* be solved either because the pivot (derivative) is too small. */
130 ForcingInfeas = 15, /* A constraint is infeasible due to bounds on all variables */
131 InconsistentRow = 16 /* All structural variables are fixed and the row in infeasible */
132 /* or the row is feasible due to bounds on all variables. */
133 };
134
135 if ( -1 == mode )
136 fprintf(fd,"\n The preprocessing transformation are described below in detection order:'\n\n");
137 else if ( -2 == mode )
138 fprintf(fd,"\n The order of the critical transformations is:\n\n");
139 switch (type) {
140 case Pretriangular:
141 if ( 0 == inf )
142 fprintf(fd,"Equation %6d solved with respect to variable %6d. Value= %18f\n", irow, icol, value );
143 else if ( 1 == inf )
144 fprintf(fd,"Equation %6d solved with respect to variable %6d. Value= +Infinity\n", irow, icol );
145 else
146 fprintf(fd,"Equation %6d solved with respect to variable %6d. Value= -Infinity\n", irow, icol );
147 break;
148 case FixedColumn:
149 fprintf(fd,"Variable %6d fixed at value= %18f\n", icol, value );
150 break;
151 case DependentRow:
152 fprintf(fd,"Equation %6d is dependent (all variables are fixed).\n", irow );
153 break;
154 case RedundantRow:
155 fprintf(fd,"Equation %6d is redundant (will later be solved with respect to the slack.\n", irow );
156 break;
157 case ImpliedLower:
158 fprintf(fd,"Equation %6d turned into lower bound on variable %6d. Bound= %18f\n", irow, icol, value );
159 break;
160 case ImpliedUpper:
161 fprintf(fd,"Equation %6d turned into upper bound on variable %6d. Bound= %18f\n", irow, icol, value );
162 break;
163 case ImpliedRange:
164 fprintf(fd,"Ranged Equation %6d turned into two bounds on variable %6d. Bound= %18f\n", irow, icol, value );
165 break;
166 case ForcedLower:
167 fprintf(fd,"Variable %6d forced to lower bound= %18f\n", icol, value );
168 break;
169 case ForcedUpper:
170 fprintf(fd,"Variable %6d forced to upper bound= %18f\n", icol, value );
171 break;
172 case ForcedValue:
173 fprintf(fd,"Variable %6d forced to implied bound= %18f\n", icol, value );
174 break;
175 case ForcingLower:
176 fprintf(fd,"Previous variables were forced by equation %6d binding as a less than or equal constraint.\n", irow );
177 break;
178 case ForcingUpper:
179 fprintf(fd,"Previous variables were forced by equation %6d binding as a greater than or equal constraint.\n", irow );
180 break;
181 case PreTriaInfeasB:
182 fprintf(fd,"Equation %6d cannot be solved with respect to variable %6d due to bounds. Value= %18f\n", irow, icol, value );
183 break;
184 case PreTriaInfeasP:
185 fprintf(fd,"Equation %6d cannot be solved with respect to variable %6d. Infeasibility has local minimum at %18f\n", irow, icol, value );
186 break;
187 case ForcingInfeas:
188 fprintf(fd,"Equation %6d is still infeasible after the above variables have been moved to the best bounds.\n", irow );
189 break;
190 case InconsistentRow:
191 fprintf(fd,"Equation %6d cannot be solved. No free variables left.\n", irow );
192 break;
193 default:
194 fprintf(fd,"Pretriangular action= %6d not implemented yet.\n", type );
195 exit(1);
196 }
197 if ( resid != 0.0 ) fprintf(fd,"Residual after last transformation= %18f\n", resid );
198
199 return 0;
200}
201
202void c_log( char *msgt, int code )
203{
204/* c_log manages the initialization and the return code file.
205 It is called exactly twice:
206 The first time with code = START and
207 the second with code = ReturnCode for overall program.
208 During the START call we allocate and initialize the CONOPT
209 control vector, read the name of the program from stdin, and
210 open the files <progname>.lst on fd and <progname>.sta on fs.
211 During the second call we close these files, free the control
212 vector, and exit with 0 if code = OK and otherwise exit with
213 the value of code.
214 The test in the first argument, msgt, is written to the return
215 code file <progname>.rc. */
216
217 FILE *fc;
218 int i;
219 int c;
220 char msgc[256];
221
222 if ( code == START )
223 {
224 i = 0; /* Read the name of the program without extension from stdin */
225 while ( (c = getchar()) != EOF && i < MAXLINE ) pname[i++] = (char)c;
226 pname[i-1] = '\0'; /* remove eof and new-line chars */
228 if (NULL==CntVect) {
229 printf("Could not create CONOPT object: %s\n", msgc);
230 strcpy(fname,pname); strcat(fname,".rc" );
231 fc = fopen(fname,"w");
232 fprintf(fc,"%s: Could not create CONOPT object.\n", pname );
233 fclose(fc);
234 exit(1);
235 }
236 strcpy(fname,pname); strcat(fname,".lst" );
237 fd = fopen(fname,"w");
238 strcpy(fname,pname); strcat(fname,".sta" );
239 fs = fopen(fname,"w");
240 }
241 strcpy(fname,pname); strcat(fname,".rc" );
242 fc = fopen(fname,"w");
243 fprintf(fc,"%s: %s.\n", pname, msgt );
244 fclose(fc);
245 if ( code != START )
246 {
247 fclose(fd);
248 fclose(fs);
250 coiFinalize();
251 if ( code == OK )
252 exit(0);
253 else
254 exit(code);
255 }
256}
int stacalls
Definition comdecl.h:4
char pname[MAXLINE]
Definition comdecl.h:10
char fname[MAXLINE]
Definition comdecl.h:11
coiHandle_t CntVect
Definition comdecl.h:14
#define START
Definition comdecl.h:13
#define MAXLINE
Definition comdecl.h:9
FILE * fd
Definition comdecl.h:3
int solcalls
Definition comdecl.h:5
double OBJ
Definition comdecl.h:6
int mstat
Definition comdecl.h:7
#define OK
Definition comdecl.h:12
int sstat
Definition comdecl.h:8
FILE * fs
Definition comdecl.h:2
void COI_CALLCONV coiFinalize(void)
finializes the solving process for CONOPT. This must be called when using OpenMP. It will terminate t...
int COI_CALLCONV coiCreate(coiHandle_t *cntvect)
initialises and create the control vector.
int COI_CALLCONV coiFree(coiHandle_t *cntvect)
frees the control vector.
int COI_CALLCONV Std_Status(int MODSTA, int SOLSTA, int ITER, double OBJVAL, void *USRMEM)
Definition std.c:45
int COI_CALLCONV Std_Message(int SMSG, int DMSG, int NMSG, char *MSGV[], void *USRMEM)
Definition std.c:8
int COI_CALLCONV Std_TriOrd(int mode, int type, int status, int irow, int icol, int inf, double value, double resid, void *USRMEM)
Definition std.c:98
void c_log(char *msgt, int code)
Definition std.c:202
int COI_CALLCONV Std_ErrMsg(int ROWNO, int COLNO, int POSNO, const char *MSG, void *USRMEM)
Definition std.c:26
int COI_CALLCONV Std_Solution(const double XVAL[], const double XMAR[], const int XBAS[], const int XSTA[], const double YVAL[], const double YMAR[], const int YBAS[], const int YSTA[], int NUMVAR, int NUMCON, void *USRMEM)
Definition std.c:77