]> Creatis software - bbtk.git/blob - packages/std/src/bbstdFindOptimalParam.cxx
#3278BBTK Feature New Normal - FindOptimalParam Box
[bbtk.git] / packages / std / src / bbstdFindOptimalParam.cxx
1 //===== 
2 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
3 //===== 
4 #include "bbstdFindOptimalParam.h"
5 #include "bbstdPackage.h"
6 namespace bbstd
7 {
8
9 BBTK_ADD_BLACK_BOX_TO_PACKAGE(std,FindOptimalParam)
10 BBTK_BLACK_BOX_IMPLEMENTATION(FindOptimalParam,bbtk::AtomicBlackBox);
11
12
13 std::string FindOptimalParam::Replace(std::string strCommand, double value)
14 {
15         std::string toReplace   = std::to_string( value );
16         int             pos                     = strCommand.find("XXvalueXX");
17         if (pos>=0)
18         {
19                 strCommand.replace(pos,9,toReplace);                    
20         } // if pos
21         return strCommand;
22 }
23
24 int FindOptimalParam::Evaluate( double value )
25 {
26         int result=-9999;
27         std::string strCommand = Replace( bbGetInputString(), value );
28         strCommand=strCommand+" NameFileTransfer="+bbGetInputTransferFile();
29         printf("EED FindOptimalParam::Evaluate  command=%s\n", strCommand.c_str());
30         system( strCommand.c_str() );
31         FILE *ff = fopen( bbGetInputTransferFile().c_str()  , "r");
32         if (ff!=NULL) 
33         {
34                 fscanf(ff,"%d",&result);
35         } else {
36                 printf("EED Warnning!!  FindOptimalParam::Evaluate  TransferFile not set.\n");
37         }// ff
38         fclose(ff);
39         return result;
40 }
41
42
43 //===== 
44 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
45 //===== 
46 void FindOptimalParam::Process()
47 {
48
49 // THE MAIN PROCESSING METHOD BODY
50 //   Here we simply set the input 'In' value to the output 'Out'
51 //   And print out the output value
52 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
53 //    void bbSet{Input|Output}NAME(const TYPE&)
54 //    const TYPE& bbGet{Input|Output}NAME() const 
55 //    Where :
56 //    * NAME is the name of the input/output
57 //      (the one provided in the attribute 'name' of the tag 'input')
58 //    * TYPE is the C++ type of the input/output
59 //      (the one provided in the attribute 'type' of the tag 'input')
60  //   bbSetOutputOut( bbGetInputIn() );
61  //   std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
62   
63         double          start   = bbGetInputInitStart();
64         double          end             = bbGetInputInitEnd();
65         double          actual;
66         int             iter    = 0;
67         int evalStart           = 1;
68         int evalEnd             = 0;
69         int evalActual;
70     do 
71         {
72                 iter++;
73                 actual          = (start+end)/2;
74                 evalActual      = Evaluate(actual);
75                 if (evalActual==1)  
76                 { 
77                         start= actual;
78                 } else if (evalActual==0) { 
79                         end     = actual;
80                 } else if (evalActual==-9999)  {
81                         printf("EED Warnning!  FindOptimalParam::Process   Transfer file Evaluation not coherent ..... \n");
82                 break;
83                 } else  {
84                         printf("EED Warnning!  FindOptimalParam::Process   Evaluation not coherent ..... \n");
85                 break;
86                 }
87                 if (iter==10000) 
88                 { 
89                         printf("EED Warnning!  FindOptimalParam::Process   Out limit iteration ..... \n");
90                 break;
91                 }
92                 printf("EED FindOptimalParam::Process  %f  \n",actual);
93         } while ( bbGetInputMinDifference() < (end-start)   );
94         bbSetOutputResult(start);
95 }
96 //===== 
97 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
98 //===== 
99 void FindOptimalParam::bbUserSetDefaultValues()
100 {
101
102 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
103 //    Here we initialize the input 'In' to 0
104    bbSetInputInitStart(0);
105    bbSetInputInitEnd(100);
106    bbSetInputString("");
107    bbSetInputMinDifference(1);
108   
109 }
110 //===== 
111 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
112 //===== 
113 void FindOptimalParam::bbUserInitializeProcessing()
114 {
115
116 //  THE INITIALIZATION METHOD BODY :
117 //    Here does nothing 
118 //    but this is where you should allocate the internal/output pointers 
119 //    if any 
120
121   
122 }
123 //===== 
124 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
125 //===== 
126 void FindOptimalParam::bbUserFinalizeProcessing()
127 {
128
129 //  THE FINALIZATION METHOD BODY :
130 //    Here does nothing 
131 //    but this is where you should desallocate the internal/output pointers 
132 //    if any
133   
134 }
135 }
136 // EO namespace bbstd
137
138