Salome HOME
updated copyright message
[modules/yacs.git] / src / yacsloader / Test / OptimizerAlgSyncExample.cxx
1 // Copyright (C) 2006-2023  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);
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 {
104   std::cout << "Algo initialize, input = " << input->getIntValue() << std::endl;
105 }
106
107 //! Start to fill the pool with samples to evaluate
108 void OptimizerAlgSyncExample::start()
109 {
110   std::cout << "Algo start " << std::endl;
111   _iter=0;
112   YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(0.5);
113   _pool->pushInSample(_iter,val);
114 }
115
116 //! This method is called each time a sample has been evaluated.
117 /*!
118  *  It can either add new samples to evaluate in the pool, do nothing (wait
119  *  for more samples), or empty the pool to finish the evaluation.
120  */
121 void OptimizerAlgSyncExample::takeDecision()
122 {
123   int currentId = _pool->getCurrentId();
124   double valIn  = _pool->getCurrentInSample()->getDoubleValue();
125   int valOut    = _pool->getCurrentOutSample()->getIntValue();
126   
127   std::cout << "Algo takeDecision currentId=" << currentId;
128   std::cout << ", valIn=" << valIn;
129   std::cout << ", valOut=" << valOut << std::endl;
130   
131   _iter++;
132   if(_iter < 3)
133   {
134     YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(valIn + 1);
135     _pool->pushInSample(_iter, val);
136   }
137 }
138
139 /*!
140  *  Optional method called when the algorithm has finished, successfully or
141  *  not, to perform any necessary clean up.
142  */
143 void OptimizerAlgSyncExample::finish()
144 {
145   std::cout << "Algo finish" << std::endl;
146   _pool->destroyAll();
147 }
148
149 /*!
150  *  Return the value of the algoResult port.
151  */
152 YACS::ENGINE::Any * OptimizerAlgSyncExample::getAlgoResult()
153 {
154   YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(42);
155   return val;
156 }
157
158 //! Factory method to create the algorithm.
159 YACS::ENGINE::OptimizerAlgBase * createOptimizerAlgSyncExample(YACS::ENGINE::Pool *pool)
160 {
161   return new OptimizerAlgSyncExample(pool);
162 }