Salome HOME
Merge remote branch 'origin/ilh/pmml'
[modules/yacs.git] / src / yacsloader / Test / OptimizerAlgSyncExample.cxx
1 // Copyright (C) 2006-2014  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <cmath>
21 #include <iostream>
22
23 #include "OptimizerAlg.hxx"
24
25 #include <iostream>
26 //using namespace YACS::ENGINE;
27
28 extern "C"
29 {
30   YACS::ENGINE::OptimizerAlgBase * createOptimizerAlgSyncExample(YACS::ENGINE::Pool * pool);
31 }
32
33 class OptimizerAlgSyncExample : public YACS::ENGINE::OptimizerAlgSync
34 {
35   private:
36     int _iter;
37     YACS::ENGINE::TypeCode *_tcInt;
38     YACS::ENGINE::TypeCode *_tcDouble;
39   public:
40     OptimizerAlgSyncExample(YACS::ENGINE::Pool *pool);
41     virtual ~OptimizerAlgSyncExample();
42     
43     //! returns typecode of type expected as Input. OwnerShip of returned pointer is held by this.
44     virtual YACS::ENGINE::TypeCode *getTCForIn() const;
45     //! returns typecode of type expected as Output. OwnerShip of returned pointer is held by this.
46     virtual YACS::ENGINE::TypeCode *getTCForOut() const;
47     //! returns typecode of type expected for algo initialization. OwnerShip of returned pointer is held by this.
48     virtual YACS::ENGINE::TypeCode *getTCForAlgoInit() const;
49     //! returns typecode of type expected as algo result. OwnerShip of returned pointer is held by this.
50     virtual YACS::ENGINE::TypeCode *getTCForAlgoResult() const;
51     virtual void initialize(const YACS::ENGINE::Any *input) throw (YACS::Exception);
52     virtual void start(); //! Update _pool attribute before performing anything.
53     virtual void takeDecision();//! _pool->getCurrentId gives the \b id at the origin of this call.
54                                 //! Perform the job of analysing to know what new jobs to do (_pool->pushInSample)
55                                 //! or in case of convergence _pool->destroyAll
56     virtual void finish();//! Called when optimization has succeed.
57     virtual YACS::ENGINE::Any * getAlgoResult();
58 };
59
60 OptimizerAlgSyncExample::OptimizerAlgSyncExample(YACS::ENGINE::Pool *pool)
61   : YACS::ENGINE::OptimizerAlgSync(pool), _tcInt(0), _tcDouble(0), _iter(0)
62 {
63   _tcDouble = new YACS::ENGINE::TypeCode(YACS::ENGINE::Double);
64   _tcInt    = new YACS::ENGINE::TypeCode(YACS::ENGINE::Int);
65 }
66
67 OptimizerAlgSyncExample::~OptimizerAlgSyncExample()
68 {
69   _tcDouble->decrRef();
70   _tcInt->decrRef();
71 }
72
73 //! Return the typecode of the expected input of the internal node
74 YACS::ENGINE::TypeCode * OptimizerAlgSyncExample::getTCForIn() const
75 {
76   return _tcDouble;
77 }
78
79 //! Return the typecode of the expected output of the internal node
80 YACS::ENGINE::TypeCode * OptimizerAlgSyncExample::getTCForOut() const
81 {
82   return _tcInt;
83 }
84
85 //! Return the typecode of the expected input of the algorithm (algoInit port)
86 YACS::ENGINE::TypeCode * OptimizerAlgSyncExample::getTCForAlgoInit() const
87 {
88   return _tcInt;
89 }
90
91 //! Return the typecode of the expected output of the algorithm (algoResult port)
92 YACS::ENGINE::TypeCode * OptimizerAlgSyncExample::getTCForAlgoResult() const
93 {
94   return _tcInt;
95 }
96
97 //! Optional method to initialize the algorithm.
98 /*!
99  *  For now, the parameter input is always NULL. It might be used in the
100  *  future to initialize an algorithm with custom data.
101  */
102 void OptimizerAlgSyncExample::initialize(const YACS::ENGINE::Any *input)
103   throw (YACS::Exception)
104 {
105   std::cout << "Algo initialize, input = " << input->getIntValue() << std::endl;
106 }
107
108 //! Start to fill the pool with samples to evaluate
109 void OptimizerAlgSyncExample::start()
110 {
111   std::cout << "Algo start " << std::endl;
112   _iter=0;
113   YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(0.5);
114   _pool->pushInSample(_iter,val);
115 }
116
117 //! This method is called each time a sample has been evaluated.
118 /*!
119  *  It can either add new samples to evaluate in the pool, do nothing (wait
120  *  for more samples), or empty the pool to finish the evaluation.
121  */
122 void OptimizerAlgSyncExample::takeDecision()
123 {
124   int currentId = _pool->getCurrentId();
125   double valIn  = _pool->getCurrentInSample()->getDoubleValue();
126   int valOut    = _pool->getCurrentOutSample()->getIntValue();
127   
128   std::cout << "Algo takeDecision currentId=" << currentId;
129   std::cout << ", valIn=" << valIn;
130   std::cout << ", valOut=" << valOut << std::endl;
131   
132   _iter++;
133   if(_iter < 3)
134   {
135     YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(valIn + 1);
136     _pool->pushInSample(_iter, val);
137   }
138 }
139
140 /*!
141  *  Optional method called when the algorithm has finished, successfully or
142  *  not, to perform any necessary clean up.
143  */
144 void OptimizerAlgSyncExample::finish()
145 {
146   std::cout << "Algo finish" << std::endl;
147   _pool->destroyAll();
148 }
149
150 /*!
151  *  Return the value of the algoResult port.
152  */
153 YACS::ENGINE::Any * OptimizerAlgSyncExample::getAlgoResult()
154 {
155   YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(42);
156   return val;
157 }
158
159 //! Factory method to create the algorithm.
160 YACS::ENGINE::OptimizerAlgBase * createOptimizerAlgSyncExample(YACS::ENGINE::Pool *pool)
161 {
162   return new OptimizerAlgSyncExample(pool);
163 }