Xmipp  v3.23.11-Nereus
KeepBests.cpp
Go to the documentation of this file.
1 /*
2 
3 CONDOR 1.06 - COnstrained, Non-linear, Direct, parallel Optimization
4  using trust Region method for high-computing load,
5  noisy functions
6 Copyright (C) 2004 Frank Vanden Berghen
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation version 2
11 of the License.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 
22 If you want to include this tools in any commercial product,
23 you can contact the author at fvandenb@iridia.ulb.ac.be
24 
25 */
26 
27 #include "KeepBests.h"
28 #include <stdlib.h>
29 #include <string.h>
30 #include <iostream>
31 
32 KeepBests::KeepBests(int _n): n(_n), optionalN(0)
33 {
34  init();
35 }
36 
37 KeepBests::KeepBests(int _n, int _optionalN): n(_n), optionalN(_optionalN)
38 {
39  init();
40 }
41 
42 void KeepBests::init()
43 {
44  int i;
45  double *t;
46  ctable=(cell*)malloc(n*sizeof(cell));
47  if (optionalN) t=(double*)malloc(optionalN*n*sizeof(double));
48  if (NULL == t) {
49  std::cerr << "Error while allocating memory" << std::endl;
50  exit(-1);
51  }
52  for (i=0; i<n; i++)
53  {
54  if (optionalN)
55  {
56  ctable[i].optValue=t;
57  t+=optionalN;
58  }
59  ctable[i].K=INF;
60  ctable[i].prev=ctable+(i-1);
61  }
62  ctable[0].prev=NULL;
63  end=ctable+(n-1);
64  _local_getOptValueI=-1;
65 }
66 
67 void KeepBests::setOptionalN(int _optionalN)
68 {
69  int i;
70  double *t;
71  if (optionalN) t=(double*)realloc(ctable[0].optValue,_optionalN*n*sizeof(double));
72  else t=(double*)malloc(_optionalN*n*sizeof(double));
73  if (NULL == t) {
74  std::cerr << "Error while allocating memory" << std::endl;
75  exit(-1);
76  }
77  for (i=0; i<n; i++)
78  {
79  ctable[i].optValue=t;
80  t+=_optionalN;
81  }
82  optionalN=_optionalN;
83 }
84 
86 {
87  if (optionalN) free(ctable[0].optValue);
88  free(ctable);
89 }
90 
92 {
93  int i;
94  for (i=0; i<n; i++) ctable[i].K=INF;
95 // if (optionalN) memset(ctable[0].optValue,0,optionalN*n*sizeof(double));
96 }
97 
98 void KeepBests::add(double key, double value)
99 {
100  add(key,value,NULL,0);
101 }
102 void KeepBests::add(double key, double value, double optionalValue)
103 {
104  add(key,value,&optionalValue,1);
105 }
106 
107 void KeepBests::add(double key, double value, double *optionalValue)
108 {
109  add(key,value,optionalValue,optionalN);
110 }
111 
112 void KeepBests::add(double key, double value, double *optionalValue, int nn)
113 {
114  cell *t=end, *prev, *t_next=NULL;
115  while ((t)&&(t->K>key)) { t_next=t; t=t->prev; };
116  if (t_next)
117  {
118  if (t_next==end)
119  {
120  end->K=key;
121  end->value=value;
122  if ((optionalN)&&(optionalValue))
123  {
124  memcpy(end->optValue, optionalValue, nn*sizeof(double));
125  if (optionalN-nn>0)
126  memset(end->optValue+nn,0,(optionalN-nn)*sizeof(double));
127  }
128  } else
129  {
130  prev=end->prev;
131  end->prev=t;
132  t_next->prev=end;
133 
134  end->K=key;
135  end->value=value;
136  if ((optionalN)&&(optionalValue))
137  {
138  memcpy(end->optValue, optionalValue, nn*sizeof(double));
139  if (optionalN-nn)
140  memset(end->optValue+nn,0,(optionalN-nn)*sizeof(double));
141  }
142  end=prev;
143  };
144  };
145 }
146 
148 {
149  cell *t=end;
150  i=n-i-1;
151  while (i) { t=t->prev; i--; }
152  return t->value;
153 }
154 
155 double KeepBests::getKey(int i)
156 {
157  cell *t=end;
158  i=n-i-1;
159  while (i) { t=t->prev; i--; }
160  return t->K;
161 }
162 
163 double KeepBests::getOptValue(int i, int no)
164 {
165  if (i==_local_getOptValueI) return _local_getOptValueC->optValue[no];
166  _local_getOptValueI=i;
167  cell *t=end;
168  i=n-i-1;
169  while (i) { t=t->prev; i--; }
170  _local_getOptValueC=t;
171  return t->optValue[no];
172 }
173 
175 {
176  cell *t=end;
177  i=n-i-1;
178  while (i) { t=t->prev; i--; }
179  return t->optValue;
180 }
double getValue(int i)
Definition: KeepBests.cpp:147
double K
Definition: KeepBests.h:36
void add(double key, double value)
Definition: KeepBests.cpp:98
double getKey(int i)
Definition: KeepBests.cpp:155
#define i
void reset()
Definition: KeepBests.cpp:91
double * optValue
Definition: KeepBests.h:38
KeepBests(int n)
Definition: KeepBests.cpp:32
struct cell_tag * prev
Definition: KeepBests.h:39
double getOptValue(int i, int n)
Definition: KeepBests.cpp:163
free((char *) ob)
double value
Definition: KeepBests.h:37
void setOptionalN(int optinalN)
Definition: KeepBests.cpp:67
#define INF
Definition: svm.cpp:43
constexpr int K
int * n