CONOPT
Loading...
Searching...
No Matches
mp_polygon.f90
Go to the documentation of this file.
1!> @file mp_polygon.f90
2!! @ingroup FORTOPENMP_EXAMPLES
3!!
4!!
5!! Polygon model from COPS test set.
6!!
7!!
8!! For more information about the individual callbacks, please have a look at the source code.
9
10#if defined(_WIN32) && !defined(_WIN64)
11#define dec_directives_win32
12#endif
13
14Module pdata
15 Integer, Parameter :: NP = 150 ! Number of polygon points
16 Integer, Parameter :: NV = 2*np ! Number of variables
17 Integer, Parameter :: NO = np-1 ! Number of Ordered constraints
18 Integer, Parameter :: ND = np*(np-1)/2 ! Number of distance constraints
19 Integer, dimension(ND) :: Rowmapi, RowmapJ ! Map from distance index to i and j
20 Integer, dimension(NP,NP) :: rowmapk
21end module pdata
22
23!> Main program. A simple setup and call of CONOPT
24!!
25Program polygon
26
28 Use conopt
29 Use pdata
30 Use omp_lib
31 implicit None
32!
33! Declare the user callback routines as Integer, External:
34!
35 Integer, External :: poly_readmatrix ! Mandatory Matrix definition routine defined below
36 Integer, External :: poly_fdeval ! Function and Derivative evaluation routine
37 ! needed a nonlinear model.
38 Integer, External :: std_status ! Standard callback for displaying solution status
39 Integer, External :: std_solution ! Standard callback for displaying solution values
40 Integer, External :: std_message ! Standard callback for managing messages
41 Integer, External :: std_errmsg ! Standard callback for managing error messages
42#ifdef dec_directives_win32
43!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Poly_ReadMatrix
44!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Poly_FDEval
45!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
46!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
47!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
48!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
49#endif
50!
51! Control vector
52!
53 INTEGER, Dimension(:), Pointer :: cntvect
54 INTEGER :: coi_error
55
56 real*8 time0, time1, time2
57 Integer :: iter1, iter2
58!
59! The model shown here in GAMS format
60!
61! set i sides / i1*i%nv% /;
62!
63! alias (i,j);
64!
65! scalar pi a famous constant;
66!
67!
68! positive variables
69! r(i) polar radius (distance to fixed vertex)
70! theta(i) polar angle (measured from fixed direction)
71! variable polygon_area
72!
73! equations
74! obj
75! distance(i,j)
76! ordered(i) ;
77!
78! obj.. polygon_area =e= 0.5*sum(j(i+1), r(i+1)*r(i)*sin(theta(i+1)-theta(i)));
79!
80! ordered(i+1).. theta(i) =l= theta(i+1);
81!
82! distance(i,j)$(ord(j) > ord(i))..
83! sqr(r(i)) + sqr(r(j)) - 2*r(i)*r(j)*cos(theta(j)-theta(i)) =l= 1;
84!
85!
86! pi = 2*arctan(inf);
87!
88! r.up(i) = 1;
89! theta.up(i) = pi;
90!
91! r.fx('i%nv%') = 0;
92! theta.fx('i%nv%') = pi;
93!
94! r.l(i) = 4*ord(i)*(card(i)+ 1- ord(i))/sqr(card(i)+1);
95! theta.l(i) = pi*ord(i)/card(i);
96!
97! model polygon /all/;
98!
99! $if set workspace polygon.workspace = %workspace%;
100!
101! solve polygon using nlp maximizing polygon_area;
102!
103! Create and initialize a Control Vector
104!
105 call startup
106
107 coi_error = coi_create( cntvect )
108!
109! Tell CONOPT about the size of the model by populating the Control Vector:
110!
111 coi_error = max( coi_error, coidef_numvar( cntvect, nv ) ) ! # variables
112 coi_error = max( coi_error, coidef_numcon( cntvect, no + nd + 1 ) ) ! # constraints
113 coi_error = max( coi_error, coidef_numnz( cntvect, 2*no+4*nd+nv ) ) ! # nonzeros in the Jacobian
114 coi_error = max( coi_error, coidef_numnlnz( cntvect, 4*nd+nv ) ) ! # of which are nonlinear
115 coi_error = max( coi_error, coidef_optdir( cntvect, +1 ) ) ! Maximize
116 coi_error = max( coi_error, coidef_objcon( cntvect, no + nd + 1 ) ) ! Objective is last constraint
117 coi_error = max( coi_error, coidef_optfile( cntvect, 'mp_polygon.opt' ) )
118!
119! Tell CONOPT about the callback routines:
120!
121 coi_error = max( coi_error, coidef_readmatrix( cntvect, poly_readmatrix ) )
122 coi_error = max( coi_error, coidef_fdeval( cntvect, poly_fdeval ) )
123 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
124 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
125 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
126 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
127
128#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
129 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
130#endif
131
132 If ( coi_error .ne. 0 ) THEN
133 write(*,*)
134 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
135 write(*,*)
136 call flog( "Skipping Solve due to setup errors", 1 )
137 ENDIF
138 coi_error = max( coi_error, coidef_itlim( cntvect, 50 ) ) ! only 50 iterations to get reasonable time
139!
140! Start CONOPT -- First time using a single thread:
141!
142 time0 = omp_get_wtime()
143 coi_error = coi_solve( cntvect )
144 time1 = omp_get_wtime() - time0
145
146 write(*,*)
147 write(*,*) 'End of Polygon example with one thread. Return code=',coi_error
148
149 If ( coi_error /= 0 ) then
150 call flog( "One thread: Errors encountered during solution", 1 )
151 elseif ( stacalls == 0 .or. solcalls == 0 ) then
152 call flog( "One thread: Status or Solution routine was not called", 1 )
153! elseif ( sstat /= 1 .or. mstat /= 2 ) then
154! call flog( "One thread: Solver and Model Status was not as expected (1,2)", 1 )
155 endif
156 iter1 = miter
157!
158! Start CONOPT again -- this time using multiple threads:
159!
160 coi_error = max( coi_error, coidef_threads( cntvect, 0 ) ) ! 0 means use as many threads as you can
161 time0 = omp_get_wtime()
162 coi_error = coi_solve( cntvect )
163 time2 = omp_get_wtime() - time0
164
165 write(*,*)
166 write(*,*) 'End of Polygon example with multiple threads. Return code=',coi_error
167
168 If ( coi_error /= 0 ) then
169 call flog( "Multi thread: Errors encountered during solution", 1 )
170 elseif ( stacalls == 0 .or. solcalls == 0 ) then
171 call flog( "Multi thread: Status or Solution routine was not called", 1 )
172! elseif ( sstat /= 1 .or. mstat /= 2 ) then
173! call flog( "Multi thread: Solver and Model Status was not as expected (1,2)", 1 )
174 endif
175 iter2 = miter
176
177 if ( coi_free( cntvect ) /= 0 ) call flog( "Error while freeing control vector", 1 )
178
179 write(*,*)
180 write(*,"('Time for single thread',f10.3)") time1
181 write(*,"('Time for multi thread',f10.3)") time2
182 write(*,"('Speedup ',f10.3)") time1/time2
183 write(*,"('Efficiency ',f10.3)") time1/time2/omp_get_max_threads()
184 write(*,"('Speedup/iteration ',f10.3)") time1/time2/iter1*iter2
185 write(*,"('Efficiency/iteration ',f10.3)") time1/time2/iter1*iter2/omp_get_max_threads()
186
187 call flog( "Successful Solve", 0 )
188
189End Program polygon
190!
191! ============================================================================
192! Define information about the model:
193!
194
195!> Define information about the model
196!!
197!! @include{doc} readMatrix_params.dox
198Integer Function poly_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
199 colsta, rowno, value, nlflag, n, m, nz, &
200 usrmem )
201#ifdef dec_directives_win32
202!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Poly_ReadMatrix
203#endif
204 Use pdata
205 implicit none
206 integer, intent (in) :: n ! number of variables
207 integer, intent (in) :: m ! number of constraints
208 integer, intent (in) :: nz ! number of nonzeros
209 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
210 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
211 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
212 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
213 ! (not defined here)
214 integer, intent (out), dimension(m) :: type ! vector of equation types
215 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
216 ! (not defined here)
217 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
218 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
219 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
220 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
221 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
222 real*8 usrmem(*) ! optional user memory
223
224 Integer :: i, j, k
225 real*8, parameter :: pi = 3.141592
226
227!
228! Information about Variables:
229! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
230! Default: the status information in Vsta is not used.
231!
232! r.up(i) = 1;
233! theta.up(i) = pi;
234!
235! r.fx('i%nv%') = 0;
236! theta.fx('i%nv%') = pi;
237!
238! r.l(i) = 4*ord(i)*(card(i)+ 1- ord(i))/sqr(card(i)+1);
239! theta.l(i) = pi*ord(i)/card(i);
240 DO i = 1, np-1
241 lower(i) = 0.d0
242 lower(i+np) = 0.d0
243 upper(i) = 1.d0
244 upper(i+np) = pi
245 curr(i) = 4.d0*i*(np+1-i)/(1+np)**2
246 curr(i+np) = pi*i/np
247 enddo
248 lower(np) = 0.d0
249 upper(np) = 0.d0
250 curr(np) = 0.d0
251 lower(2*np) = pi
252 upper(2*np) = pi
253 curr(2*np) = pi
254!
255! Information about Constraints:
256! Default: Rhs = 0
257! Default: the status information in Esta and the function
258! value in FV are not used.
259! Default: Type: There is no default.
260! 0 = Equality,
261! 1 = Greater than or equal,
262! 2 = Less than or equal,
263! 3 = Non binding.
264!
265! ordered(i+1).. theta(i) =l= theta(i+1);
266!
267! distance(i,j)$(ord(j) > ord(i))..
268! sqr(r(i)) + sqr(r(j)) - 2*r(i)*r(j)*cos(theta(j)-theta(i)) =l= 1;
269!
270! obj.. polygon_area =e= 0.5*sum(j(i+1), r(i+1)*r(i)*sin(theta(i+1)-theta(i)));
271!
272 k = 0
273 DO i = 1, no
274 k = k + 1
275 Type(k) = 2 ! Ordered constraint Less than and RHS = 0
276 Enddo
277 rowmapk = 0 ! Initialize so we can recognize (i,j) combinations that are not equations
278 DO i = 1, np-1
279 DO j = i+1, np
280 k = k + 1
281 Type(k) = 2 ! Dist(i,j) Less than
282 rhs(k) = 1.d0
283 rowmapi(k-no) = i ! To be used for easier lookup
284 rowmapj(k-no) = j
285 rowmapk(i,j) = k
286 Enddo
287 Enddo
288 k = k + 1
289 type(k) = 3 ! Objective of type Non binding
290!
291! Information about the Jacobian. We have to define Rowno, Value,
292! Nlflag and Colsta.
293!
294! Colsta = Start of column indices (No Defaults):
295! Rowno = Row indices
296! Value = Value of derivative (by default only linear
297! derivatives are used)
298! Nlflag = 0 for linear and 1 for nonlinear derivative
299! (not needed for completely linear models)
300!
301! Indices
302!
303! r(1) r(2) r(3) r(4) .. Th(1) th(2) th(3) th(4)
304! 1: 1 -1
305! 2: 1 -1
306! 3: 1 -1
307! ..
308! NP: NL NL NL NL
309! NP+1: NL NL NL NL
310! NP+2 NL NL NL NL
311! ..
312! NL NL NL NL
313! NL NL NL NL
314! ..
315! Obj: NL NL NL NL NL NL
316!
317! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
318! All nonzeros are nonlinear
319!
320 k = 0
321 DO i = 1, np ! r variables
322 colsta(i) = k+1
323 DO j = i+1, np
324 k = k + 1
325 rowno(k) = rowmapk(i,j) ! Distance - i index
326 nlflag(k) = 1
327 enddo
328 DO j = 1,np
329 IF ( rowmapk(j,i) > 0 ) Then
330 k = k + 1
331 rowno(k) = rowmapk(j,i) ! Distance - j index
332 nlflag(k) = 1
333 endif
334 enddo
335 k = k + 1
336 rowno(k) = no+nd+1 ! Objective constraint
337 nlflag(k) = 1
338 enddo
339 DO i = 1, np ! theta variables
340 colsta(np+i) = k+1
341 if ( i < np ) then
342 k = k + 1
343 rowno(k) = i ! ordered constraint index i
344 nlflag(k) = 0
345 value(k) = 1.d0
346 endif
347 if ( i > 1 ) then
348 k = k + 1
349 rowno(k) = i-1 ! ordered constraint index i+1
350 nlflag(k) = 0
351 value(k) = -1.d0
352 endif
353 DO j = i+1, np
354 k = k + 1
355 rowno(k) = rowmapk(i,j) ! Distance - i index
356 nlflag(k) = 1
357 enddo
358 DO j = 1,np
359 IF ( rowmapk(j,i) > 0 ) Then
360 k = k + 1
361 rowno(k) = rowmapk(j,i) ! Distance - j index
362 nlflag(k) = 1
363 endif
364 enddo
365 k = k + 1
366 rowno(k) = no+nd+1 ! Objective constraint
367 nlflag(k) = 1
368 enddo
369 colsta(nv+1) = k+1
371 poly_readmatrix = 0 ! Return value means OK
372
373end Function poly_readmatrix
374!
375!==========================================================================
376! Compute nonlinear terms and non-constant Jacobian elements
377!
378
379!> Compute nonlinear terms and non-constant Jacobian elements
380!!
381!! @include{doc} fdeval_params.dox
382Integer Function poly_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
383 n, nz, thread, usrmem )
384#ifdef dec_directives_win32
385!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Poly_FDEval
386#endif
387 Use pdata
388 implicit none
389 integer, intent (in) :: n ! number of variables
390 integer, intent (in) :: rowno ! number of the row to be evaluated
391 integer, intent (in) :: nz ! number of nonzeros in this row
392 real*8, intent (in), dimension(n) :: x ! vector of current solution values
393 real*8, intent (in out) :: g ! constraint value
394 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
395 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
396 ! in this row. Ffor information only.
397 integer, intent (in) :: mode ! evaluation mode: 1 = function value
398 ! 2 = derivatives, 3 = both
399 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
400 ! as errcnt is incremented
401 integer, intent (in out) :: errcnt ! error counter to be incremented in case
402 ! of function evaluation errors.
403 integer, intent (in) :: thread
404 real*8 usrmem(*) ! optional user memory
405
406 Integer :: ne
407 Integer :: i, j
408 real*8 :: s, c
409
410 ne = n / 3
411!
412! Row NO+ND+1: the objective function is nonlinear
413!
414 if ( rowno == no+nd+1 ) then
415!
416! Mode = 1 or 3. Function value:
417! obj.. polygon_area =e= 0.5*sum(j(i+1), r(i+1)*r(i)*sin(theta(i+1)-theta(i)));
418!
419 if ( mode .eq. 1 .or. mode .eq. 3 ) then
420 g = 0.d0
421 do i = 1, np-1
422 g = g + x(i+1)*x(i)*sin(x(np+i+1)-x(np+i))
423 enddo
424 g = g * 0.5d0
425 endif
426!
427! Mode = 2 or 3: Derivative values: w.r.t. x: 2*(x(i)-x(j))*(-1/2)*dist(-3/2)
428!
429 if ( mode .eq. 2 .or. mode .eq. 3 ) then
430 do i = 1, nv
431 jac(i) = 0.d0
432 enddo
433 do i = 1, np-1
434! g = g + 0.5* x(i+1)*x(i)*Sin(x(NP+i+1)-x(NP+i))
435 s = 0.5d0*sin(x(np+i+1)-x(np+i))
436 jac(i+1) = jac(i+1) + x(i)*s
437 jac(i) = jac(i) + x(i+1)*s
438 c = 0.5d0*x(i+1)*x(i)*cos(x(np+i+1)-x(np+i))
439 jac(np+i+1) = jac(np+i+1) + c
440 jac(np+i) = jac(np+i) - c
441 enddo
442 endif
443!
444 Else if ( rowno > no ) then ! this is a distance constraint
445!
446! Mode = 1 or 3. Function value:
447! sqr(r(i)) + sqr(r(j)) - 2*r(i)*r(j)*cos(theta(j)-theta(i)) =l= 1;
448!
449 i = rowmapi(rowno-no)
450 j = rowmapj(rowno-no)
451 if ( mode .eq. 1 .or. mode .eq. 3 ) then
452 g = x(i)**2 + x(j)**2 -2.d0*x(i)*x(j)*cos(x(j+np)-x(i+np))
453 endif
454!
455! Mode = 2 or 3: Derivative values:
456!
457 if ( mode .eq. 2 .or. mode .eq. 3 ) then
458 c = cos(x(j+np)-x(i+np))
459 jac(i) = 2.d0*(x(i)-x(j)*c)
460 jac(j) = 2.d0*(x(j)-x(i)*c)
461 s = 2.d0*sin(x(j+np)-x(i+np))*x(i)*x(j)
462 jac(i+np) = -s
463 jac(j+np) = s
464 endif
465 Else
466 write(*,*) 'Fdeval called with rowno=',rowno
467 poly_fdeval = 1; return ! error in row number
468 endif
469 poly_fdeval = 0
470
471end Function poly_fdeval
integer function std_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition comdecl.f90:132
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:88
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:205
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:248
integer(c_int) function coidef_message(cntvect, coi_message)
define callback routine for handling messages returned during the solution process.
Definition conopt.f90:1265
integer(c_int) function coidef_solution(cntvect, coi_solution)
define callback routine for returning the final solution values.
Definition conopt.f90:1238
integer(c_int) function coidef_status(cntvect, coi_status)
define callback routine for returning the completion status.
Definition conopt.f90:1212
integer(c_int) function coidef_readmatrix(cntvect, coi_readmatrix)
define callback routine for providing the matrix data to CONOPT.
Definition conopt.f90:1111
integer(c_int) function coidef_errmsg(cntvect, coi_errmsg)
define callback routine for returning error messages for row, column or Jacobian elements.
Definition conopt.f90:1291
integer(c_int) function coidef_fdeval(cntvect, coi_fdeval)
define callback routine for performing function and derivative evaluations.
Definition conopt.f90:1135
integer(c_int) function coidef_optfile(cntvect, optfile)
define callback routine for defining an options file.
Definition conopt.f90:928
integer(c_int) function coidef_license(cntvect, licint1, licint2, licint3, licstring)
define the License Information.
Definition conopt.f90:293
integer(c_int) function coidef_itlim(cntvect, itlim)
define the Iteration Limit.
Definition conopt.f90:323
integer(c_int) function coidef_threads(cntvect, threads)
number of threads allowed internally in CONOPT.
Definition conopt.f90:627
integer(c_int) function coidef_numvar(cntvect, numvar)
defines the number of variables in the model.
Definition conopt.f90:97
integer(c_int) function coidef_numcon(cntvect, numcon)
defines the number of constraints in the model.
Definition conopt.f90:121
integer(c_int) function coidef_numnlnz(cntvect, numnlnz)
defines the Number of Nonlinear Nonzeros.
Definition conopt.f90:167
integer(c_int) function coidef_optdir(cntvect, optdir)
defines the Optimization Direction.
Definition conopt.f90:213
integer(c_int) function coidef_numnz(cntvect, numnz)
defines the number of nonzero elements in the Jacobian.
Definition conopt.f90:144
integer(c_int) function coidef_objcon(cntvect, objcon)
defines the Objective Constraint.
Definition conopt.f90:239
integer(c_int) function coi_create(cntvect)
initializes CONOPT and creates the control vector.
Definition conopt.f90:1726
integer(c_int) function coi_free(cntvect)
frees the control vector.
Definition conopt.f90:1749
integer(c_int) function coi_solve(cntvect)
method for starting the solving process of CONOPT.
Definition conopt.f90:1625
program polygon
Main program. A simple setup and call of CONOPT.
integer, dimension(nd) rowmapj
Definition polygon.f90:68
integer, dimension(np, np) rowmapk
Definition polygon.f90:69
integer, dimension(nd) rowmapi
Definition polygon.f90:68
integer, parameter np
Definition polygon.f90:64
integer, parameter nd
Definition polygon.f90:67
integer, parameter nv
Definition polygon.f90:65
integer, parameter no
Definition polygon.f90:66
subroutine flog(msg, code)
Definition comdeclp.f90:48
integer miter
Definition comdeclp.f90:22
subroutine startup
Definition comdeclp.f90:31
integer function poly_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition polygon.f90:158
integer recursive function poly_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition polygon.f90:338