]> Creatis software - bbtk.git/blob - packages/std/src/bbstdFindOptimalParam.cxx
2e863cee0c5551ce9daf5885ceafdb33d71c3def
[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         system( strCommand.c_str() );
29         FILE *ff = fopen( bbGetInputTransferFile().c_str()  , "r");
30         if (ff!=NULL) 
31         {
32                 fscanf(ff,"%d",&result);
33         } // ff
34         fclose(ff);
35         return result;
36 }
37
38
39 //===== 
40 // 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)
41 //===== 
42 void FindOptimalParam::Process()
43 {
44
45 // THE MAIN PROCESSING METHOD BODY
46 //   Here we simply set the input 'In' value to the output 'Out'
47 //   And print out the output value
48 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
49 //    void bbSet{Input|Output}NAME(const TYPE&)
50 //    const TYPE& bbGet{Input|Output}NAME() const 
51 //    Where :
52 //    * NAME is the name of the input/output
53 //      (the one provided in the attribute 'name' of the tag 'input')
54 //    * TYPE is the C++ type of the input/output
55 //      (the one provided in the attribute 'type' of the tag 'input')
56  //   bbSetOutputOut( bbGetInputIn() );
57  //   std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
58   
59         double          start   = bbGetInputInitStart();
60         double          end             = bbGetInputInitEnd();
61         double          actual;
62         int             iter=0;
63         int evalStart   = 1;
64         int evalEnd     = 0;
65         int evalActual;
66     do 
67         {
68                 iter++;
69                 actual          = (start+end)/2;
70                 evalActual      = Evaluate(actual);
71                 if (evalActual==1)  
72                 { 
73                         start= actual;
74                 } else if (evalActual==0) { 
75                         end     = actual;
76                 } else if (evalActual==-9999)  {
77                         printf("EED Warnning!  FindOptimalParam::Process   Transfer file Evaluation not coherent ..... \n");
78                 break;
79                 } else  {
80                         printf("EED Warnning!  FindOptimalParam::Process   Evaluation not coherent ..... \n");
81                 break;
82                 }
83                 if (iter==10000) 
84                 { 
85                         printf("EED Warnning!  FindOptimalParam::Process   Out limit iteration ..... \n");
86                 break;
87                 }
88                 printf("EED FindOptimalParam::Process  %f  \n",actual);
89         } while ( bbGetInputMinDifference() < (end-start)   );
90         bbSetOutputResult(start);
91 }
92 //===== 
93 // 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)
94 //===== 
95 void FindOptimalParam::bbUserSetDefaultValues()
96 {
97
98 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
99 //    Here we initialize the input 'In' to 0
100    bbSetInputInitStart(0);
101    bbSetInputInitEnd(100);
102    bbSetInputString("");
103    bbSetInputMinDifference(1);
104   
105 }
106 //===== 
107 // 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)
108 //===== 
109 void FindOptimalParam::bbUserInitializeProcessing()
110 {
111
112 //  THE INITIALIZATION METHOD BODY :
113 //    Here does nothing 
114 //    but this is where you should allocate the internal/output pointers 
115 //    if any 
116
117   
118 }
119 //===== 
120 // 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)
121 //===== 
122 void FindOptimalParam::bbUserFinalizeProcessing()
123 {
124
125 //  THE FINALIZATION METHOD BODY :
126 //    Here does nothing 
127 //    but this is where you should desallocate the internal/output pointers 
128 //    if any
129   
130 }
131 }
132 // EO namespace bbstd
133
134