Salome HOME
Merge from V6_main 01/04/2013
[modules/yacs.git] / src / yacsloader / Test / algosyncexample.py
1 # Copyright (C) 2006-2013  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.
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 import SALOMERuntime
21
22 class myalgosync(SALOMERuntime.OptimizerAlgSync):
23   def __init__(self):
24     SALOMERuntime.OptimizerAlgSync.__init__(self, None)
25     r=SALOMERuntime.getSALOMERuntime()
26     self.tin=r.getTypeCode("double")
27     self.tout=r.getTypeCode("int")
28
29   def setPool(self,pool):
30     """Must be implemented to set the pool"""
31     self.pool=pool
32
33   def getTCForIn(self):
34     """returns typecode of type expected as Input"""
35     return self.tin
36
37   def getTCForOut(self):
38     """returns typecode of type expected as Output"""
39     return self.tout
40
41   def initialize(self,input):
42     """Optional method called on initialization. Do nothing here"""
43
44   def start(self):
45     """Start to fill the pool with samples to evaluate."""
46     self.iter=0
47     self.pool.pushInSample(4,1.2)
48     self.pool.pushInSample(9,3.4)
49
50   def takeDecision(self):
51     """ This method is called each time a sample has been evaluated. It can either add
52         new samples to evaluate in the pool, do nothing (wait for more samples), or empty
53         the pool to finish the evaluation.
54     """
55     currentId=self.pool.getCurrentId()
56
57     if self.iter==1:
58       self.pool.pushInSample(16,5.6)
59       self.pool.pushInSample(25,7.8)
60       self.pool.pushInSample(36,9.)
61       self.pool.pushInSample(49,12.3)
62     elif self.iter==4:
63       self.pool.pushInSample(64,45.6)
64       self.pool.pushInSample(81,78.9)
65     else:
66       val=self.pool.getCurrentInSample()
67       if abs(val.getDoubleValue()-45.6) < 1.e-12:
68         self.pool.destroyAll()
69     self.iter=self.iter+1
70
71   def finish(self):
72     """Optional method called when the algorithm has finished, successfully or not, to
73        perform any necessary clean up. Do nothing here"""