Reputation: 33
I'm trying to implement some C++ code to find the maximum of a function using a simplex algoithm. Unfortunately, I have zero experience in C++. I'm running into this error and can't seem to find a solution from answers to similar questions.
simplex.cpp: In function ‘int main(int, char**)’:
simplex.cpp:22:25: error: no matching function for call to ‘simplex615::amoeba(arbitraryFunc&, double)’
simplex.amoeba(foo, 1e-7);
There is also warning related to the linked file "simplex615.h"
In file included from simplex.cpp:4:0:
simplex615.h:302:6: note: candidate: void simplex615::amoeba(optFunc&, double) [with F = arbitraryFunc]
void simplex615 ::amoeba(optFunc& foo, double tol) {
simplex615.h:302:6: note: no known conversion for argument 1 from ‘arbitraryFunc’ to ‘optFunc&
simplex.cpp
#include <vector>
#include <cmath>
#include <iostream>
#include "simplex615.h"
#define ZEPS 1e-10
// function object used as an argument
class arbitraryFunc {
public:
double operator() (std::vector<double>& x) {
// f(x0,x1) = 100*(x1-x0^2)^2 + (1-x0)^2
return 100*(x[1]-x[0]*x[0])*(x[1]-x[0]*x[0])+(1-x[0])*(1-x[0]);
}
};
int main(int main, char** argv) {
double point[2] = {-1.2, 1};
arbitraryFunc foo;
// initial point to start
// WILL BE DISCUSSED LATER
simplex615 <arbitraryFunc> simplex(point, 2); // create a simplex
simplex.amoeba(foo, 1e-7); // optimize for a function
// print outputs
std::cout << "Minimum = " << simplex.ymin() << ", at ("
<< simplex.xmin()[0] << ", " << simplex.xmin()[1]
<< ")" << std::endl;
return 0;
}
simplex615.h
template <class F> // F is a function object
void simplex615 <F>::amoeba(optFunc& foo, double tol) {
evaluateFunction(foo);
while(true) {
evaluateExtremes();
prepareUpdate();
if ( check_tol(Y[idxHi],Y[idxLo],tol) ) break;
updateSimplex(foo, -1.0); // reflection
if ( Y[idxHi] < Y[idxLo] ) {
updateSimplex(foo, -2.0); // expansion
}
else if ( Y[idxHi] >= Y[idxNextHi] ) {
if ( !updateSimplex(foo, 0.5) ) {
contractSimplex(foo);
}
}
}
}
simplex615.h
class optFunc {
public:
virtual double operator() (std::vector<double>& x) = 0;
};
Link to the complete files simplex.cpp and simplex.h: Source code
Any help will be appreciated. Thanks.
Upvotes: 2
Views: 58
Reputation: 1085
It seems to me that in your simplex615.h
you have forgotten to use 'class F' in amoeba
method. Just replace optFunc
with F
and it should fix the problem.
template <class F> // F is a function object
void simplex615 <F>::amoeba(F& foo, double tol) {
...
}
A template class argument in C++ defines a general type which can be replaced when using the template.
Also from this example, you can remove the declaration of optFunc from the header file.
Upvotes: 1