1 // Copyright (C) 2006-2014 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
23 #include "OptimizerAlg.hxx"
26 //using namespace YACS::ENGINE;
30 YACS::ENGINE::OptimizerAlgBase * createOptimizerAlgSyncExample(YACS::ENGINE::Pool * pool);
33 class OptimizerAlgSyncExample : public YACS::ENGINE::OptimizerAlgSync
37 YACS::ENGINE::TypeCode *_tcInt;
38 YACS::ENGINE::TypeCode *_tcDouble;
40 OptimizerAlgSyncExample(YACS::ENGINE::Pool *pool);
41 virtual ~OptimizerAlgSyncExample();
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();
60 OptimizerAlgSyncExample::OptimizerAlgSyncExample(YACS::ENGINE::Pool *pool)
61 : YACS::ENGINE::OptimizerAlgSync(pool), _tcInt(0), _tcDouble(0), _iter(0)
63 _tcDouble = new YACS::ENGINE::TypeCode(YACS::ENGINE::Double);
64 _tcInt = new YACS::ENGINE::TypeCode(YACS::ENGINE::Int);
67 OptimizerAlgSyncExample::~OptimizerAlgSyncExample()
73 //! Return the typecode of the expected input of the internal node
74 YACS::ENGINE::TypeCode * OptimizerAlgSyncExample::getTCForIn() const
79 //! Return the typecode of the expected output of the internal node
80 YACS::ENGINE::TypeCode * OptimizerAlgSyncExample::getTCForOut() const
85 //! Return the typecode of the expected input of the algorithm (algoInit port)
86 YACS::ENGINE::TypeCode * OptimizerAlgSyncExample::getTCForAlgoInit() const
91 //! Return the typecode of the expected output of the algorithm (algoResult port)
92 YACS::ENGINE::TypeCode * OptimizerAlgSyncExample::getTCForAlgoResult() const
97 //! Optional method to initialize the algorithm.
99 * For now, the parameter input is always NULL. It might be used in the
100 * future to initialize an algorithm with custom data.
102 void OptimizerAlgSyncExample::initialize(const YACS::ENGINE::Any *input)
103 throw (YACS::Exception)
105 std::cout << "Algo initialize, input = " << input->getIntValue() << std::endl;
108 //! Start to fill the pool with samples to evaluate
109 void OptimizerAlgSyncExample::start()
111 std::cout << "Algo start " << std::endl;
113 YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(0.5);
114 _pool->pushInSample(_iter,val);
117 //! This method is called each time a sample has been evaluated.
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.
122 void OptimizerAlgSyncExample::takeDecision()
124 int currentId = _pool->getCurrentId();
125 double valIn = _pool->getCurrentInSample()->getDoubleValue();
126 int valOut = _pool->getCurrentOutSample()->getIntValue();
128 std::cout << "Algo takeDecision currentId=" << currentId;
129 std::cout << ", valIn=" << valIn;
130 std::cout << ", valOut=" << valOut << std::endl;
135 YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(valIn + 1);
136 _pool->pushInSample(_iter, val);
141 * Optional method called when the algorithm has finished, successfully or
142 * not, to perform any necessary clean up.
144 void OptimizerAlgSyncExample::finish()
146 std::cout << "Algo finish" << std::endl;
151 * Return the value of the algoResult port.
153 YACS::ENGINE::Any * OptimizerAlgSyncExample::getAlgoResult()
155 YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(42);
159 //! Factory method to create the algorithm.
160 YACS::ENGINE::OptimizerAlgBase * createOptimizerAlgSyncExample(YACS::ENGINE::Pool *pool)
162 return new OptimizerAlgSyncExample(pool);