Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Code in C++, use in Python: tutorial needed [sf#40] #40

Closed
darioizzo opened this issue Feb 24, 2014 · 8 comments
Closed

Code in C++, use in Python: tutorial needed [sf#40] #40

darioizzo opened this issue Feb 24, 2014 · 8 comments
Milestone

Comments

@darioizzo
Copy link
Member

Originally created by darioizzo on 2013-11-25 15:01:51.

Create a tutorial on how to code a pagmo problem in C++ and use it in python. The following files are the bulk of it

@darioizzo
Copy link
Member Author

Originally posted by mmarcusx on 2013-11-25 15:44:27.

I have already written a tutorial on that:

https://sourceforge.net/apps/mediawiki/pagmo/index.php?title=Developing_guide

it could be improved though...

@darioizzo
Copy link
Member Author

Originally posted by darioizzo on 2013-11-25 15:55:19.

The tutorial you refer to is for developers. It shows how a developer can put in PaGMO a new problem / algorithm that is to be shipped in the next release.

Since v 1.1.5 we allow the option to install header files. The tutorial I am asking should show how a normal user (not touching/compiling the pagmo code) can still use libpagmo and PyGMO to code its own problems in c++ and have them visible in Python. You can see the attached files for an example.

@darioizzo
Copy link
Member Author

Originally posted by darioizzo on 2013-11-28 16:51:08.

And here is a new updated version of the scripts that are more robust to porting

@darioizzo
Copy link
Member Author

The files have been moved to github gist (https://gist.github.com/darioizzo/9215291)

@neduard
Copy link

neduard commented Mar 8, 2015

Hello, I've started writing a tutorial at https://github.com/neduard/pagmo/wiki/PaGMO-C---Tutorial

However, I have a couple of questions:

  1. What should be the format? Currently it's written as a github Markdown wiki page in line with the project's other wiki pages. Is this acceptable?
  2. I am unable to load my_pygmo_probs into Python:
    Using the compile command from compile.script, I get:
$ g++ -shared -o my_pygmo_probs.so my_pygmo_probs.o prob_A.o  -std=c++11 -lboost_python-mt -pthread -DNDEBUG -lpython2.7 -lboost_serialization-mt -lpagmo
/usr/lib/gcc/x86_64-pc-linux-gnu/4.8.4/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -lboost_python-mt
collect2: error: ld returned 1 exit status

I can compile successfully using -lboost_python-2.7-mt flag instead.
However, when I go into Python and type import my_pygmo_probs I get:

>>> import my_pygmo_probs
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: extension class wrapper for base class pagmo::problem::base has not been created yet

I found https://mail.python.org/pipermail/cplusplus-sig/2009-February/014244.html but changing the position of the -lboost_python-2.7-mt flag didn't seem to help.

I then fiddled with the code in my_pygmo_probs.cpp:
Changing class_<Problem,bases<problem::base> > to class_<Problem> seems to remove the issue allowing to import the module in Python (and allowing me to execute methods such as a_method()), but I am unsure of what is the impact of removing bases<problem::base>.

Any ideas on what I am missing?
My box setup:

Funtoo Linux (Gentoo Based)
boost package: dev-libs/boost-1.56.0-r1000:0/1.56.0  USE="icu nls python threads -c++11 -context -debug -doc -mpi -static-libs -tools" ABI_X86="(64) -32 (-x32)" PYTHON_ABIS="2.7 3.3 -2.6 -3.1 -3.2 -3.4 (-3.5)
Python version 2.7.9

@darioizzo
Copy link
Member Author

Maybe you need to import PyGMO before importing my_pygmo_probs?

@neduard
Copy link

neduard commented Mar 8, 2015

@darioizzo Thanks! That did the trick!

I finished writing the rest of the tutorial.
Should I leave it at its current address or should I move it somewhere else so that it can be reviewed and commented upon?

@darioizzo
Copy link
Member Author

The tutorial is now available in the wiki at https://github.com/esa/pagmo/wiki/%5BTUTORIAL%5D-Code-a-problem-in-Cpp,-use-it-in-Python

This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants