Version: 9.16.0
simplex.cxx
Go to the documentation of this file.
1// Copyright (C) 2006-2026 CEA, EDF
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 "simplex.hxx"
21
22#include <iostream>
23
24#include "aleas.hxx"
25
26Simplex::Simplex(long sz, long nbgen, Maestro &maest)
27{
28 size = sz;
29 nbin = nbgen;
30 calc = &maest;
31 maxe = 40000;
32 nbeval = 0;
33}
34
36{
37 int i;
38
39 if (work.size() == size) {
40 for (i=0; i<size; i++)
41 delete work[i];
42 for (i=0; i<nbin; i++)
43 delete simplx[i];
44 }
45}
46
47void Simplex::setStop(long max)
48{
49 maxe = max;
50}
51
53{
54 long i;
55
56 work.resize(size);
57 for (i=0; i<size; i++) {
58 work[i] = new Point(nbin);
59 calc->put(i, *(work[i]->next()));
60 }
61 nbeval = maxe;
62}
63
64
66{
67 long id;
68 Solution *pt, *pnt;
69 std::vector<double> *next, *res;
70
71 res = calc->get(&id);
72 nbeval--;
73 pt = work[id]->inform(*res);
74 if (pt) {
75 pnt = add(pt);
76 if (pnt)
77 work[id]->mute(*pnt, *barycentre(), *minimum());
78 else
79 work[id]->reinit();
80 }
81 next = work[id]->next();
82 if (nbeval > size)
83 if (next)
84 calc->put(id, *next);
85 //else
86 // nbeval = size - 1;
87 return (nbeval > 0);
88}
89
91{
92 std::cout << maxe - nbeval << std::endl;
93 return;
94}
95
97{
98 return simplx[0];
99}
100
102{
103 int i;
104 Solution *ret, *swp;
105
106 if (simplx.size() < nbin) {
107 ret = (Solution *) NULL;
108 i = simplx.size();
109 simplx.push_back(sol);
110 while (i && simplx[i]->obj[0] < simplx[i-1]->obj[0]) {
111 // TODO swap interne
112 swp = simplx[i];
113 simplx[i] = simplx[i-1];
114 simplx[i-1] = swp;
115 i--;
116 }
117 }
118 else if (sol->obj[0] > simplx[nbin-1]->obj[0])
119 ret = sol;
120 else {
121 i = nbin -1;
122 ret = simplx[i];
123 simplx[i] = sol;
124 while (i && simplx[i]->obj[0] < simplx[i-1]->obj[0]) {
125 swp = simplx[i];
126 simplx[i] = simplx[i-1];
127 simplx[i-1] = swp;
128 i--;
129 }
130 }
131 return ret;
132}
133
134std::vector<double> *Simplex::minimum(void)
135{
136 return new std::vector<double>(*simplx[0]->param);
137}
138
139std::vector<double> *Simplex::barycentre(void)
140{
141 int i,j;
142 std::vector<double> *ret;
143
144 ret = new std::vector<double>(nbin);
145 for (i=0; i<nbin; i++) {
146 (*ret)[i] = 0.0;
147 for (j=0; j<nbin; j++)
148 (*ret)[i] += (*simplx[j]->param)[i];
149 (*ret)[i] /= nbin;
150 }
151 return ret;
152}
std::vector< double > * get(long *)
Definition: maestro.cxx:38
void put(long, std::vector< double > &)
Definition: maestro.cxx:29
Definition: point.hxx:28
long maxe
Definition: simplex.hxx:31
std::vector< double > * barycentre(void)
Definition: simplex.cxx:139
void start(void)
Definition: simplex.cxx:52
std::vector< Point * > work
Definition: simplex.hxx:33
int next(void)
Definition: simplex.cxx:65
Solution * add(Solution *)
Definition: simplex.cxx:101
Maestro * calc
Definition: simplex.hxx:34
long size
Definition: simplex.hxx:31
long nbeval
Definition: simplex.hxx:31
~Simplex(void)
Definition: simplex.cxx:35
void finish(void)
Definition: simplex.cxx:90
std::vector< double > * minimum(void)
Definition: simplex.cxx:134
long nbin
Definition: simplex.hxx:31
std::vector< Solution * > simplx
Definition: simplex.hxx:32
void setStop(long)
Definition: simplex.cxx:47
Simplex(long, long, Maestro &)
Definition: simplex.cxx:26
Solution * solution(void)
Definition: simplex.cxx:96
std::vector< double > * obj
Definition: solution.hxx:27