Version: 8.3.0
saemul.cxx
Go to the documentation of this file.
1 // Copyright (C) 2006-2016 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 #include "saemul.hxx"
21 
22 #include "saconst.h"
23 
24 #include "math.h"
25 
27 {
28  solver = new SalomeTest((Superviseur &) *this);
29 }
30 
32 {
33  delete solver;
34  destroyAll();
35 }
36 
38 {
39  std::pair<long,std::vector<double> *> pa;
40 
41  while (! q.empty()) {
42  pa = q.front();
43  q.pop();
44  delete pa.second;
45  }
46 }
47 
48 void Emulator::push(long id, std::vector<double> &cal)
49 {
50  std::pair<long, std::vector<double> *> *tmp;
51 
52  tmp = new std::pair<long, std::vector<double> *>(id, &cal);
53  q.push(*tmp);
54  delete tmp;
55 }
56 
58 {
59  std::vector<double> *cal;
60  std::pair<long,std::vector<double> *> pa;
61 
62  pa = q.front(); q.pop();
63  cal = pa.second;
64  out = eval(*cal);
65  delete cal;
66  return pa.first;
67 }
68 
69 std::vector<double> *Emulator::getCurrentOut(void)
70 {
71  return out;
72 }
73 
74 std::vector<double> *Emulator::eval(std::vector<double> &x)
75 {
76  std::vector<double> *res;
77  res = new std::vector<double>(1);
78 #ifdef TRIPOD
79  //Tripod
80  double x1, x2;
81 
82  x1 = x[0]; x2 = x[1];
83 
84  if (x2 < 0)
85  (*res)[0] = fabs(x1) + fabs(x2+50);
86  else {
87  (*res)[0] = fabs(x2-50);
88  if (x1 < 0)
89  (*res)[0] += 1 + fabs(x1+50);
90  else
91  (*res)[0] += 2 + fabs(x1-50);
92  }
93 #endif
94 #ifdef ROSENBROCK
95  // rosenbrock
96  double x1, x2;
97 
98  x1 = x[0] ; x2 = x1*x1 - x[1]; x1 -= 1;
99  (*res)[0] = x1*x1 + 100*x2*x2 ;
100 #endif
101 #ifdef GRIEWANK
102  double x1, x2, y;
103  int i;
104 
105  x1 = 0.0; x2 = 1.0;
106  for (i=0; i<NBGENE; i++) {
107  y = 3*x[i] - 100;
108  x1 += y*y;
109  x2 *= cos(y/sqrt(i+1));
110  }
111  (*res)[0] = x1 / 4000 - x2 + 1;
112 #endif
113  return res;
114 }
115 
116 void Emulator::run(void)
117 {
118  solver->readFromFile("caca");
119  solver->start();
120  while (! q.empty())
121  solver->next();
122  solver->finish();
123 }
124