Salome HOME
Updated copyright comment
[modules/yacs.git] / src / yacsloader / Test / OptimizerAlgASyncExample.cxx
1 // Copyright (C) 2006-2024  CEA, EDF
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 "OptimizerAlg.hxx"
21
22 #include <iostream>
23 //using namespace YACS::ENGINE;
24
25 extern "C"
26 {
27   YACS::ENGINE::OptimizerAlgBase * createOptimizerAlgASyncExample(YACS::ENGINE::Pool * pool);
28 }
29
30 class OptimizerAlgASyncExample : public YACS::ENGINE::OptimizerAlgASync
31 {
32   private:
33     YACS::ENGINE::TypeCode *_tcInt;
34     YACS::ENGINE::TypeCode *_tcDouble;
35   public:
36     OptimizerAlgASyncExample(YACS::ENGINE::Pool *pool);
37     virtual ~OptimizerAlgASyncExample();
38     
39     //! returns typecode of type expected as Input. OwnerShip of returned pointer is held by this.
40     virtual YACS::ENGINE::TypeCode *getTCForIn() const;
41     //! returns typecode of type expected as Output. OwnerShip of returned pointer is held by this.
42     virtual YACS::ENGINE::TypeCode *getTCForOut() const;
43     //! returns typecode of type expected for algo initialization. OwnerShip of returned pointer is held by this.
44     virtual YACS::ENGINE::TypeCode *getTCForAlgoInit() const;
45     //! returns typecode of type expected as algo result. OwnerShip of returned pointer is held by this.
46     virtual YACS::ENGINE::TypeCode *getTCForAlgoResult() const;
47     virtual void initialize(const YACS::ENGINE::Any *input);
48     virtual void startToTakeDecision();
49     virtual void finish();//! Called when optimization has succeed.
50     virtual YACS::ENGINE::Any * getAlgoResult();
51 };
52
53 OptimizerAlgASyncExample::OptimizerAlgASyncExample(YACS::ENGINE::Pool *pool)
54   : YACS::ENGINE::OptimizerAlgASync(pool), _tcInt(0), _tcDouble(0)
55 {
56   _tcDouble = new YACS::ENGINE::TypeCode(YACS::ENGINE::Double);
57   _tcInt    = new YACS::ENGINE::TypeCode(YACS::ENGINE::Int);
58 }
59
60 OptimizerAlgASyncExample::~OptimizerAlgASyncExample()
61 {
62   _tcDouble->decrRef();
63   _tcInt->decrRef();
64 }
65
66 //! Return the typecode of the expected input of the internal node
67 YACS::ENGINE::TypeCode * OptimizerAlgASyncExample::getTCForIn() const
68 {
69   return _tcDouble;
70 }
71
72 //! Return the typecode of the expected output of the internal node
73 YACS::ENGINE::TypeCode * OptimizerAlgASyncExample::getTCForOut() const
74 {
75   return _tcInt;
76 }
77
78 //! Return the typecode of the expected input of the algorithm (algoInit port)
79 YACS::ENGINE::TypeCode * OptimizerAlgASyncExample::getTCForAlgoInit() const
80 {
81   return _tcInt;
82 }
83
84 //! Return the typecode of the expected output of the algorithm (algoResult port)
85 YACS::ENGINE::TypeCode * OptimizerAlgASyncExample::getTCForAlgoResult() const
86 {
87   return _tcInt;
88 }
89
90 //! Optional method to initialize the algorithm.
91 /*!
92  *  For now, the parameter input is always NULL. It might be used in the
93  *  future to initialize an algorithm with custom data.
94  */
95 void OptimizerAlgASyncExample::initialize(const YACS::ENGINE::Any *input)
96 {
97   std::cout << "Algo initialize, input = " << input->getIntValue() << std::endl;
98 }
99
100 //! This method is called only once to launch the algorithm.
101 /*!
102  *  It must first fill the pool with samples to evaluate and call
103  *  signalMasterAndWait() to block until a sample has been evaluated. When
104  *  returning from this method, it MUST check for an eventual termination
105  *  request (with the method isTerminationRequested()). If the termination
106  *  is requested, the method must perform any necessary cleanup and return
107  *  as soon as possible. Otherwise it can either add new samples to evaluate
108  *  in the pool, do nothing (wait for more samples), or empty the pool and
109  *  return to finish the evaluation.
110  */
111 void OptimizerAlgASyncExample::startToTakeDecision()
112 {
113   std::cout << "startToTakeDecision" << std::endl;
114   int iter = 0;
115   YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(0.5);
116   _pool->pushInSample(iter, val);
117   
118   signalMasterAndWait();
119   while(!isTerminationRequested())
120   {
121     int currentId = _pool->getCurrentId();
122     double valIn  = _pool->getCurrentInSample()->getDoubleValue();
123     int valOut    = _pool->getCurrentOutSample()->getIntValue();
124     
125     std::cout << "Compute currentId=" << currentId;
126     std::cout << ", valIn=" << valIn;
127     std::cout << ", valOut=" << valOut << std::endl;
128     
129     iter++;
130     if(iter < 3)
131     {
132       YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(valIn + 1);
133       _pool->pushInSample(iter, val);
134     }
135     signalMasterAndWait();
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 OptimizerAlgASyncExample::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 * OptimizerAlgASyncExample::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 * createOptimizerAlgASyncExample(YACS::ENGINE::Pool *pool)
160 {
161   return new OptimizerAlgASyncExample(pool);
162 }