Salome HOME
d944f12de3c7635b52984a4175304082bc012b62
[modules/yacs.git] / src / yacsloader / Test / algoasyncexample.py
1 # Copyright (C) 2006-2021  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 import SALOMERuntime
21
22 class myalgoasync(SALOMERuntime.OptimizerAlgASync):
23   def __init__(self):
24     SALOMERuntime.OptimizerAlgASync.__init__(self, None)
25     r=SALOMERuntime.getSALOMERuntime()
26     self.tin=r.getTypeCode("double")
27     self.tout=r.getTypeCode("int")
28     self.tAlgoInit=r.getTypeCode("int")
29     self.tAlgoResult=r.getTypeCode("int")
30
31   def setPool(self,pool):
32     """Must be implemented to set the pool"""
33     self.pool=pool
34
35   def getTCForIn(self):
36     """return typecode of type expected as Input of the internal node """
37     return self.tin
38
39   def getTCForOut(self):
40     """return typecode of type expected as Output of the internal node"""
41     return self.tout
42
43   def getTCForAlgoInit(self):
44     """return typecode of type expected as input for initialize """
45     return self.tAlgoInit
46
47   def getTCForAlgoResult(self):
48     """return typecode of type expected as output of the algorithm """
49     return self.tAlgoResult
50
51   def initialize(self,input):
52     """Optional method called on initialization.
53        The type of "input" is returned by "getTCForAlgoInit"
54     """
55     print("Algo initialize, input = ", input.getIntValue())
56
57   def startToTakeDecision(self):
58     """This method is called only once to launch the algorithm. It must
59        first fill the pool with samples to evaluate and call
60        self.signalMasterAndWait() to block until a sample has been
61        evaluated. When returning from this method, it MUST check for an
62        eventual termination request (with the method
63        self.isTerminationRequested()). If the termination is requested, the
64        method must perform any necessary cleanup and return as soon as
65        possible. Otherwise it can either add new samples to evaluate in the
66        pool, do nothing (wait for more samples), or empty the pool and
67        return to finish the evaluation.
68     """
69     print("startToTakeDecision")
70     # fill the pool with samples
71     iter=0
72     self.pool.pushInSample(0, 0.5)
73     
74     # 
75     self.signalMasterAndWait()
76     while not self.isTerminationRequested():
77       currentId=self.pool.getCurrentId()
78       valIn = self.pool.getCurrentInSample().getDoubleValue()
79       valOut = self.pool.getCurrentOutSample().getIntValue()
80       print("Compute currentId=%s, valIn=%s, valOut=%s" % (currentId, valIn, valOut))
81       iter=iter+1
82       
83       if iter < 3:
84         nextSample = valIn + 1
85         self.pool.pushInSample(iter, nextSample)
86         
87       self.signalMasterAndWait()
88
89   def finish(self):
90     """Optional method called when the algorithm has finished, successfully
91        or not, to perform any necessary clean up."""
92     print("Algo finish")
93     self.pool.destroyAll()
94
95   def getAlgoResult(self):
96     """return the result of the algorithm.
97        The object returned is of type indicated by getTCForAlgoResult.
98     """
99     return 42
100
101