ImportError: No module named optimization

Asked by ahmad ali

Hallo All
I'm trying to solve numerically an optimization problem with PDE constraints as a part of my thesis, and I found this interesting paper while looking for some packages to solve such problems " A framework for automated PDE-constrained optimisation " by Patrick E. Farrell, Colin J. Cotter and Simon W. Funke.
I tried to implement the first example given in that paper , which is "distributed control for the heat equation" by copying and past the code , but it wasn't working with me, and the same thing for the example mentioned in the documentation here http://dolfin-adjoint.org/documentation/index.html

I'm really newbie in FEniCS and Linux system, so please, bear me.
I ready managed to solve some examples in FEniCS sucessfuly by reading their tutorial, so I don't think the problem is with the FEniCS installation.

I'm using ubuntu 12.04 LTS , and I installed scipy 0.11 ( together with numpy 1.6.1) as it was mentioned in the documentation.
I installed the dolfin-adjoint from the "launchpad PPA".

 Whenever I run the code to solve the minimization problem , I get this message

python /home/ahmad/stationary/optimal_control.py
Traceback (most recent call last):
  File "/home/ahmad/stationary/optimal_control.py", line 3, in <module>
    from dolfin_adjoint import *
  File "/usr/lib/python2.7/dist-packages/dolfin_adjoint/__init__.py", line 37, in <module>
    import optimization
ImportError: No module named optimization

So, please, if you know what might be the mistake, then I really appreciate your answer.

Question information

Language:
English Edit question
Status:
Solved
For:
dolfin-adjoint Edit question
Assignee:
No assignee Edit question
Solved by:
Simon Funke
Solved:
Last query:
Last reply:
Revision history for this message
Best Simon Funke (simon-funke) said :
#1

Hi Ahmad Ali,

Thanks for submitting this bug report. The error you are seeing is caused by a recent internal restructuring of the dolfin-adjoint.optimize code.

I just commited a fix - once the Ubuntu packages are available, you can install the update and the error should disappear.

 Simon

Revision history for this message
ahmad ali (cfcf1111) said :
#2

Thank you so much for your reply !
I should have mentioned that I get this message as well when I run the forward modle ,

"

python /home/ahmad/stationary/optimal_control.py
Traceback (most recent call last):
  File "/home/ahmad/stationary/optimal_control.py", line 9, in <module>
    u = Function(V, name="State")
TypeError: __init__() got an unexpected keyword argument 'name'

"

Revision history for this message
Simon Funke (simon-funke) said :
#3

Hi Ahmed,

Can you make sure that you have imported the dolfin_adjoint module after (!) dolfin, i.e.

from dolfin import *
from dolfin_adjoint import *

If this does not help, could you please give me your code please and I'll have a look.

Simon

Revision history for this message
ahmad ali (cfcf1111) said :
#4

ok, I'm trying to do the example in the paper

as far as I understood, I should excute the forward model once before solving the optimization problem, therefore I use this code

from dolfin import *

# Define the domain [-1, 1] x [-1, 1]
n = 200
mesh = RectangleMesh(-1, -1, 1, 1, n, n)
# Define the function spaces
V = FunctionSpace(mesh, "CG", degree = 1)
W = FunctionSpace(mesh, "DG", degree = 0)
# Define the functions
u = Function(V, name = "Solution")
m = Function(W, name = "Control")
v = TestFunction(V)

# Solve the forward model to create the tape
F = (inner(grad(u), grad(v)) - m*v)*dx
bc = DirichletBC(V, 0.0, "on_boundary")
solve(F == 0, u, bc)

and I get this message

python /home/ahmad/stationary/optimal_control.py
Traceback (most recent call last):
  File "/home/ahmad/stationary/optimal_control.py", line 11, in <module>
    u = Function(V, name = "Solution")
TypeError: __init__() got an unexpected keyword argument 'name'

 I added in the forward model
from dolfin import *
from dolfin_adjoint import *

as you just suggested for me , and I get this message

python /home/ahmad/stationary/optimal_control.py
No Jacobian form specified for nonlinear variational problem.
Differentiating residual form F to obtain Jacobian J = F'.
Solving nonlinear variational problem.
  Newton iteration 1: r (abs) = 0.000e+00 (tol = 1.000e-10) r (rel) = -nan (tol = 1.000e-09)
  Newton solver finished in 1 iterations and 1 linear solver iterations.

it seemed it works , even though I'm not sure
but if I try to solve the optimization problem with this code

from dolfin import *
from dolfin_adjoint import *
# Define the domain [-1, 1] x [-1, 1]
n = 200
mesh = RectangleMesh(-1, -1, 1, 1, n, n)
# Define the function spaces
V = FunctionSpace(mesh, "CG", degree = 1)
W = FunctionSpace(mesh, "DG", degree = 0)
# Define the functions
u = Function(V, name = "Solution")
m = Function(W, name = "Control")
v = TestFunction(V)

# Solve the forward model to create the tape
F = (inner(grad(u), grad(v)) - m*v)*dx
bc = DirichletBC(V, 0.0, "on_boundary")
solve(F == 0, u, bc)

# Define the functional of interest
u_d = exp(-1/(1-x[0]*x[0])-1/(1-x[1]*x[1]))
J = Functional((0.5*inner(u-u_d, u-u_d))*dx)

# Define the reduced functional
J_hat = ReducedFunctional(J, SteadyParameter(m))
# Solve the optimisation problem
m_opt = minimize(J_hat, method = "L-BFGS-B", bounds = (0.0, 0.5),
options = {"gtol": 1e-16, "ftol": 1e-16})

I get this message

python /home/ahmad/stationary/optimal_control.py
No Jacobian form specified for nonlinear variational problem.
Differentiating residual form F to obtain Jacobian J = F'.
Solving nonlinear variational problem.
  Newton iteration 1: r (abs) = 0.000e+00 (tol = 1.000e-10) r (rel) = -nan (tol = 1.000e-09)
  Newton solver finished in 1 iterations and 1 linear solver iterations.
Traceback (most recent call last):
  File "/home/ahmad/stationary/optimal_control.py", line 22, in <module>
    u_d = exp(-1/(1-x[0]*x[0])-1/(1-x[1]*x[1]))
NameError: name 'x' is not defined

Revision history for this message
Simon Funke (simon-funke) said :
#5

Hi Ahmad,

what you have done so far looks good. To get rid of the new error message you need to add the line
x = triangle.x
before you use x. This should do the trick.

If it still does not work, try using this program as a template:
http://dolfin-adjoint.org/_static/optimal_control.py

And compare the differences from your program.

Simon

Revision history for this message
ahmad ali (cfcf1111) said :
#6

Thanks Simon Funke, that solved my question.

Revision history for this message
ahmad ali (cfcf1111) said :
#7

Thank you again for your help and explanation !
just I have one enquiry about the second example in the paper, which is a nonlinear equation. 'I'm just interested in the nonlinearity"
In the paper it was metioned the " Newton solver for the nonlinear equation" and it was used
solve(F == 0, u, bc) in the code.

but according to Fenics tutorial , this is the same syntax to solve linear problems, while for nonlinear pdes one should use

problem = NonlinearVariationalProblem(F, u, bcs, J)
solver = NonlinearVariationalSolver(problem)
solver.solve()

which requires the Jacobian.

So, do you have any comments on this ?

again, thank you for the help, and I'm sorry for taking from your time.

Revision history for this message
Simon Funke (simon-funke) said :
#8

Hi Ahmad,

the

solve(F == 0, u, bc )

indeed solves nonlinear problems with the Newton solver (and in fact is a shortcut for creating NonlinearVariationalSolver the way you described it). Of course the same syntax can be used to solve linear equations (in which case the Newton solver will converge after 1 iteration).

If you want to explicitly use a linear solver, you can use instead:

solve(lhs(F) == rhs(F), u, bc)

(see for example the Poisson demo in the FEniCS tutorial: http://fenicsproject.org/documentation/tutorial/fundamentals.html)

hope this helps,

Simon