1 // Copyright (C) 2006-2021 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "OptimizerAlg.hxx"
23 //using namespace YACS::ENGINE;
27 YACS::ENGINE::OptimizerAlgBase * createOptimizerAlgASyncExample(YACS::ENGINE::Pool * pool);
30 class OptimizerAlgASyncExample : public YACS::ENGINE::OptimizerAlgASync
33 YACS::ENGINE::TypeCode *_tcInt;
34 YACS::ENGINE::TypeCode *_tcDouble;
36 OptimizerAlgASyncExample(YACS::ENGINE::Pool *pool);
37 virtual ~OptimizerAlgASyncExample();
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();
53 OptimizerAlgASyncExample::OptimizerAlgASyncExample(YACS::ENGINE::Pool *pool)
54 : YACS::ENGINE::OptimizerAlgASync(pool), _tcInt(0), _tcDouble(0)
56 _tcDouble = new YACS::ENGINE::TypeCode(YACS::ENGINE::Double);
57 _tcInt = new YACS::ENGINE::TypeCode(YACS::ENGINE::Int);
60 OptimizerAlgASyncExample::~OptimizerAlgASyncExample()
66 //! Return the typecode of the expected input of the internal node
67 YACS::ENGINE::TypeCode * OptimizerAlgASyncExample::getTCForIn() const
72 //! Return the typecode of the expected output of the internal node
73 YACS::ENGINE::TypeCode * OptimizerAlgASyncExample::getTCForOut() const
78 //! Return the typecode of the expected input of the algorithm (algoInit port)
79 YACS::ENGINE::TypeCode * OptimizerAlgASyncExample::getTCForAlgoInit() const
84 //! Return the typecode of the expected output of the algorithm (algoResult port)
85 YACS::ENGINE::TypeCode * OptimizerAlgASyncExample::getTCForAlgoResult() const
90 //! Optional method to initialize the algorithm.
92 * For now, the parameter input is always NULL. It might be used in the
93 * future to initialize an algorithm with custom data.
95 void OptimizerAlgASyncExample::initialize(const YACS::ENGINE::Any *input)
97 std::cout << "Algo initialize, input = " << input->getIntValue() << std::endl;
100 //! This method is called only once to launch the algorithm.
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.
111 void OptimizerAlgASyncExample::startToTakeDecision()
113 std::cout << "startToTakeDecision" << std::endl;
115 YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(0.5);
116 _pool->pushInSample(iter, val);
118 signalMasterAndWait();
119 while(!isTerminationRequested())
121 int currentId = _pool->getCurrentId();
122 double valIn = _pool->getCurrentInSample()->getDoubleValue();
123 int valOut = _pool->getCurrentOutSample()->getIntValue();
125 std::cout << "Compute currentId=" << currentId;
126 std::cout << ", valIn=" << valIn;
127 std::cout << ", valOut=" << valOut << std::endl;
132 YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(valIn + 1);
133 _pool->pushInSample(iter, val);
135 signalMasterAndWait();
140 * Optional method called when the algorithm has finished, successfully or
141 * not, to perform any necessary clean up.
143 void OptimizerAlgASyncExample::finish()
145 std::cout << "Algo finish" << std::endl;
150 * Return the value of the algoResult port.
152 YACS::ENGINE::Any * OptimizerAlgASyncExample::getAlgoResult()
154 YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(42);
158 //! Factory method to create the algorithm.
159 YACS::ENGINE::OptimizerAlgBase * createOptimizerAlgASyncExample(YACS::ENGINE::Pool *pool)
161 return new OptimizerAlgASyncExample(pool);