Giter Site home page Giter Site logo

cfl-minds / drl_shape_optimization Goto Github PK

View Code? Open in Web Editor NEW
53.0 6.0 14.0 1.01 MB

Deep reinforcement learning to perform shape optimization

License: MIT License

Python 99.79% Starlark 0.07% Makefile 0.06% Batchfile 0.08%
deep-reinforcement-learning ppo shape-optimization drl

drl_shape_optimization's Introduction

drl_shape_optimization

The code in this repository presents a case of shape optimization for aerodynamics using DRL (Deep Reinforcement Learning). It is based on Tensorforce for the DRL components, and on Fenics for the CFD computations. The corresponding paper is here and here. If you use this code for your research, please consider citing this paper.

Update: The optimization method has been greatly improved, you can find more here.

What is in the repo

  • The parameterization of the DRL problem can be done in parametered_env.py.

  • If you wish to modify the reward computation, you should check the function compute_reward in environment.py.

  • The reset folder contains the initial shape that is loaded as initial state at the beginning of each episode. As of now, only the 4-points initial shape is present. You can generate more initial cylinders with different point numbers using the generate_shape.py file.

  • The parallel learning generates copies of the environment in separate folders. To concatenate all the results in a linear history, you can run sort_envs.py periodically. All the results will be summed up in a sorted_envs folder. You should be aware that the "sorted numbering" of a shape can change from one sorting run to another.

  • The CFD solver is contained in the fenics_solver.py file.

Installation and requirements

Method 1 (discouraged): installing by hand

  • This project relies on several external libraries. You will need to install Fenics (https://fenicsproject.org/download/) and gmsh, in the right versions (Gmsh 3.0.6, dolfin/fenics 2018.1.0), on your platform. This may be tricky to get given the complex dependency graphs of both these packages.

In addition, you will need the following packages that are provided / defined in the repo (you may need to run these with sudo rights):

  • Install the tensorforce version present in the repo:

cd tensorforce; pip3 install -e .; cd ..

  • Then install the required python modules using pip3:

pip3 install -r requirements.txt

Method 2 (recommended): using the docker container provided

Get the docker container at: https://cloud.mines-paristech.fr/index.php/s/kv7ymdrITXbeBNa

Load the docker container, and start an interactive session sharing your cwd to allow exchange of information in and out of the container:

sudo docker load < shape_2d_2020_12.tar  # load the image into docker
sudo docker run -ti --name shape2dopt -v $(pwd):/home/fenics/shared shape_2d_2020_12:latest  # spin an interactive container (named shape2dopt) out of the image, sharing the cwd for allowing exchange of information
  at this stage you get a prompt inside the container; exit the container immediately
sudo docker start shape2dopt  # container was stopped when exiting, start it again
sudo docker exec -ti -u fenics shape2dopt /bin/bash -l  # get an interactive terminal inside the re-started container
  you are now in an interactive session inside the container, where the /shared folder is a mirror of your cwd, and /local contains the code, with all packages and dependencies availalbe

In addition, once you are finished working, you can stop the whole container by using sudo docker container stop shape2dopt

The code contained in this repo is included already in the container, at: /home/fenics/local/shape_optimization_DRL_prep. The source code for performing learning is available at: /home/fenics/local/shape_optimization_DRL_prep/src. All necessary dependencies and packages in the correct version are already included, no more setup is needed.

For a more in-depth introduction to docker commands, and more explanations about how this container was built, see the discussions written for a similar project available at: https://github.com/jerabaul29/Cylinder2DFlowControlDRLParallel/blob/master/Docker/README_container.md .

How to start learning

It is recommended to use multiple terminals, or a multiplexer like tmux, in order to manage the simulation servers vs the training client.

  • First, start the servers (for example in a tmux pane) using the following command:

python3 launch_servers.py -n 4 -p 1111

The -n argument corresponds to the number of available cores on your machine, while -p must be an available port (any four-digit number should be ok, feel free to change it if 1111 is taken on your machine).

  • Once all servers are ready (confirmed by the mention all processes started, ready to serve...), start the training (for example in a new tmux pane):

python3 launch_parallel_training.py -p 1111 -n 4

Note that the number of servers and the first port value must match that used for starting the simulation servers.

  • At this stage, the DRL shape optimization is now working.

drl_shape_optimization's People

Contributors

jerabaul29 avatar jviquerat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

drl_shape_optimization's Issues

Why is terminal = False?

Hi, great idea to use PPO to design stuff!

I'm a Master's degree student at Eafit University (Colombia). [email protected]

I got interesting in your research and I want to try to use the one step PPO to design fourbar mechanisms for my master's thesis (giving the proper citations and credit to your repo and paper)

I was trying to adapt a version changing the scripts environment.py and launch_parallel_training.py but it's not giving the results that I expected.

I would really like to get it working but I have some doubts: the first one is why did you return 'terminal=False' even though it's a single step PPO? Could you please help me to clarify this? I also saw that in the paper you mentioned that the advantage estimation was only the current reward because the other two prediction terms cancel each other
Aπ(st, at) = reward_t + γV (st+1) − V (st),. But I wasn't able to tell how you got rid of the critic in the agent code.

https://github.com/jviquerat/drl_shape_optimization/blob/master/src/environment.py#L227

Apart from that I offer an invitation to work together if you are interested in testing you work on another domain.

Thank you and sorry for the long message but I got really interested in the work that you did!

Warning msg and new ver

Hi,

I just tried it. There are some warning msg below. Are they normal:

****** Starting episode 0
***    Starting control 0
WARNING:root:Dolfin's XML is a legacy format. Consider using XDMF instead.
-2.465207003554415 -0.5213985274605915
good epoch; reward: 0.2115029393916301
****** Starting episode 1
***    Starting control 0
WARNING:root:Dolfin's XML is a legacy format. Consider using XDMF instead.
Building point search tree to accelerate distance queries.

WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.

Also, has the new PBO been added into the current code? If not, will it be easier to update? Is there any guide on how it can be done?

Thanks!

Using GPU in WSL with drl_shape_optimization

Hi, I've got the docker file working. However, when I did a check, it says GPU not available.

So does this code uses GPU for its deep learning?

Or is it that GPU is not available when I'm using it in WSL with docker?

Thanks.

PS: I have other deep learning docker codes which work in WSL with GPU capability.

Code not compiling

After following all the steps as specified in the readme, I am getting the following error(in the execution of - " python3 launch_parallel_training.py -p 1111 -n 4") -

WARNING:tensorflow:From /home/aaryansh/.local/lib/python3.8/site-packages/tensorflow/python/ops/resource_variable_ops.py:1659: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
2022-01-21 15:26:15.508561: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /opt/ros/noetic/lib:/opt/ros/noetic/lib/x86_64-linux-gnu:/home/aaryansh/.mujoco/mujoco210/bin
2022-01-21 15:26:15.508595: E tensorflow/stream_executor/cuda/cuda_driver.cc:313] failed call to cuInit: UNKNOWN ERROR (303)
2022-01-21 15:26:15.508619: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (aaryansh-ThinkPad-E14): /proc/driver/nvidia/version does not exist
Traceback (most recent call last):
  File "launch_parallel_training.py", line 91, in <module>
    runner = ParallelRunner(agent=agent, environments=remote_envs)
  File "/home/aaryansh/Documents/RL_fluids_papers/drl_shape_optim/drl_shape_optimization/src/tensorforce/tensorforce/execution/parallel_runner.py", line 48, in __init__
    self.agent.initialize()
  File "/home/aaryansh/Documents/RL_fluids_papers/drl_shape_optim/drl_shape_optimization/src/tensorforce/tensorforce/agents/agent.py", line 151, in initialize
    self.model.setup()  # should be self.model.initialize()
  File "/home/aaryansh/Documents/RL_fluids_papers/drl_shape_optim/drl_shape_optimization/src/tensorforce/tensorforce/core/models/model.py", line 354, in setup
    self.initialize()
  File "/home/aaryansh/Documents/RL_fluids_papers/drl_shape_optim/drl_shape_optimization/src/tensorforce/tensorforce/core/module.py", line 320, in initialize
    function = self.create_api_function(
  File "/home/aaryansh/Documents/RL_fluids_papers/drl_shape_optim/drl_shape_optimization/src/tensorforce/tensorforce/core/module.py", line 358, in create_api_function
    results = api_function()
  File "/home/aaryansh/Documents/RL_fluids_papers/drl_shape_optim/drl_shape_optimization/src/tensorforce/tensorforce/core/models/model.py", line 832, in api_act
    with tf.control_dependencies(control_inputs=assertions):
  File "/home/aaryansh/.local/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 5173, in control_dependencies
    return get_default_graph().control_dependencies(control_inputs)
  File "/home/aaryansh/.local/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 4591, in control_dependencies
    c = self.as_graph_element(c)
  File "/home/aaryansh/.local/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 3512, in as_graph_element
    return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
  File "/home/aaryansh/.local/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 3600, in _as_graph_element_locked
    raise TypeError("Can not convert a %s into a %s." %
TypeError: Can not convert a NoneType into a Tensor or Operation.
ERROR:tensorflow:==================================
Object was never used (type <class 'tensorflow.python.framework.ops.Operation'>):
<tf.Operation 'ppo.act/assert_less/Assert/AssertGuard/Identity' type=Identity>
If you want to mark it as used call its "mark_used()" method.
It was originally created here:
  File "/home/aaryansh/Documents/RL_fluids_papers/drl_shape_optim/drl_shape_optimization/src/tensorforce/tensorforce/core/models/model.py", line 832, in api_act
    with tf.control_dependencies(control_inputs=assertions):  File "/home/aaryansh/.local/lib/python3.8/site-packages/tensorflow/python/ops/check_ops.py", line 873, in assert_less_v2
    return assert_less(x=x, y=y, summarize=summarize, message=message, name=name)  File "/home/aaryansh/.local/lib/python3.8/site-packages/tensorflow/python/ops/check_ops.py", line 879, in assert_less
    return _binary_assert('<', 'assert_less', math_ops.less, np.less, x, y, data,  File "/home/aaryansh/.local/lib/python3.8/site-packages/tensorflow/python/ops/check_ops.py", line 372, in _binary_assert
    return control_flow_ops.Assert(condition, data, summarize=summarize)  File "/home/aaryansh/.local/lib/python3.8/site-packages/tensorflow/python/util/tf_should_use.py", line 235, in wrapped
    return _add_should_use_warning(fn(*args, **kwargs),
==================================
ERROR:tensorflow:==================================
Object was never used (type <class 'tensorflow.python.framework.ops.Operation'>):
<tf.Operation 'ppo.act/assert_non_negative/assert_less_equal/Assert/AssertGuard/Identity' type=Identity>
If you want to mark it as used call its "mark_used()" method.
It was originally created here:
  File "/home/aaryansh/.local/lib/python3.8/site-packages/tensorflow/python/ops/check_ops.py", line 539, in assert_non_negative_v2
    return assert_non_negative(x=x, summarize=summarize, message=message,  File "/home/aaryansh/.local/lib/python3.8/site-packages/tensorflow/python/ops/check_ops.py", line 560, in assert_non_negative
    return assert_less_equal(zero, x, data=data, summarize=summarize)  File "/home/aaryansh/.local/lib/python3.8/site-packages/tensorflow/python/ops/check_ops.py", line 923, in assert_less_equal
    return _binary_assert('<=', 'assert_less_equal', math_ops.less_equal,  File "/home/aaryansh/.local/lib/python3.8/site-packages/tensorflow/python/ops/check_ops.py", line 372, in _binary_assert
    return control_flow_ops.Assert(condition, data, summarize=summarize)  File "/home/aaryansh/.local/lib/python3.8/site-packages/tensorflow/python/util/tf_should_use.py", line 235, in wrapped
    return _add_should_use_warning(fn(*args, **kwargs),
==================================

Please provide possible solutions or let me know if there is a compatibility/version issue with the code.

Negative Drag

Hi
The numerical values of drag being computed over shapes are coming out to be negative(which cannot be possible). Although the mathematical equations seem to be correct in the code. Also in the "compute_reward" function, it has been commented that drag is always less than zero. I am not able to figure out why this is happening.

Can you please enlighten me on this?
Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.