From: Ovidiu Mircescu Date: Tue, 15 Jul 2014 15:15:49 +0000 (+0200) Subject: New complete example of OptimizerLoop plugin. X-Git-Tag: V7_5_0a1~7^2~3 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=10b0febbd5ad7a8706f8a2c38d9d775f7e24efca;p=modules%2Fyacs.git New complete example of OptimizerLoop plugin. The new test examples use every function of the plugin interface. Also, the sphinx doc includes the test files directly. --- diff --git a/doc/optimizer.rst b/doc/optimizer.rst index 9fda36957..5ea0cfd01 100644 --- a/doc/optimizer.rst +++ b/doc/optimizer.rst @@ -32,191 +32,17 @@ C++ plugin example '''''''''''''''''''' Here is a small example of a C++ synchronous algorithm: -.. code-block:: cpp - - #include - - #include "OptimizerAlg.hxx" - - using namespace YACS::ENGINE; - - extern "C" - { - OptimizerAlgBase * createOptimizerAlgSyncExample(Pool * pool); - } - - class OptimizerAlgSyncExample : public OptimizerAlgSync - { - private: - int _idTest; - TypeCode *_tcIn; - TypeCode *_tcOut; - public: - OptimizerAlgSyncExample(Pool *pool); - virtual ~OptimizerAlgSyncExample(); - TypeCode *getTCForIn() const; - TypeCode *getTCForOut() const; - void start(); - void takeDecision(); - void initialize(const Any *input) throw(YACS::Exception); - void finish(); - }; - - OptimizerAlgSyncExample::OptimizerAlgSyncExample(Pool *pool) - : OptimizerAlgSync(pool), _tcIn(0), _tcOut(0), _idTest(0) - { - _tcIn=new TypeCode(Double); - _tcOut=new TypeCode(Int); - } - - OptimizerAlgSyncExample::~OptimizerAlgSyncExample() - { - _tcIn->decrRef(); - _tcOut->decrRef(); - } - - //! Return the typecode of the expected input type - TypeCode * OptimizerAlgSyncExample::getTCForIn() const - { - return _tcIn; - } - - //! Return the typecode of the expected output type - TypeCode * OptimizerAlgSyncExample::getTCForOut() const - { - return _tcOut; - } - - //! Start to fill the pool with samples to evaluate - void OptimizerAlgSyncExample::start() - { - _idTest=0; - Any *val=AtomAny::New(1.2); - _pool->pushInSample(4,val); - val=AtomAny::New(3.4); - _pool->pushInSample(9,val); - } - - //! This method is called each time a sample has been evaluated. - /*! - * It can either add new samples to evaluate in the pool, do nothing (wait - * for more samples), or empty the pool to finish the evaluation. - */ - void OptimizerAlgSyncExample::takeDecision() - { - if(_idTest==1) - { - Any *val=AtomAny::New(5.6); - _pool->pushInSample(16,val); - val=AtomAny::New(7.8); - _pool->pushInSample(25,val); - val=AtomAny::New(9. ); - _pool->pushInSample(36,val); - val=AtomAny::New(12.3); - _pool->pushInSample(49,val); - } - else if(_idTest==4) - { - Any *val=AtomAny::New(45.6); - _pool->pushInSample(64,val); - val=AtomAny::New(78.9); - _pool->pushInSample(81,val); - } - else - { - Any *tmp= _pool->getCurrentInSample(); - if(fabs(tmp->getDoubleValue()-45.6)<1.e-12) - _pool->destroyAll(); - } - _idTest++; - } - - //! Optional method to initialize the algorithm. - /*! - * For now, the parameter input is always NULL. It might be used in the - * future to initialize an algorithm with custom data. - */ - void OptimizerAlgSyncExample::initialize(const Any *input) - throw (YACS::Exception) - { - } - - /*! - * Optional method called when the algorithm has finished, successfully or - * not, to perform any necessary clean up. - */ - void OptimizerAlgSyncExample::finish() - { - } - - //! Factory method to create the algorithm. - OptimizerAlgBase * createOptimizerAlgSyncExample(Pool *pool) - { - return new OptimizerAlgSyncExample(pool); - } - +.. literalinclude:: ../src/yacsloader/Test/OptimizerAlgSyncExample.cxx + :language: cpp Here, the entry point in the dynamic library is the name of the factory function : createOptimizerAlgSyncExample that returns an instance of the OptimizerAlgSyncExample class that implements the algorithm. Python plugin example '''''''''''''''''''''' -Here, the same example of a synchronous algorithm in Python:: +Here, the same example of a synchronous algorithm in Python: - import SALOMERuntime - - class myalgosync(SALOMERuntime.OptimizerAlgSync): - def __init__(self): - SALOMERuntime.OptimizerAlgSync.__init__(self, None) - r=SALOMERuntime.getSALOMERuntime() - self.tin=r.getTypeCode("double") - self.tout=r.getTypeCode("int") - - def setPool(self,pool): - """Must be implemented to set the pool""" - self.pool=pool - - def getTCForIn(self): - """returns typecode of type expected as Input""" - return self.tin - - def getTCForOut(self): - """returns typecode of type expected as Output""" - return self.tout - - def initialize(self,input): - """Optional method called on initialization. Do nothing here""" - - def start(self): - """Start to fill the pool with samples to evaluate.""" - self.iter=0 - self.pool.pushInSample(4,1.2) - self.pool.pushInSample(9,3.4) - - def takeDecision(self): - """ This method is called each time a sample has been evaluated. It can - either add new samples to evaluate in the pool, do nothing (wait for - more samples), or empty the pool to finish the evaluation. - """ - currentId=self.pool.getCurrentId() - - if self.iter==1: - self.pool.pushInSample(16,5.6) - self.pool.pushInSample(25,7.8) - self.pool.pushInSample(36,9.) - self.pool.pushInSample(49,12.3) - elif self.iter==4: - self.pool.pushInSample(64,45.6) - self.pool.pushInSample(81,78.9) - else: - val=self.pool.getCurrentInSample() - if abs(val.getDoubleValue()-45.6) < 1.e-12: - self.pool.destroyAll() - self.iter=self.iter+1 - - def finish(self): - """Optional method called when the algorithm has finished, successfully - or not, to perform any necessary clean up. Do nothing here""" +.. literalinclude:: ../src/yacsloader/Test/algosyncexample.py Here, the entry point in the Python module is directly the name of the class that implements the algorithm : myalgosync. @@ -243,94 +69,8 @@ C++ plugin example '''''''''''''''''''' Here is a small example of a C++ asynchronous algorithm: -.. code-block:: cpp - - #include "OptimizerAlg.hxx" - - using namespace YACS::ENGINE; - - extern "C" - { - OptimizerAlgBase * createOptimizerAlgASyncExample(Pool * pool); - } - - class OptimizerAlgASyncExample : public OptimizerAlgASync - { - private: - TypeCode * _tcIn; - TypeCode * _tcOut; - public: - OptimizerAlgASyncExample(Pool * pool); - virtual ~OptimizerAlgASyncExample(); - TypeCode * getTCForIn() const; - TypeCode * getTCForOut() const; - void startToTakeDecision(); - }; - - OptimizerAlgASyncExample::OptimizerAlgASyncExample(Pool * pool) - : OptimizerAlgASync(pool), _tcIn(0), _tcOut(0) - { - _tcIn = new TypeCode(Double); - _tcOut = new TypeCode(Int); - } - - OptimizerAlgASyncExample::~OptimizerAlgASyncExample() - { - _tcIn->decrRef(); - _tcOut->decrRef(); - } - - //! Return the typecode of the expected input type - TypeCode *OptimizerAlgASyncExample::getTCForIn() const - { - return _tcIn; - } - - //! Return the typecode of the expected output type - TypeCode *OptimizerAlgASyncExample::getTCForOut() const - { - return _tcOut; - } - - //! This method is called only once to launch the algorithm. - /*! - * It must first fill the pool with samples to evaluate and call - * signalMasterAndWait() to block until a sample has been evaluated. When - * returning from this method, it MUST check for an eventual termination - * request (with the method isTerminationRequested()). If the termination - * is requested, the method must perform any necessary cleanup and return - * as soon as possible. Otherwise it can either add new samples to evaluate - * in the pool, do nothing (wait for more samples), or empty the pool and - * return to finish the evaluation. - */ - void OptimizerAlgASyncExample::startToTakeDecision() - { - double val = 1.2; - for (int i=0 ; i<5 ; i++) { - // push a sample in the input of the slave node - _pool->pushInSample(i, AtomAny::New(val)); - // wait until next sample is ready - signalMasterAndWait(); - // check error notification - if (isTerminationRequested()) { - _pool->destroyAll(); - return; - } - - // get a sample from the output of the slave node - Any * v = _pool->getCurrentOutSample(); - val += v->getIntValue(); - } - - // in the end destroy the pool content - _pool->destroyAll(); - } - - //! Factory method to create the algorithm. - OptimizerAlgBase * createOptimizerAlgASyncExample(Pool * pool) - { - return new OptimizerAlgASyncExample(pool); - } +.. literalinclude:: ../src/yacsloader/Test/OptimizerAlgASyncExample.cxx + :language: cpp Here, the entry point in the dynamic library is the name of the factory function : createOptimizerAlgASyncExample @@ -338,59 +78,9 @@ that returns an instance of the OptimizerAlgASyncExample class that implements t Python plugin example '''''''''''''''''''''''' -Here is an example of an asynchronous algorithm implemented in Python:: +Here is an example of an asynchronous algorithm implemented in Python: - import SALOMERuntime - - class myalgoasync(SALOMERuntime.OptimizerAlgASync): - def __init__(self): - SALOMERuntime.OptimizerAlgASync.__init__(self, None) - r=SALOMERuntime.getSALOMERuntime() - self.tin=r.getTypeCode("double") - self.tout=r.getTypeCode("int") - - def setPool(self,pool): - """Must be implemented to set the pool""" - self.pool=pool - - def getTCForIn(self): - """returns typecode of type expected as Input""" - return self.tin - - def getTCForOut(self): - """returns typecode of type expected as Output""" - return self.tout - - def startToTakeDecision(self): - """This method is called only once to launch the algorithm. It must - first fill the pool with samples to evaluate and call - self.signalMasterAndWait() to block until a sample has been - evaluated. When returning from this method, it MUST check for an - eventual termination request (with the method - self.isTerminationRequested()). If the termination is requested, the - method must perform any necessary cleanup and return as soon as - possible. Otherwise it can either add new samples to evaluate in the - pool, do nothing (wait for more samples), or empty the pool and - return to finish the evaluation. - """ - val=1.2 - for iter in xrange(5): - #push a sample in the input of the slave node - self.pool.pushInSample(iter,val) - #wait until next sample is ready - self.signalMasterAndWait() - #check error notification - if self.isTerminationRequested(): - self.pool.destroyAll() - return - - #get a sample from the output of the slave node - currentId=self.pool.getCurrentId() - v=self.pool.getCurrentOutSample() - val=val+v.getIntValue() - - #in the end destroy the pool content - self.pool.destroyAll() +.. literalinclude:: ../src/yacsloader/Test/algoasyncexample.py Here, the entry point in the Python module is directly the name of the class that implements the algorithm : myalgoasync. diff --git a/src/yacsloader/Test/OptimizerAlgASyncExample.cxx b/src/yacsloader/Test/OptimizerAlgASyncExample.cxx index 86bc84ecb..34d4a0d4e 100644 --- a/src/yacsloader/Test/OptimizerAlgASyncExample.cxx +++ b/src/yacsloader/Test/OptimizerAlgASyncExample.cxx @@ -19,86 +19,145 @@ #include "OptimizerAlg.hxx" -using namespace YACS::ENGINE; +#include +//using namespace YACS::ENGINE; extern "C" { - OptimizerAlgBase * createOptimizerAlgASyncExample(Pool * pool); + YACS::ENGINE::OptimizerAlgBase * createOptimizerAlgASyncExample(YACS::ENGINE::Pool * pool); } -class OptimizerAlgASyncExample : public OptimizerAlgASync - { +class OptimizerAlgASyncExample : public YACS::ENGINE::OptimizerAlgASync +{ private: - TypeCode * _tcIn; - TypeCode * _tcOut; + YACS::ENGINE::TypeCode *_tcInt; + YACS::ENGINE::TypeCode *_tcDouble; public: - OptimizerAlgASyncExample(Pool * pool); + OptimizerAlgASyncExample(YACS::ENGINE::Pool *pool); virtual ~OptimizerAlgASyncExample(); - TypeCode * getTCForIn() const; - TypeCode * getTCForOut() const; - void startToTakeDecision(); - }; + + //! returns typecode of type expected as Input. OwnerShip of returned pointer is held by this. + virtual YACS::ENGINE::TypeCode *getTCForIn() const; + //! returns typecode of type expected as Output. OwnerShip of returned pointer is held by this. + virtual YACS::ENGINE::TypeCode *getTCForOut() const; + //! returns typecode of type expected for algo initialization. OwnerShip of returned pointer is held by this. + virtual YACS::ENGINE::TypeCode *getTCForAlgoInit() const; + //! returns typecode of type expected as algo result. OwnerShip of returned pointer is held by this. + virtual YACS::ENGINE::TypeCode *getTCForAlgoResult() const; + virtual void initialize(const YACS::ENGINE::Any *input) throw (YACS::Exception); + virtual void startToTakeDecision(); + virtual void finish();//! Called when optimization has succeed. + virtual YACS::ENGINE::Any * getAlgoResult(); +}; -OptimizerAlgASyncExample::OptimizerAlgASyncExample(Pool * pool) : OptimizerAlgASync(pool), - _tcIn(0), _tcOut(0) +OptimizerAlgASyncExample::OptimizerAlgASyncExample(YACS::ENGINE::Pool *pool) + : YACS::ENGINE::OptimizerAlgASync(pool), _tcInt(0), _tcDouble(0) { - _tcIn = new TypeCode(Double); - _tcOut = new TypeCode(Int); + _tcDouble = new YACS::ENGINE::TypeCode(YACS::ENGINE::Double); + _tcInt = new YACS::ENGINE::TypeCode(YACS::ENGINE::Int); } OptimizerAlgASyncExample::~OptimizerAlgASyncExample() { - _tcIn->decrRef(); - _tcOut->decrRef(); + _tcDouble->decrRef(); + _tcInt->decrRef(); +} + +//! Return the typecode of the expected input of the internal node +YACS::ENGINE::TypeCode * OptimizerAlgASyncExample::getTCForIn() const +{ + return _tcDouble; +} + +//! Return the typecode of the expected output of the internal node +YACS::ENGINE::TypeCode * OptimizerAlgASyncExample::getTCForOut() const +{ + return _tcInt; } -//! Return the typecode of the expected input type -TypeCode *OptimizerAlgASyncExample::getTCForIn() const +//! Return the typecode of the expected input of the algorithm (algoInit port) +YACS::ENGINE::TypeCode * OptimizerAlgASyncExample::getTCForAlgoInit() const { - return _tcIn; + return _tcInt; } -//! Return the typecode of the expected output type -TypeCode *OptimizerAlgASyncExample::getTCForOut() const +//! Return the typecode of the expected output of the algorithm (algoResult port) +YACS::ENGINE::TypeCode * OptimizerAlgASyncExample::getTCForAlgoResult() const { - return _tcOut; + return _tcInt; +} + +//! Optional method to initialize the algorithm. +/*! + * For now, the parameter input is always NULL. It might be used in the + * future to initialize an algorithm with custom data. + */ +void OptimizerAlgASyncExample::initialize(const YACS::ENGINE::Any *input) + throw (YACS::Exception) +{ + std::cout << "Algo initialize, input = " << input->getIntValue() << std::endl; } //! This method is called only once to launch the algorithm. /*! - * It must first fill the pool with samples to evaluate and call signalMasterAndWait() - * to block until a sample has been evaluated. When returning from this method, it MUST - * check for an eventual termination request (with the method isTerminationRequested()). - * If the termination is requested, the method must perform any necessary cleanup and - * return as soon as possible. Otherwise it can either add new samples to evaluate in - * the pool, do nothing (wait for more samples), or empty the pool and return to finish - * the evaluation. + * It must first fill the pool with samples to evaluate and call + * signalMasterAndWait() to block until a sample has been evaluated. When + * returning from this method, it MUST check for an eventual termination + * request (with the method isTerminationRequested()). If the termination + * is requested, the method must perform any necessary cleanup and return + * as soon as possible. Otherwise it can either add new samples to evaluate + * in the pool, do nothing (wait for more samples), or empty the pool and + * return to finish the evaluation. */ void OptimizerAlgASyncExample::startToTakeDecision() { - double val = 1.2; - for (int i=0 ; i<5 ; i++) { - // push a sample in the input of the slave node - _pool->pushInSample(i, AtomAny::New(val)); - // wait until next sample is ready - signalMasterAndWait(); - // check error notification - if (isTerminationRequested()) { - _pool->destroyAll(); - return; + std::cout << "startToTakeDecision" << std::endl; + int iter = 0; + YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(0.5); + _pool->pushInSample(iter, val); + + signalMasterAndWait(); + while(!isTerminationRequested()) + { + int currentId = _pool->getCurrentId(); + double valIn = _pool->getCurrentInSample()->getDoubleValue(); + int valOut = _pool->getCurrentOutSample()->getIntValue(); + + std::cout << "Compute currentId=" << currentId; + std::cout << ", valIn=" << valIn; + std::cout << ", valOut=" << valOut << std::endl; + + iter++; + if(iter < 3) + { + YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(valIn + 1); + _pool->pushInSample(iter, val); } - - // get a sample from the output of the slave node - Any * v = _pool->getCurrentOutSample(); - val += v->getIntValue(); + signalMasterAndWait(); } +} - // in the end destroy the pool content +/*! + * Optional method called when the algorithm has finished, successfully or + * not, to perform any necessary clean up. + */ +void OptimizerAlgASyncExample::finish() +{ + std::cout << "Algo finish" << std::endl; _pool->destroyAll(); } +/*! + * Return the value of the algoResult port. + */ +YACS::ENGINE::Any * OptimizerAlgASyncExample::getAlgoResult() +{ + YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(42); + return val; +} + //! Factory method to create the algorithm. -OptimizerAlgBase * createOptimizerAlgASyncExample(Pool * pool) +YACS::ENGINE::OptimizerAlgBase * createOptimizerAlgASyncExample(YACS::ENGINE::Pool *pool) { return new OptimizerAlgASyncExample(pool); -} +} \ No newline at end of file diff --git a/src/yacsloader/Test/OptimizerAlgSyncExample.cxx b/src/yacsloader/Test/OptimizerAlgSyncExample.cxx index db7134fae..6161dbf90 100644 --- a/src/yacsloader/Test/OptimizerAlgSyncExample.cxx +++ b/src/yacsloader/Test/OptimizerAlgSyncExample.cxx @@ -22,121 +22,142 @@ #include "OptimizerAlg.hxx" -using namespace YACS::ENGINE; +#include +//using namespace YACS::ENGINE; extern "C" { - OptimizerAlgBase * createOptimizerAlgSyncExample(Pool * pool); + YACS::ENGINE::OptimizerAlgBase * createOptimizerAlgSyncExample(YACS::ENGINE::Pool * pool); } -class OptimizerAlgSyncExample : public OptimizerAlgSync - { +class OptimizerAlgSyncExample : public YACS::ENGINE::OptimizerAlgSync +{ private: - int _idTest; - TypeCode *_tcIn; - TypeCode *_tcOut; + int _iter; + YACS::ENGINE::TypeCode *_tcInt; + YACS::ENGINE::TypeCode *_tcDouble; public: - OptimizerAlgSyncExample(Pool *pool); + OptimizerAlgSyncExample(YACS::ENGINE::Pool *pool); virtual ~OptimizerAlgSyncExample(); - TypeCode *getTCForIn() const; - TypeCode *getTCForOut() const; - void start(); - void takeDecision(); - void initialize(const Any *input) throw(YACS::Exception); - void finish(); - }; - -OptimizerAlgSyncExample::OptimizerAlgSyncExample(Pool *pool) : OptimizerAlgSync(pool), - _tcIn(0), _tcOut(0), - _idTest(0) + + //! returns typecode of type expected as Input. OwnerShip of returned pointer is held by this. + virtual YACS::ENGINE::TypeCode *getTCForIn() const; + //! returns typecode of type expected as Output. OwnerShip of returned pointer is held by this. + virtual YACS::ENGINE::TypeCode *getTCForOut() const; + //! returns typecode of type expected for algo initialization. OwnerShip of returned pointer is held by this. + virtual YACS::ENGINE::TypeCode *getTCForAlgoInit() const; + //! returns typecode of type expected as algo result. OwnerShip of returned pointer is held by this. + virtual YACS::ENGINE::TypeCode *getTCForAlgoResult() const; + virtual void initialize(const YACS::ENGINE::Any *input) throw (YACS::Exception); + virtual void start(); //! Update _pool attribute before performing anything. + virtual void takeDecision();//! _pool->getCurrentId gives the \b id at the origin of this call. + //! Perform the job of analysing to know what new jobs to do (_pool->pushInSample) + //! or in case of convergence _pool->destroyAll + virtual void finish();//! Called when optimization has succeed. + virtual YACS::ENGINE::Any * getAlgoResult(); +}; + +OptimizerAlgSyncExample::OptimizerAlgSyncExample(YACS::ENGINE::Pool *pool) + : YACS::ENGINE::OptimizerAlgSync(pool), _tcInt(0), _tcDouble(0), _iter(0) { - _tcIn=new TypeCode(Double); - _tcOut=new TypeCode(Int); + _tcDouble = new YACS::ENGINE::TypeCode(YACS::ENGINE::Double); + _tcInt = new YACS::ENGINE::TypeCode(YACS::ENGINE::Int); } OptimizerAlgSyncExample::~OptimizerAlgSyncExample() { - std::cout << "Destroying OptimizerAlgSyncExample" << std::endl; - _tcIn->decrRef(); - _tcOut->decrRef(); - std::cout << "Destroyed OptimizerAlgSyncExample" << std::endl; + _tcDouble->decrRef(); + _tcInt->decrRef(); } -//! Return the typecode of the expected input type -TypeCode * OptimizerAlgSyncExample::getTCForIn() const +//! Return the typecode of the expected input of the internal node +YACS::ENGINE::TypeCode * OptimizerAlgSyncExample::getTCForIn() const { - return _tcIn; + return _tcDouble; } -//! Return the typecode of the expected output type -TypeCode * OptimizerAlgSyncExample::getTCForOut() const +//! Return the typecode of the expected output of the internal node +YACS::ENGINE::TypeCode * OptimizerAlgSyncExample::getTCForOut() const { - return _tcOut; + return _tcInt; +} + +//! Return the typecode of the expected input of the algorithm (algoInit port) +YACS::ENGINE::TypeCode * OptimizerAlgSyncExample::getTCForAlgoInit() const +{ + return _tcInt; +} + +//! Return the typecode of the expected output of the algorithm (algoResult port) +YACS::ENGINE::TypeCode * OptimizerAlgSyncExample::getTCForAlgoResult() const +{ + return _tcInt; +} + +//! Optional method to initialize the algorithm. +/*! + * For now, the parameter input is always NULL. It might be used in the + * future to initialize an algorithm with custom data. + */ +void OptimizerAlgSyncExample::initialize(const YACS::ENGINE::Any *input) + throw (YACS::Exception) +{ + std::cout << "Algo initialize, input = " << input->getIntValue() << std::endl; } //! Start to fill the pool with samples to evaluate void OptimizerAlgSyncExample::start() { - _idTest=0; - Any *val=AtomAny::New(1.2); - _pool->pushInSample(4,val); - val=AtomAny::New(3.4); - _pool->pushInSample(9,val); + std::cout << "Algo start " << std::endl; + _iter=0; + YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(0.5); + _pool->pushInSample(_iter,val); } //! This method is called each time a sample has been evaluated. /*! - * It can either add new samples to evaluate in the pool, do nothing (wait for more - * samples), or empty the pool to finish the evaluation. + * It can either add new samples to evaluate in the pool, do nothing (wait + * for more samples), or empty the pool to finish the evaluation. */ void OptimizerAlgSyncExample::takeDecision() { - if(_idTest==1) - { - Any *val=AtomAny::New(5.6); - _pool->pushInSample(16,val); - val=AtomAny::New(7.8); - _pool->pushInSample(25,val); - val=AtomAny::New(9. ); - _pool->pushInSample(36,val); - val=AtomAny::New(12.3); - _pool->pushInSample(49,val); - } - else if(_idTest==4) - { - Any *val=AtomAny::New(45.6); - _pool->pushInSample(64,val); - val=AtomAny::New(78.9); - _pool->pushInSample(81,val); - } - else - { - Any *tmp= _pool->getCurrentInSample(); - if(fabs(tmp->getDoubleValue()-45.6)<1.e-12) - _pool->destroyAll(); - } - _idTest++; + int currentId = _pool->getCurrentId(); + double valIn = _pool->getCurrentInSample()->getDoubleValue(); + int valOut = _pool->getCurrentOutSample()->getIntValue(); + + std::cout << "Algo takeDecision currentId=" << currentId; + std::cout << ", valIn=" << valIn; + std::cout << ", valOut=" << valOut << std::endl; + + _iter++; + if(_iter < 3) + { + YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(valIn + 1); + _pool->pushInSample(_iter, val); + } } -//! Optional method to initialize the algorithm. /*! - * For now, the parameter input is always NULL. It might be used in the future to - * initialize an algorithm with custom data. + * Optional method called when the algorithm has finished, successfully or + * not, to perform any necessary clean up. */ -void OptimizerAlgSyncExample::initialize(const Any *input) throw (YACS::Exception) +void OptimizerAlgSyncExample::finish() { + std::cout << "Algo finish" << std::endl; + _pool->destroyAll(); } /*! - * Optional method called when the algorithm has finished, successfully or not, to - * perform any necessary clean up. + * Return the value of the algoResult port. */ -void OptimizerAlgSyncExample::finish() +YACS::ENGINE::Any * OptimizerAlgSyncExample::getAlgoResult() { + YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(42); + return val; } //! Factory method to create the algorithm. -OptimizerAlgBase * createOptimizerAlgSyncExample(Pool *pool) +YACS::ENGINE::OptimizerAlgBase * createOptimizerAlgSyncExample(YACS::ENGINE::Pool *pool) { return new OptimizerAlgSyncExample(pool); -} +} \ No newline at end of file diff --git a/src/yacsloader/Test/algoasyncexample.py b/src/yacsloader/Test/algoasyncexample.py index c371890f8..430820e5c 100644 --- a/src/yacsloader/Test/algoasyncexample.py +++ b/src/yacsloader/Test/algoasyncexample.py @@ -25,44 +25,77 @@ class myalgoasync(SALOMERuntime.OptimizerAlgASync): r=SALOMERuntime.getSALOMERuntime() self.tin=r.getTypeCode("double") self.tout=r.getTypeCode("int") + self.tAlgoInit=r.getTypeCode("int") + self.tAlgoResult=r.getTypeCode("int") def setPool(self,pool): """Must be implemented to set the pool""" self.pool=pool def getTCForIn(self): - """returns typecode of type expected as Input""" + """return typecode of type expected as Input of the internal node """ return self.tin def getTCForOut(self): - """returns typecode of type expected as Output""" + """return typecode of type expected as Output of the internal node""" return self.tout - def startToTakeDecision(self): - """This method is called only once to launch the algorithm. It must first fill the - pool with samples to evaluate and call self.signalMasterAndWait() to block until a - sample has been evaluated. When returning from this method, it MUST check for an - eventual termination request (with the method self.isTerminationRequested()). If - the termination is requested, the method must perform any necessary cleanup and - return as soon as possible. Otherwise it can either add new samples to evaluate in - the pool, do nothing (wait for more samples), or empty the pool and return to - finish the evaluation. + def getTCForAlgoInit(self): + """return typecode of type expected as input for initialize """ + return self.tAlgoInit + + def getTCForAlgoResult(self): + """return typecode of type expected as output of the algorithm """ + return self.tAlgoResult + + def initialize(self,input): + """Optional method called on initialization. + The type of "input" is returned by "getTCForAlgoInit" """ - val=1.2 - for iter in xrange(5): - #push a sample in the input of the slave node - self.pool.pushInSample(iter,val) - #wait until next sample is ready - self.signalMasterAndWait() - #check error notification - if self.isTerminationRequested(): - self.pool.destroyAll() - return + print "Algo initialize, input = ", input.getIntValue() - #get a sample from the output of the slave node + def startToTakeDecision(self): + """This method is called only once to launch the algorithm. It must + first fill the pool with samples to evaluate and call + self.signalMasterAndWait() to block until a sample has been + evaluated. When returning from this method, it MUST check for an + eventual termination request (with the method + self.isTerminationRequested()). If the termination is requested, the + method must perform any necessary cleanup and return as soon as + possible. Otherwise it can either add new samples to evaluate in the + pool, do nothing (wait for more samples), or empty the pool and + return to finish the evaluation. + """ + print "startToTakeDecision" + # fill the pool with samples + iter=0 + self.pool.pushInSample(0, 0.5) + + # + self.signalMasterAndWait() + while not self.isTerminationRequested(): currentId=self.pool.getCurrentId() - v=self.pool.getCurrentOutSample() - val=val+v.getIntValue() + valIn = self.pool.getCurrentInSample().getDoubleValue() + valOut = self.pool.getCurrentOutSample().getIntValue() + print "Compute currentId=%s, valIn=%s, valOut=%s" % (currentId, valIn, valOut) + iter=iter+1 + + if iter < 3: + nextSample = valIn + 1 + self.pool.pushInSample(iter, nextSample) + + self.signalMasterAndWait() - #in the end destroy the pool content + def finish(self): + """Optional method called when the algorithm has finished, successfully + or not, to perform any necessary clean up.""" + print "Algo finish" self.pool.destroyAll() + + def getAlgoResult(self): + """return the result of the algorithm. + The object returned is of type indicated by getTCForAlgoResult. + """ + return 42 + + diff --git a/src/yacsloader/Test/algosyncexample.py b/src/yacsloader/Test/algosyncexample.py index 1ccf6bda4..d40a93764 100644 --- a/src/yacsloader/Test/algosyncexample.py +++ b/src/yacsloader/Test/algosyncexample.py @@ -25,49 +25,68 @@ class myalgosync(SALOMERuntime.OptimizerAlgSync): r=SALOMERuntime.getSALOMERuntime() self.tin=r.getTypeCode("double") self.tout=r.getTypeCode("int") + self.tAlgoInit=r.getTypeCode("int") + self.tAlgoResult=r.getTypeCode("int") def setPool(self,pool): """Must be implemented to set the pool""" self.pool=pool def getTCForIn(self): - """returns typecode of type expected as Input""" + """return typecode of type expected as Input of the internal node """ return self.tin def getTCForOut(self): - """returns typecode of type expected as Output""" + """return typecode of type expected as Output of the internal node""" return self.tout + def getTCForAlgoInit(self): + """return typecode of type expected as input for initialize """ + return self.tAlgoInit + + def getTCForAlgoResult(self): + """return typecode of type expected as output of the algorithm """ + return self.tAlgoResult + def initialize(self,input): - """Optional method called on initialization. Do nothing here""" + """Optional method called on initialization. + The type of "input" is returned by "getTCForAlgoInit" + """ + print "Algo initialize, input = ", input.getIntValue() def start(self): """Start to fill the pool with samples to evaluate.""" + print "Algo start " self.iter=0 - self.pool.pushInSample(4,1.2) - self.pool.pushInSample(9,3.4) + # pushInSample(id, value) + self.pool.pushInSample(self.iter, 0.5) def takeDecision(self): - """ This method is called each time a sample has been evaluated. It can either add - new samples to evaluate in the pool, do nothing (wait for more samples), or empty - the pool to finish the evaluation. + """ This method is called each time a sample has been evaluated. It can + either add new samples to evaluate in the pool, do nothing (wait for + more samples), or empty the pool to finish the evaluation. """ currentId=self.pool.getCurrentId() + valIn = self.pool.getCurrentInSample().getDoubleValue() + valOut = self.pool.getCurrentOutSample().getIntValue() + print "Algo takeDecision currentId=%s, valIn=%s, valOut=%s" % (currentId, valIn, valOut) - if self.iter==1: - self.pool.pushInSample(16,5.6) - self.pool.pushInSample(25,7.8) - self.pool.pushInSample(36,9.) - self.pool.pushInSample(49,12.3) - elif self.iter==4: - self.pool.pushInSample(64,45.6) - self.pool.pushInSample(81,78.9) - else: - val=self.pool.getCurrentInSample() - if abs(val.getDoubleValue()-45.6) < 1.e-12: - self.pool.destroyAll() self.iter=self.iter+1 + if self.iter < 3: + # continue + nextSample = valIn + 1 + self.pool.pushInSample(self.iter, nextSample) def finish(self): - """Optional method called when the algorithm has finished, successfully or not, to - perform any necessary clean up. Do nothing here""" + """Optional method called when the algorithm has finished, successfully + or not, to perform any necessary clean up.""" + print "Algo finish" + self.pool.destroyAll() + + def getAlgoResult(self): + """return the result of the algorithm. + The object returned is of type indicated by getTCForAlgoResult. + """ + return 42 + + diff --git a/src/yacsloader/samples/optimizer_async_cpp.xml b/src/yacsloader/samples/optimizer_async_cpp.xml index 223f38e06..bf6932013 100644 --- a/src/yacsloader/samples/optimizer_async_cpp.xml +++ b/src/yacsloader/samples/optimizer_async_cpp.xml @@ -32,38 +32,64 @@ + + + + + + + + + - - - - - - - + + + + + + + + + 5 + + + + + + OptimizerLoop1 OutNode5 + DataIn3 OptimizerLoop1 + + OptimizerLoop1 algoResults + OutNode5 i6 + + + OptimizerLoop1 evalSamples + OptimizerLoop1.PyScript7 i8 + - OptimizerLoop0 evalSamples - OptimizerLoop0.PyFunction1 inValue + DataIn3 o4 + OptimizerLoop1 algoInit - OptimizerLoop0.PyFunction1 outValue - OptimizerLoop0 evalResults + OptimizerLoop1.PyScript7 o9 + OptimizerLoop1 evalResults - OptimizerLoop0nbBranches - 4 + OptimizerLoop1nbBranches + 1 - - - + + + + + diff --git a/src/yacsloader/samples/optimizer_async_py.xml b/src/yacsloader/samples/optimizer_async_py.xml index de8ba0480..115bb6824 100644 --- a/src/yacsloader/samples/optimizer_async_py.xml +++ b/src/yacsloader/samples/optimizer_async_py.xml @@ -32,38 +32,64 @@ + + + + + + + + + - - - - - - - + + + + + + + + + 5 + + + + + + OptimizerLoop1 OutNode5 + DataIn3 OptimizerLoop1 + + OptimizerLoop1 algoResults + OutNode5 i6 + + + OptimizerLoop1 evalSamples + OptimizerLoop1.PyScript7 i8 + - OptimizerLoop0 evalSamples - OptimizerLoop0.PyFunction0 inputValue + DataIn3 o4 + OptimizerLoop1 algoInit - OptimizerLoop0.PyFunction0 outputValue - OptimizerLoop0 evalResults + OptimizerLoop1.PyScript7 o9 + OptimizerLoop1 evalResults - OptimizerLoop0nbBranches - 4 + OptimizerLoop1nbBranches + 1 - - - + + + + + diff --git a/src/yacsloader/samples/optimizer_sync_cpp.xml b/src/yacsloader/samples/optimizer_sync_cpp.xml index 97394e915..59b78017e 100644 --- a/src/yacsloader/samples/optimizer_sync_cpp.xml +++ b/src/yacsloader/samples/optimizer_sync_cpp.xml @@ -32,38 +32,64 @@ + + + + + + + + + - - - - - - - + + + + + + + + + 5 + + + + + + OptimizerLoop1 OutNode5 + DataIn3 OptimizerLoop1 + + OptimizerLoop1 algoResults + OutNode5 i6 + + + OptimizerLoop1 evalSamples + OptimizerLoop1.PyScript7 i8 + - OptimizerLoop0 evalSamples - OptimizerLoop0.PyFunction1 inValue + DataIn3 o4 + OptimizerLoop1 algoInit - OptimizerLoop0.PyFunction1 outValue - OptimizerLoop0 evalResults + OptimizerLoop1.PyScript7 o9 + OptimizerLoop1 evalResults - OptimizerLoop0nbBranches - 4 + OptimizerLoop1nbBranches + 1 - - - + + + + + diff --git a/src/yacsloader/samples/optimizer_sync_py.xml b/src/yacsloader/samples/optimizer_sync_py.xml index 3da9d59c6..0b69be3fc 100644 --- a/src/yacsloader/samples/optimizer_sync_py.xml +++ b/src/yacsloader/samples/optimizer_sync_py.xml @@ -32,38 +32,64 @@ + + + + + + + + + - - - - - - - + + + + + + + + + 5 + + + + + + OptimizerLoop1 OutNode5 + DataIn3 OptimizerLoop1 + + OptimizerLoop1 algoResults + OutNode5 i6 + + + OptimizerLoop1 evalSamples + OptimizerLoop1.PyScript7 i8 + - OptimizerLoop0 evalSamples - OptimizerLoop0.PyFunction0 inputValue + DataIn3 o4 + OptimizerLoop1 algoInit - OptimizerLoop0.PyFunction0 outputValue - OptimizerLoop0 evalResults + OptimizerLoop1.PyScript7 o9 + OptimizerLoop1 evalResults - OptimizerLoop0nbBranches - 4 + OptimizerLoop1nbBranches + 1 - - - + + + + +