File: maximizeGradient.cpp

package info (click to toggle)
mldemos 0.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 32,224 kB
  • ctags: 46,525
  • sloc: cpp: 306,887; ansic: 167,718; ml: 126; sh: 109; makefile: 2
file content (192 lines) | stat: -rw-r--r-- 5,408 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*********************************************************************
MLDemos: A User-Friendly visualization toolkit for machine learning
Copyright (C) 2010  Basilio Noris
Contact: mldemos@b4silio.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*********************************************************************/
#include "public.h"
#include "basicMath.h"
#include "maximizeGradient.h"
#include <QDebug>

using namespace std;

MaximizeGradient::MaximizeGradient()
{
	data = 0;
	dim = 2;
	maximum.resize(dim);
	FOR(d,dim) maximum[d] = rand()/(float)RAND_MAX;
	strength = 0.1;
	unmoving = 0;
	adaptive = true;
}

MaximizeGradient::~MaximizeGradient()
{
	KILL(data);
}

void MaximizeGradient::SetParams(float strength, bool adaptive)
{
	this->strength = strength;
	this->adaptive = adaptive;
}

void MaximizeGradient::Draw(QPainter &painter)
{
	painter.setPen(QPen(Qt::black, 1.5));
	painter.setBrush(Qt::NoBrush);
	FOR(i, visited.size())
	{
		QPointF point(visited[i][0]*w, visited[i][1]*h);
		painter.drawEllipse(point, 3, 3);
	}

	painter.setPen(QPen(Qt::black, 1.5));
	FOR(i, history.size()-1 )
	{
		QPointF point(history[i][0]*w, history[i][1]*h);
		QPointF pointNext(history[i+1][0]*w, history[i+1][1]*h);

		painter.setBrush(Qt::NoBrush);
		painter.drawLine(point, pointNext);
		painter.setBrush(Qt::white);
		painter.drawEllipse(point, 4, 4);
	}
	// we draw the current maximum
	QPointF point(history[history.size()-1][0]*w, history[history.size()-1][1]*h);
	painter.setBrush(QColor(255*(1-historyValue[history.size()-1]), 255, 255*(1-historyValue[history.size()-1])));
	painter.drawEllipse(point, 5, 5);
}

void MaximizeGradient::Train(float *dataMap, fVec size, fvec startingPoint)
{
	w = size.x;
	h = size.y;
	if(data) delete [] data;
	data = new float[w*h];
	memcpy(data, dataMap, w*h*sizeof(float));
	bConverged = false;
	if(!startingPoint.size())
	{
		startingPoint.resize(dim);
		FOR(d, dim) startingPoint[d] = drand48();
	}
	unmoving = 0;
	maximum = startingPoint;
	float value = GetValue(startingPoint);
	maximumValue = value;
	history.push_back(maximum);
	HistoryValue().push_back(value);
	evaluations = 0;
	//qDebug() << "Starting maximization at " << maximum[0] << " " << maximum[1];
}

fvec MaximizeGradient::Test( const fvec &sample)
{
	if(bConverged) return maximum;
	fvec newSample;

	newSample = sample;
	if(!sample.size()) newSample = maximum;

	int xIndex = newSample[0]*w;
	int yIndex = newSample[1]*h;

	float delta = 0.003;
	float value = GetValue(newSample);
	evaluations++;
	// we compute the values of the gradient in the 9 directions around
	float values[8];
	fVec v[8];
	int searchType = 1;
	switch(searchType)
	{
	case 2: // 8 directions
		values[0] = GetValue(newSample + fVec(-delta	,-delta));
		values[2] = GetValue(newSample + fVec(delta	,-delta));
		values[5] = GetValue(newSample + fVec(-delta	,+delta));
		values[7] = GetValue(newSample + fVec(delta	,+delta));
		v[0] = fVec(-1,-1)*(values[0]-value);
		v[2] = fVec( 1,-1)*(values[2]-value);
		v[5] = fVec(-1, 1)*(values[5]-value);
		v[7] = fVec( 1, 1)*(values[7]-value);
		evaluations += 4;
	case 1: // 4 directions
		values[1] = GetValue(newSample + fVec(0		,-delta));
		values[3] = GetValue(newSample + fVec(-delta	,0));
		v[1] = fVec( 0,-1)*(values[1]-value);
		v[3] = fVec(-1, 0)*(values[3]-value);
		evaluations += 2;
	case 0: // 2 directions
		values[4] = GetValue(newSample + fVec(delta	,0));
		values[6] = GetValue(newSample + fVec(0		,+delta));
		v[4] = fVec( 1, 0)*(values[4]-value);
		v[6] = fVec( 0, 1)*(values[6]-value);
		evaluations += 2;
	}

	fVec gradient;
	FOR(i, 8) gradient += v[i];
	gradient /= (float)(1<<(searchType+1));
	if(!adaptive) // we just use the strength as factor for moving
	{
		gradient.normalize();
		gradient *= strength*0.1;
	}
	else // we use the actual value of gradient to decide how much to move
	{
		gradient *= strength/delta*0.1;
	}

	fvec oldSample = newSample;
	newSample += gradient;

	if(oldSample[0]*w == newSample[0]*w && oldSample[1]*h == newSample[1]*h) // we're not moving anymore!
	{
		unmoving++;
		if(unmoving > 10)
		{
			bConverged = true;
			return newSample;
		}
	}
	else unmoving=0;

	visited.push_back(newSample);
	value = GetValue(newSample);
	if(value > maximumValue)
	{
		maximum = newSample;
		maximumValue = value;
		history.push_back(maximum);
		historyValue.push_back(value);
	}
	return newSample;
}

fvec MaximizeGradient::Test(const fVec &sample)
{
	return Test((fvec)sample);
}

const char *MaximizeGradient::GetInfoString()
{
	char *text = new char[1024];
	sprintf(text, "Gradient Ascent");
	return text;
}