Salome HOME
Copyright update 2021
[modules/yacs.git] / src / yacsloader / Test / optim_plugin.py
1 # Copyright (C) 2015-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 import SALOMERuntime
20
21 class myalgosync(SALOMERuntime.OptimizerAlgSync):
22   def __init__(self):
23     SALOMERuntime.OptimizerAlgSync.__init__(self, None)
24     r=SALOMERuntime.getSALOMERuntime()
25     self.tin=r.getTypeCode("int")
26     self.tout=r.getTypeCode("int")
27     self.tAlgoInit=r.getTypeCode("int")
28     self.tAlgoResult=r.getTypeCode("int")
29
30   def setPool(self,pool):
31     """Must be implemented to set the pool"""
32     self.pool=pool
33
34   def getTCForIn(self):
35     """return typecode of type expected as Input the internal node """
36     return self.tin
37
38   def getTCForOut(self):
39     """return typecode of type expected as Output the internal node"""
40     return self.tout
41
42   def getTCForAlgoInit(self):
43     """return typecode of type expected as input for initialize """
44     return self.tAlgoInit
45
46   def getTCForAlgoResult(self):
47     """return typecode of type expected as output of the algorithm """
48     return self.tAlgoResult
49
50   def initialize(self,input):
51     """Optional method called on initialization.
52        The type of "input" is returned by "getTCForAlgoInit"
53     """
54     print("Algo initialize, input = ", input.getIntValue())
55
56   def start(self):
57     """Start to fill the pool with samples to evaluate."""
58     print("Algo start ")
59     self.iter=0
60     # pushInSample(id, value)
61     self.pool.pushInSample(self.iter, 1)
62
63   def takeDecision(self):
64     """ This method is called each time a sample has been evaluated. It can
65         either add new samples to evaluate in the pool, do nothing (wait for
66         more samples), or empty the pool to finish the evaluation.
67     """
68     currentId=self.pool.getCurrentId()
69     valIn = self.pool.getCurrentInSample().getIntValue()
70     valOut = self.pool.getCurrentOutSample().getIntValue()
71     print("Algo takeDecision currentId=%s, valIn=%s, valOut=%s" % (currentId, valIn, valOut))
72
73     self.iter=self.iter+1
74     if self.iter < 3:
75       # continue
76       nextSample = valIn + 1
77       self.pool.pushInSample(self.iter, nextSample)
78
79   def finish(self):
80     """Optional method called when the algorithm has finished, successfully
81        or not, to perform any necessary clean up."""
82     print("Algo finish")
83     self.pool.destroyAll()
84
85   def getAlgoResult(self):
86     """return the result of the algorithm.
87        The object returned is of type indicated by getTCForAlgoResult.
88     """
89     return 42
90
91