Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

nlopt.f

Status
Not open for further replies.

shaberg

Programmer
Dec 1, 2015
4
0
0
CA
Hi Everyone!

I'm using Fortran 90 and trying to use the library nlopt.f in my code.
I downloaded the library from NLopt on Windows from
After downloading the zip code from there I went to the DOS command option and run the line: lib /def:libnlopt-0.def (as suggested by wikipedia). However, I am still not able to use it in my code.

I am using compaq Visual Fortran to code.

I am not sure how to install the library on my computer so that when I write in my code include nlopt.f, I will be able to use the functions that the library has. Can anyone help with this problem?

Thanks in advance,
Shahar
 
I have not Visual Fortran compiler, but with the gfortran compiler it works for me as following:
1. I downloaded the package nlopt-2.4.2-dll32.zip from here

2. I extracted the package and copied from it these 2 files into my working directory:
libnlopt-0.dll
nlopt.f

3. For trying if it works I took the code from here
and created nlopt_example.f95

Now I have in my working dorectory these 3 files
Code:
libnlopt-0.dll
nlopt.f
nlopt_example.f95

4. I compiled the example using this command line
Code:
$ gfortran nlopt_example.f95 -o nlopt_example -L. libnlopt-0.dll
which created the nlopt_example.exe

5. when I run the exe I get the result
Code:
$ nlopt_example
 found min at   0.33333333454526648       0.29629628990899065
 min val =   0.54433104808470245
 
Here is the source I used:

nlopt_example.f95
Code:
[COLOR=#0000ff]! Compile:[/color]
[COLOR=#0000ff]! gfortran nlopt_example.f95 -o nlopt_example -L. libnlopt-0.dll[/color]
 
[COLOR=#a020f0]program[/color] main
  [COLOR=#2e8b57][b]external[/b][/color] myfunc, myconstraint
  [COLOR=#2e8b57][b]double precision[/b][/color] lb([COLOR=#ff00ff]2[/color])
  [COLOR=#2e8b57][b]integer[/b][/color][COLOR=#804040][b]*[/b][/color][COLOR=#ff00ff]8[/color] opt
  [COLOR=#2e8b57][b]double precision[/b][/color] d1([COLOR=#ff00ff]2[/color]), d2([COLOR=#ff00ff]2[/color])
  [COLOR=#2e8b57][b]double precision[/b][/color] x([COLOR=#ff00ff]2[/color]), minf
  [COLOR=#2e8b57][b]integer[/b][/color] ires
  [COLOR=#a020f0]include[/color] [COLOR=#ff00ff]'nlopt.f'[/color]
 
  [COLOR=#008080]call[/color] nlo_create(opt, NLOPT_LD_MMA, [COLOR=#ff00ff]2[/color])
  [COLOR=#008080]call[/color] nlo_get_lower_bounds(ires, opt, lb)
  lb([COLOR=#ff00ff]2[/color]) [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0.0[/color]
  [COLOR=#008080]call[/color] nlo_set_lower_bounds(ires, opt, lb)
  [COLOR=#008080]call[/color] nlo_set_min_objective(ires, opt, myfunc, [COLOR=#ff00ff]0[/color])
 
  d1([COLOR=#ff00ff]1[/color]) [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]2[/color].
  d1([COLOR=#ff00ff]2[/color]) [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color].
  [COLOR=#008080]call[/color] nlo_add_inequality_constraint(ires, opt, [COLOR=#804040][b]&[/b][/color]
       myconstraint, d1, [COLOR=#ff00ff]1.D-8[/color])
  d2([COLOR=#ff00ff]1[/color]) [COLOR=#804040][b]=[/b][/color] [COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color].
  d2([COLOR=#ff00ff]2[/color]) [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color].
  [COLOR=#008080]call[/color] nlo_add_inequality_constraint(ires, opt, [COLOR=#804040][b]&[/b][/color]
       myconstraint, d2, [COLOR=#ff00ff]1.D-8[/color])
 
  [COLOR=#008080]call[/color] nlo_set_xtol_rel(ires, opt, [COLOR=#ff00ff]1.D-4[/color])
 
  x([COLOR=#ff00ff]1[/color]) [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1.234[/color]
  x([COLOR=#ff00ff]2[/color]) [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]5.678[/color]
  [COLOR=#008080]call[/color] nlo_optimize(ires, opt, x, minf)
  [COLOR=#804040][b]if[/b][/color] (ires[COLOR=#804040][b].lt.[/b][/color][COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
     [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'nlopt failed!'[/color]
  [COLOR=#804040][b]else[/b][/color]
     [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'found min at '[/color], x([COLOR=#ff00ff]1[/color]), x([COLOR=#ff00ff]2[/color])
     [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'min val = '[/color], minf
  [COLOR=#804040][b]endif[/b][/color]

  [COLOR=#008080]call[/color] nlo_destroy(opt)
[COLOR=#a020f0]end[/color]

[COLOR=#a020f0]subroutine[/color] myfunc(val, n, x, grad, need_gradient, f_data)
  [COLOR=#2e8b57][b]double precision[/b][/color] val, x(n), grad(n)
  [COLOR=#2e8b57][b]integer[/b][/color] n, need_gradient
  [COLOR=#804040][b]if[/b][/color] (need_gradient[COLOR=#804040][b].ne.[/b][/color][COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
     grad([COLOR=#ff00ff]1[/color]) [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0.0[/color]
     grad([COLOR=#ff00ff]2[/color]) [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0.5[/color] [COLOR=#804040][b]/[/b][/color] [COLOR=#008080]dsqrt[/color](x([COLOR=#ff00ff]2[/color]))
  [COLOR=#804040][b]endif[/b][/color]
  val [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]dsqrt[/color](x([COLOR=#ff00ff]2[/color]))
[COLOR=#a020f0]end[/color]

[COLOR=#a020f0]subroutine[/color] myconstraint(val, n, x, grad, need_gradient, d)
  [COLOR=#2e8b57][b]integer[/b][/color] need_gradient
  [COLOR=#2e8b57][b]double precision[/b][/color] val, x(n), grad(n), d([COLOR=#ff00ff]2[/color]), a, b
  a [COLOR=#804040][b]=[/b][/color] d([COLOR=#ff00ff]1[/color])
  b [COLOR=#804040][b]=[/b][/color] d([COLOR=#ff00ff]2[/color])
  [COLOR=#804040][b]if[/b][/color] (need_gradient[COLOR=#804040][b].ne.[/b][/color][COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
     grad([COLOR=#ff00ff]1[/color]) [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]3[/color]. [COLOR=#804040][b]*[/b][/color] a [COLOR=#804040][b]*[/b][/color] (a[COLOR=#804040][b]*[/b][/color]x([COLOR=#ff00ff]1[/color]) [COLOR=#804040][b]+[/b][/color] b)[COLOR=#804040][b]**[/b][/color][COLOR=#ff00ff]2[/color]
     grad([COLOR=#ff00ff]2[/color]) [COLOR=#804040][b]=[/b][/color] [COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1.0[/color]
  [COLOR=#804040][b]endif[/b][/color]
  val [COLOR=#804040][b]=[/b][/color] (a[COLOR=#804040][b]*[/b][/color]x([COLOR=#ff00ff]1[/color]) [COLOR=#804040][b]+[/b][/color] b)[COLOR=#804040][b]**[/b][/color][COLOR=#ff00ff]3[/color] [COLOR=#804040][b]-[/b][/color] x([COLOR=#ff00ff]2[/color])
[COLOR=#a020f0]end[/color]
 
Thank you for your help!

I did everything you are saying except I don't understand what it means in step 2 to add the files into the directory. Where do you add them exactly? How do you do that?

Also what do you mean by compiling here? In your code the compile line has a ! so it is actually just a remark....So I am not sure what you mean by that either. Can you expand on that?

Thanks so much in advance,
Shahar

 
shaberg said:
I don't understand what it means in step 2 to add the files into the directory
I copied the 2 files (libnlopt-0.dll and nlopt.f) into my working directory, which is
Code:
C:\MinGW\msys\1.0\home\Roman\fortran\nlopt
It's so long because I'm using MingW + MSYS (see
Also what do you mean by compiling here? In your code the compile line has a ! so it is actually just a remark....
Yes, I remarked in the fortran code too, how to compile it.
First, I changed to my working directory (i.e.: C:\MinGW\msys\1.0\home\Roman\fortran\nlopt) and then I compiled it executing the command:
Code:
gfortran nlopt_example.f95 -o nlopt_example -L. libnlopt-0.dll
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top