File: minimizer.cpp

package info (click to toggle)
dmrgpp 6.06-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 113,900 kB
  • sloc: cpp: 80,986; perl: 14,772; ansic: 2,923; makefile: 83; sh: 17
file content (56 lines) | stat: -rw-r--r-- 1,119 bytes parent folder | download
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
#include "Minimizer.h"
#include "Vector.h"
// #include "Square.h"

template <typename T>
T square(const T& t1) { return t1 * t1; }

template <typename T>
std::ostream& operator<<(std::ostream& os,
    const typename PsimagLite::Vector<T>::Type& v)
{
	os << v.size() << "\n";
	for (SizeType i = 0; i < v.size(); i++)
		os << v[i] << " ";
	os << "\n";
	return os;
}

using namespace PsimagLite;
typedef double RealType;

class MyFunctionTest
{
public:

	typedef double FieldType;

	template <typename SomeVectorType>
	FieldType operator()(const SomeVectorType& v) const
	{
		return square(v[0] - 2) + square(v[1] - 3);
	}
	SizeType size() const { return 2; }
};

int main(int argc, char* argv[])
{
	SizeType n = 2;
	typename Vector<RealType>::Type x(n);

	// inital guess:
	for (SizeType i = 0; i < n; i++)
		x[i] = drand48();

	SizeType maxIter = 100;
	MyFunctionTest f;
	Minimizer<RealType, MyFunctionTest> min(f, maxIter);

	int iter = min.simplex(x, 1e-3, 1e-5);
	if (iter < 0) {
		std::cout << "No minimum found\n";
		return 1;
	}
	std::cout << "Minimum found after " << iter << " iterations.\n";
	std::cout << x;
}