/
nelderMead_test.cpp
70 lines (60 loc) · 1.79 KB
/
nelderMead_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* nelderMead_test.cpp
*
* Created on: Aug 16, 2013
* Author: statek
*/
#include "nelderMead.h"
#include "functions.h"
#include<cppunit/TestFixture.h>
#include<cppunit/Test.h>
#include<cppunit/TestAssert.h>
#include<cppunit/TestCaller.h>
#include<cppunit/TestSuite.h>
using namespace CppUnit;
class NelderMeadTest: public CppUnit::TestFixture {
public:
void creationTest() {
SimpleQuadFunction sqf;
NelderMead nm(&sqf);
Point n;
CPPUNIT_ASSERT(nm.getIterations() == 0);
CPPUNIT_ASSERT(nm.getResult() == n);
CPPUNIT_ASSERT(nm.dim == sqf.dim);
CPPUNIT_ASSERT(nm.points == 0);
CPPUNIT_ASSERT(nm.idx.x1 == 0);
CPPUNIT_ASSERT(nm.idx.xn1 == 0);
CPPUNIT_ASSERT(nm.idx.xn == 0);
CPPUNIT_ASSERT(nm.params.alpha==ALPHA);
CPPUNIT_ASSERT(nm.params.beta==BETA);
CPPUNIT_ASSERT(nm.params.gamma==GAMMA);
CPPUNIT_ASSERT(nm.params.epsilon==EPSILON);
}
void calculateTest() {
SimpleQuadFunction sqf;
Function* f = &sqf;
NelderMead nm(f);
Point** p = new Point*[f->dim + 1];
for (int i = 0; i < (f->dim + 1); ++i) {
p[i] = new Point(f->dim);
p[i]->randomizeCoordinates();
}
nm.setPoints(p);
nm.run();
int iterations = nm.getIterations();
Point result = nm.getResult();
for (int i = 0; i < f->dim; ++i) {
CPPUNIT_ASSERT(nm.getResult().getCrd(i) <= f->optimum->getCrd(i) + nm.params.epsilon);
CPPUNIT_ASSERT(nm.getResult().getCrd(i) >= f->optimum->getCrd(i) - nm.params.epsilon);
}
for (int i = 0; i < (f->dim + 1); ++i) {
delete p[i];
}
}
static Test* suite() {
TestSuite* testsSuite = new TestSuite("NelderMeadTest");
testsSuite->addTest(new TestCaller<NelderMeadTest>("creationTest", &NelderMeadTest::creationTest));
testsSuite->addTest(new TestCaller<NelderMeadTest>("calculateTest", &NelderMeadTest::calculateTest));
return testsSuite;
}
};