Salome HOME
updated copyright message
[modules/yacs.git] / src / yacsloader_swig / Test / optim_plugin.py
1 # Copyright (C) 2015-2023  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 myalgosync(SALOMERuntime.OptimizerAlgSync):
23   def __init__(self):
24     SALOMERuntime.OptimizerAlgSync.__init__(self, None)
25     r=SALOMERuntime.getSALOMERuntime()
26     self.tin=r.getTypeCode("int")
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 the internal node """
37     return self.tin
38
39   def getTCForOut(self):
40     """return typecode of type expected as Output 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 start(self):
58     """Start to fill the pool with samples to evaluate."""
59     print("Algo start ")
60     self.iter=0
61     # pushInSample(id, value)
62     self.pool.pushInSample(self.iter, 1)
63
64   def takeDecision(self):
65     """ This method is called each time a sample has been evaluated. It can
66         either add new samples to evaluate in the pool, do nothing (wait for
67         more samples), or empty the pool to finish the evaluation.
68     """
69     currentId=self.pool.getCurrentId()
70     valIn = self.pool.getCurrentInSample().getIntValue()
71     valOut = self.pool.getCurrentOutSample().getIntValue()
72     print("Algo takeDecision currentId=%s, valIn=%s, valOut=%s" % (currentId, valIn, valOut))
73
74     self.iter=self.iter+1
75     if self.iter < 3:
76       # continue
77       nextSample = valIn + 1
78       self.pool.pushInSample(self.iter, nextSample)
79
80   def finish(self):
81     """Optional method called when the algorithm has finished, successfully
82        or not, to perform any necessary clean up."""
83     print("Algo finish")
84     self.pool.destroyAll()
85
86   def getAlgoResult(self):
87     """return the result of the algorithm.
88        The object returned is of type indicated by getTCForAlgoResult.
89     """
90     return 42
91
92