OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Match.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: Match.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.2 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class Match
10 // This class encapsulates all data for matching which do not depend
11 // on the matching method.
12 //
13 // ------------------------------------------------------------------------
14 //
15 // $Date: 2000/03/29 10:42:09 $
16 // $Author: opal $
17 //
18 // ------------------------------------------------------------------------
19 
20 #include "Match/Match.h"
23 #include "Algebra/Vector.h"
24 #include "Match/AbstractFun.h"
25 #include "Match/AbstractVar.h"
26 #include <iomanip>
27 #include <iostream>
28 
29 using std::cout;
30 
31 
32 // Class Match
33 // ------------------------------------------------------------------------
34 
35 Match *Match::block = 0;
36 
37 
38 Match::Match(): parser(), constraintCount(0), callCount(0), printLevel(1)
39 {}
40 
41 
43  while(! theVariables.empty()) {
44  delete theVariables.back();
45  theVariables.pop_back();
46  }
47 
48  while(! theFunctions.empty()) {
49  delete theFunctions.back();
50  theFunctions.pop_back();
51  }
52 }
53 
54 
56  theVariables.push_back(var);
57 }
58 
59 
60 void Match::deleteVariable(const std::string &name) {
61  for(VarList::iterator var = theVariables.begin();
62  var != theVariables.end();) { // for loop without increment
63  if((*var)->getName() == name) {
64  var = theVariables.erase(var); // new value: one after the erased element
65  } else {
66  ++var;
67  }
68  }
69 }
70 
71 
72 AbstractVar *Match::findVariable(const std::string &name) {
73  for(VarList::iterator var = theVariables.begin();
74  var != theVariables.end(); ++var) {
75  if((*var)->getName() == name) return *var;
76  }
77 
78  return 0;
79 }
80 
81 
83  x = Vector<double>(theVariables.size(), 0.0);
84  int j = 0;
85 
86  for(VarList::const_iterator var = theVariables.begin();
87  var != theVariables.end(); ++var) {
88  x[j++] = (*var)->getInternalValue();
89  }
90 }
91 
92 
94  int j = 0;
95 
96  for(VarList::iterator var = theVariables.begin();
97  var != theVariables.end(); ++var) {
98  (*var)->setInternalValue(x[j++]);
99  }
100 
101  // Force update.
103 }
104 
105 
107  return theVariables.size();
108 }
109 
110 
112  theFunctions.push_back(fun);
114 }
115 
116 
118  // Evaluate the matching functions.
119  callCount++;
120  setVariables(x);
121 
122  // Evaluate and copy the matching function values.
124  getFunctions(f);
125  return true;
126 }
127 
128 
131  int j = 0;
132 
133  for(FunList::const_iterator fun = theFunctions.begin();
134  fun != theFunctions.end(); ++fun) {
135  (*fun)->evaluate(f, j);
136  }
137 }
138 
139 
141  return constraintCount;
142 }
143 
144 
145 void Match::print(const char *method, MatchState state) {
146  static const char *str[] = {
147  "internal", // Internal call.
148  "progress", // New minimum found.
149  "start", // First call.
150  "restart", // Algorithm restarted.
151  "check", // Check phase.
152  "converged", // Match converged.
153  "failed", // Match failed.
154  "call limit", // Call limit exceeded.
155  "accuracy_limit", // Tolerance too small.
156  "terminated" // Terminated by user.
157  };
158 
159  // Test if printing desired at all.
160  if(( state == INTERNAL && printLevel <= 2 ) ||
161  ( state == PROGRESS && printLevel <= 1 )) return;
162 
163  // Print variable values.
164  cout << "\nMethod: " << method
165  << ", Matching state: " << str[state] << ", function calls = "
166  << callCount << '\n';
167 
168  cout << "\nCurrent variable values:\n";
169  for(VarList::const_iterator var = theVariables.begin();
170  var != theVariables.end(); ++var) {
171  (*var)->print(cout);
172  }
173 
174  // Print function values.
176  callCount++;
177  if(( state == INTERNAL && printLevel <= 3 ) ||
178  ( state == PROGRESS && printLevel <= 2 )) {
179  getFunctions(f);
180  } else {
181  cout << "\nCurrent matching conditions:\n";
183  int index = 0;
184  for(FunList::const_iterator fun = theFunctions.begin();
185  fun != theFunctions.end(); ++fun) {
186  (*fun)->evaluate(f, index);
187  (*fun)->print(cout);
188  }
189  }
190 
191  // The sum of squares.
192  double sum = 0.0;
193  for(Vector<double>::iterator i = f.begin(); i != f.end(); ++i) {
194  sum += (*i) * (*i);
195  }
196 
197  std::streamsize old_prec = cout.precision(12);
198  cout << "\nSum of squares = " << sum << std::endl;
199  cout.precision(old_prec);
200 }
201 
202 
203 int Match::getPrintLevel() const {
204  return printLevel;
205 }
206 
207 
208 void Match::setPrintLevel(int level) {
209  printLevel = level;
210 }
211 
212 
213 int Match::getCallCount() const {
214  return callCount;
215 }
void getFunctions(Vector< double > &f) const
Get cached values of matching functions.
Definition: Match.cpp:129
AbstractVar * findVariable(const std::string &name)
Find a matching variable.
Definition: Match.cpp:72
~Match()
Definition: Match.cpp:42
MatchState
The possible states of a matching process.
Definition: MatchState.h:29
void setPrintLevel(int)
Set the flag for printing.
Definition: Match.cpp:208
Match()
Constructor.
Definition: Match.cpp:38
iterator end()
Get end of data.
Definition: Array1D.h:210
Abstract base for matching constraints.
Definition: AbstractFun.h:32
VarList theVariables
Definition: Match.h:110
static Match * block
The block of match data.
Definition: Match.h:100
virtual int countConstraints() const =0
Get number of constrained values.
int countFunctions() const
Return total number of functions.
Definition: Match.cpp:140
void update()
Update all objects.
Definition: OpalData.cpp:723
T::PETE_Expr_t::PETE_Return_t sum(const PETE_Expr< T > &expr)
Definition: PETE.h:1213
void setVariables(const Vector< double > &x)
Set values of matching variables.
Definition: Match.cpp:93
void print(const char *method, MatchState state)
Print the results of minimisation.
Definition: Match.cpp:145
void addVariable(AbstractVar *)
Add a matching variable.
Definition: Match.cpp:55
static OpalData * getInstance()
Definition: OpalData.cpp:209
int countVariables() const
Get total number of variables.
Definition: Match.cpp:106
int callCount
Definition: Match.h:120
void deleteVariable(const std::string &name)
Delete a matching variable.
Definition: Match.cpp:60
iterator begin()
Get beginning of data.
Definition: Array1D.h:204
bool evaluate(const Vector< double > &x, Vector< double > &f)
Evaluate the matching functions.
Definition: Match.cpp:117
void addFunction(AbstractFun *)
Add a set of matching function(s).
Definition: Match.cpp:111
int constraintCount
Definition: Match.h:117
int printLevel
Definition: Match.h:123
int precision() const
Definition: Inform.h:115
Matching block.
Definition: Match.h:40
const std::string name
FunList theFunctions
Definition: Match.h:114
std::string::iterator iterator
Definition: MSLang.h:16
int getPrintLevel() const
Get the flag for printing.
Definition: Match.cpp:203
Abstract base for a matching variable.
Definition: AbstractVar.h:31
int getCallCount() const
Return count of function evaluations.
Definition: Match.cpp:213
void getVariables(Vector< double > &x) const
Get values of matching variables.
Definition: Match.cpp:82
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
void makeDirty(Object *object)
Invalidate expressions.
Definition: OpalData.cpp:629