-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriangles.cpp
More file actions
586 lines (475 loc) · 19.2 KB
/
triangles.cpp
File metadata and controls
586 lines (475 loc) · 19.2 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
#include "triangles.h"
#include "x11_undefs.h"
#include <QFileDialog>
#include <QDateTime>
#include <QThreadPool>
#include <QtConcurrent>
#include <QFuture>
#include <QMessageBox>
#include <QGraphicsPixmapItem>
#include "trianglescene.h"
#include "emberscene.h"
#include "faceweightedpixelsumfitness.h"
#include "randomiser.h"
Triangles::Triangles(QWidget *parent, Qt::WindowFlags flags)
: QDialog(parent, flags)
{
ui.setupUi(this);
ui.currentCandidate->setScene( new QGraphicsScene( this ) );
ui.bestCandidate->setScene( new QGraphicsScene( this ) );
ui.target->setScene( new QGraphicsScene( this ) );
ui.inputFrameGroup->setCurrentIndex( 0 );
m_running = false;
clear();
m_oclWrapper.CheckOpenCL();
connect( ui.reset, SIGNAL( clicked() ), this, SLOT( clear() ) );
connect( ui.start, SIGNAL( clicked() ), this, SLOT( run() ) );
connect( ui.stop, SIGNAL( clicked() ), this, SLOT( stop() ) );
connect( ui.selectTarget, SIGNAL( clicked() ), this, SLOT( selectTarget() ) );
connect( ui.useFlames, SIGNAL( toggled(bool) ), this, SLOT( setMethodFrame() ) );
connect( ui.useTriangles, SIGNAL( toggled(bool) ), this, SLOT( setMethodFrame() ) );
connect( ui.usePsfw, SIGNAL( toggled(bool) ), this, SLOT( setFitnessFrame() ) );
connect( ui.usePs, SIGNAL( toggled(bool) ), this, SLOT( setFitnessFrame() ) );
connect( ui.useSsim, SIGNAL( toggled(bool) ), this, SLOT( setFitnessFrame() ) );
foreach( std::string platform, m_oclWrapper.PlatformNames() )
ui.openclPlatform->addItem( QString::fromStdString( platform ) );
populateDeviceList();
QThreadPool::globalInstance()->setMaxThreadCount( 32 );
}
Triangles::~Triangles()
{
}
void Triangles::calculateFitnessForScene( const AbstractFitness *fitness, AbstractScene *scene )
{
QImage candidateImage( fitness->target() );
while ( ! scene->renderTo( candidateImage ) )
scene->randomise();
scene->setFitness( fitness->getFitness( candidateImage ) );
}
void Triangles::run()
{
enum scenetype { TRIANGLES, EMBERS };
scenetype scenetype = TRIANGLES;
if ( ui.useFlames->isChecked() )
scenetype = EMBERS;
if ( scenetype == EMBERS )
{
if ( ! EmberScene::initialiseRenderer( ui.palettesFile->text(), ui.openclPlatform->currentIndex(), ui.openclDevice->currentIndex() ) )
{
QMessageBox::critical( this, "Derp!", "Couldn't initialise opencl renderer" );
return;
}
}
// initialise the candates to m_target, to match format and size
m_bestCandidate = m_target;
m_currentCandidate = m_target;
// initialise everything
int populationSize = ui.poolSize->value();
m_currentFitness = -1;
m_bestFitness = -1;
float iterationsPerSec = 0;
QList< AbstractScene* > previousAge;
QList< AbstractScene* > nextAge;
QDir logDir( m_imageFilename + ".triangles" );
removeDir( m_imageFilename + ".triangles" );
logDir.mkdir( m_imageFilename + ".triangles" );
QFile bestScenesFile( logDir.absoluteFilePath( "bestScenes.log" ) );
bestScenesFile.open( QFile::WriteOnly | QFile::Truncate );
QDataStream bestScenes( &bestScenesFile );
int faceWeight = ui.faceWeight->value();
m_running = true;
AbstractFitness *fitness = new FaceWeightedPixelSumFitness( m_target, faceWeight);
int age = 0;
int culture = 0;
int maxCultures = 0;
updateDialog( 0, 0, 0, 0, 0, 0, 0, 0 );
AbstractScene *bestScene = 0;
if ( scenetype == TRIANGLES )
bestScene = new TriangleScene( 0, 0, 0, QColor() );
if ( scenetype == EMBERS )
bestScene = new EmberScene( 300, 300 );
int iterations = 0;
int maxIterations = 0;
quint64 acceptCount = 0;
int improvements = 0;
logDir.remove( logDir.absoluteFilePath( "age." + QString::number( age ) + ".log" ) );
// loop until the user tells us to stop
while( m_running )
{
// each age simulates a number of cultures over a set number of iterations.
// at the start of every new age, the best cultures from the previous age are selected and merged into a smaller number
// of cultures by placing the best scene from each into a new scene pool
// one culture simulates one attempt to find the best fitness from a given starting point over a certain number of iterations
// each culture has a scene pool of a certaion size. for each iteration, the scene pool is mutated and scenes are cross-bred
// with each other. the scenes with the best fitness survive to the next iteration.
// this loop runs once per culture. age-management variables persist across runs
// start by setting up the logs...
QFile cultureLogFile( logDir.absoluteFilePath( "culture." + QString::number( age ) + "." + QString::number( culture ) + ".log" ) );
cultureLogFile.open( QFile::WriteOnly | QFile::Truncate );
QDataStream cultureLog( &cultureLogFile );
QFile ageLogFile( logDir.absoluteFilePath( "age." + QString::number( age ) + ".log" ) );
ageLogFile.open( QFile::WriteOnly | QFile::Append );
QDataStream ageLog( &ageLogFile );
// our pool of scenes for the current culture
QList< AbstractScene* > pool;
// the maximum number of cultures for the given age
maxCultures = 0;
// run many more iterations for future ages, as we hit diminishing returns
maxIterations = ( ui.generationCount->value() * ( 1 << age ) );
// run more cultures in earlier ages, so we can throw away the items with the lowest fitness more swiftly
for( int i = 0; i < ui.maxAge->value() - age; ++ i )
{
if ( maxCultures == 0 )
maxCultures = populationSize;
else
maxCultures *= 4;
}
if ( age == ui.maxAge->value() )
maxIterations = 0;
// set up the current pool
m_currentFitness = -1;
if ( previousAge.isEmpty() )
{
// if there's no previous age, we're in the first age so initialise the pool with random values
for( int i = 0; i < populationSize; ++ i )
{
if ( scenetype == TRIANGLES )
pool.append( new TriangleScene( ui.triangleCount->value(), m_target.width(), m_target.height(), QColor( 255, 255, 255, 255 ) ) );
if ( scenetype == EMBERS )
pool.append( new EmberScene( m_target.width(), m_target.height() ) );
calculateFitnessForScene( fitness, pool[i] );
}
} else {
// randomly take scenes from theprevious age to populate this one
for( int i = 0; i < populationSize; ++ i )
{
pool.append( previousAge[ Randomiser::randomInt( previousAge.count() ) ]->clone() );
}
}
// calculate the current and best fitness for this age, based on the new culture
for( int i = 0; i < populationSize; ++ i )
{
if ( fitness->isBetterFitness( pool[i]->fitness(), m_currentFitness ) || m_currentFitness < 0 )
{
m_currentFitness = pool[i]->fitness();
pool[i]->renderTo( m_currentCandidate );
}
if ( fitness->isBetterFitness( pool[i]->fitness(), m_bestFitness ) || m_bestFitness < 0 )
{
m_bestFitness = pool[i]->fitness();
pool[i]->renderTo( m_bestCandidate );
}
}
// update the dialog with our starting variables
updateDialog( iterations, acceptCount, improvements, age, culture, maxCultures, maxIterations, iterationsPerSec );
iterations = 0;
acceptCount = 0;
improvements = 0;
QElapsedTimer timer;
timer.start();
// run the loop for the current culture (or indefinitely for the last culture)
while ( ( maxIterations == 0 || iterations < maxIterations ) && m_running )
{
// set up containers for the current generation, and the next
QSet< AbstractScene * > gen1( pool.toSet() );
QList< AbstractScene * > gen2;
QList< QFuture< void > > futures;
// take scenes at random from the pool, in pairs...
while( pool.count() )
{
AbstractScene *p1 = pool.takeAt( Randomiser::randomInt( pool.count() ) );
AbstractScene *p2 = pool.takeAt( Randomiser::randomInt( pool.count() ) );
// cross-breed and mutate the pair
QPair< AbstractScene*, AbstractScene* > children = p1->breed( p2, ui.mutationStrength->value() );
// run the fitness function for the newly-generated children using the thread pool
futures << QtConcurrent::run( calculateFitnessForScene, fitness, children.first );
futures << QtConcurrent::run( calculateFitnessForScene, fitness, children.second );
// add both parents and both clildren to the next generation's pool
gen2 << p1;
gen2 << p2;
gen2 << children.first;
gen2 << children.second;
}
// wait for the fitness functions from this generation to complete
while( futures.count() )
{
while( futures.last().isRunning() )
futures.last().waitForFinished();
futures.takeLast();
}
// sort the next generation by fitness
qSort( gen2.begin(), gen2.end(), fitness->sceneHasBetterFitnessMethod() );
// if the next generation has a better fitness than the current best fitness, update the candidate data
if ( fitness->isBetterFitness( gen2.first()->fitness(), m_currentFitness ) )
{
++ improvements;
m_currentFitness = gen2.first()->fitness();
if ( scenetype != EMBERS )
gen2.first()->saveToStream( cultureLog );
gen2.first()->renderTo( m_currentCandidate );
if ( fitness->isBetterFitness( m_currentFitness, m_bestFitness ) )
{
m_bestFitness = m_currentFitness;
delete bestScene;
bestScene = gen2.first()->clone();
bestScenes << iterations;
bestScenes << m_currentFitness;
if ( scenetype != EMBERS )
bestScene->saveToStream( bestScenes );
m_bestCandidate = m_currentCandidate;
}
}
// populate the next pool
for( int i = 0; i < populationSize; ++ i )
{
if ( i == 0 )
{
// always include the best candidate
pool.append( gen2.takeFirst() );
} else {
// take other candidates at random from the best n results (where n is the tournament size) to keep the gene pool more varied
int selection = Randomiser::randomInt( ui.tournamentSize->value() );
AbstractScene *s = gen2.takeAt( selection );
if ( ! gen1.contains( s ) )
{
++ acceptCount;
}
pool.append( s );
}
}
// clear the next pool and sort the current data, ready for another iteration
qDeleteAll( gen2 );
qSort( pool.begin(), pool.end(), fitness->sceneHasBetterFitnessMethod() );
++ iterations;
iterationsPerSec = static_cast< float > ( iterations ) / static_cast< float > ( timer.elapsed() / 1000 );
// update the window if we've covered enough iterations
if ( iterations % ui.updateFrequency->value() == 0 )
{
updateDialog( iterations, acceptCount, improvements, age, culture, maxCultures, maxIterations, iterationsPerSec );
}
}
// we've completed all the iterations for the culture...
// write the best candidate to the age log, and place into the next age
if ( scenetype != EMBERS )
pool.first()->saveToStream( ageLog );
nextAge.append( pool.takeFirst() );
// clear the pool and advance to the next culture
qDeleteAll( pool );
++ culture;
// if we've been through all the cultures, advance to the next age
if ( culture == maxCultures )
{
qDeleteAll( previousAge );
previousAge = nextAge;
nextAge.clear();
culture = 0;
++ age;
logDir.remove( logDir.absoluteFilePath( "age." + QString::number( age ) + ".log" ) );
}
updateDialog( iterations, acceptCount, improvements, age, culture, maxCultures, maxIterations, iterationsPerSec );
}
// the user has told us to stop...
// update the screen
updateDialog( iterations, acceptCount, improvements, age, culture, maxCultures, maxIterations, iterationsPerSec );
// delete everyhing that's left
qDeleteAll( nextAge );
qDeleteAll( previousAge );
// save the best scene to an svg
bestScene->saveToFile( logDir.absoluteFilePath( "bestPicture.svg" ) );
// iterate over the best scenes file, so that we can see the history of all improvements
bestScenesFile.close();
bestScenesFile.open( QFile::ReadOnly );
QDataStream bestScenesLoader( &bestScenesFile );
logDir.mkdir( "bestScenes" );
QDir bsDir( logDir.absoluteFilePath( "bestScenes" ) );
int count = 0;
while( ! bestScenesLoader.atEnd() )
{
int iteration;
float fitness;
bestScenesLoader >> iteration;
bestScenesLoader >> fitness;
QString bsFilePath = bsDir.absoluteFilePath( QString( "%1.%2.svg" ).arg( count, 7, 10, QLatin1Char( '0' ) ).arg( iteration ) );
AbstractScene *s = 0;
if ( scenetype == TRIANGLES )
s = new TriangleScene( ui.triangleCount->value(), m_target.width(), m_target.height(), QColor( 255, 255, 255, 255 ) );
if ( scenetype == EMBERS )
s = new EmberScene( m_target.width(), m_target.height() );
if ( scenetype != EMBERS )
{
s->loadFromStream( bestScenesLoader );
s->saveToFile( bsFilePath );
}
delete s;
++ count;
}
delete bestScene;
// write out the best svgs for each individual culture
QStringList filters;
filters.append( "age.*.log" );
//filters.append( "culture.*.log" );
foreach( QString logEntry, logDir.entryList( filters ) )
{
logDir.mkdir( logEntry + ".d" );
QDir entryDir( logDir.absoluteFilePath( logEntry + ".d" ) );
QFile lf( logDir.absoluteFilePath( logEntry ) );
lf.open( QFile::ReadOnly );
QDataStream d( &lf );
count = 0;
while( !d.atEnd() )
{
QString fp = entryDir.absoluteFilePath( QString( "%1.svg" ).arg( count, 7, 10, QLatin1Char( '0' ) ) );
AbstractScene *s = 0;
if ( scenetype == TRIANGLES )
s = new TriangleScene( ui.triangleCount->value(), m_target.width(), m_target.height(), QColor( 255, 255, 255, 255 ) );
if ( scenetype == EMBERS )
s = new EmberScene( m_target.width(), m_target.height() );
s->loadFromStream( d );
s->saveToFile( fp );
delete s;
++ count;
}
}
if ( scenetype == EMBERS )
EmberScene::destroyRenderer();
}
void Triangles::updateDialog( int iterations, quint64 acceptCount, int improvements, int age, int culture, int maxCultures, int maxIterations, float iterationsPerSec )
{
ui.iteration->setText( QString::number( iterations ) + "/" + QString::number( maxIterations ) );
ui.acceptCount->setText( QString::number( acceptCount ) );
ui.improvements->setText( QString::number( improvements ) );
ui.age->setText( QString::number( age ) );
ui.culture->setText( QString::number( culture ) + "/" + QString::number( maxCultures ) );
ui.bestFitness->setText( QString::number( m_bestFitness ) );
ui.currentFitness->setText( QString::number( m_currentFitness ) );
ui.iterationsPerSec->setText( QString::number( iterationsPerSec) );
updateCandidateView();
qApp->processEvents();
}
void Triangles::stop()
{
m_running = false;
}
void Triangles::clear()
{
ui.iteration->setText( "0" );
ui.triangleCount->setValue( 20 );
ui.poolSize->setValue( 10 );
ui.tournamentSize->setValue( 2 );
ui.mutationStrength->setValue( 0 );
ui.generationCount->setValue( 10000 );
ui.faceWeight->setValue( 10 );
ui.maxAge->setValue( 1 );
ui.updateFrequency->setValue( 1 );
ui.age->setText( "0" );
ui.culture->setText( "0" );
ui.currentFitness->setText( "0" );
ui.bestFitness->setText( "0" );
ui.acceptCount->setText( "0" );
ui.improvements->setText( "0" );
m_bestCandidate = QImage();
m_currentCandidate = QImage();
m_target = QImage();
m_currentFitness = 0;
m_bestFitness = 0;
ui.target->scene()->clear();
ui.bestCandidate->scene()->clear();
ui.currentCandidate->scene()->clear();
}
void Triangles::selectTarget()
{
QString imageFile( QFileDialog::getOpenFileName( this, "Select Image" ) );
if ( imageFile.length() )
{
m_target.load( imageFile );
ui.target->scene()->clear();
if ( ! m_target.isNull() )
{
m_target = m_target.convertToFormat( QImage::Format_RGB32 );
QGraphicsPixmapItem *item = ui.target->scene()->addPixmap( QPixmap::fromImage( m_target ) );
ui.target->fitInView( item, Qt::KeepAspectRatio );
m_imageFilename = imageFile;
}
}
}
void Triangles::resizeEvent( QResizeEvent *e )
{
Q_UNUSED( e );
QList< QGraphicsItem * > items( ui.target->scene()->items() );
if ( items.count() == 1 )
ui.target->fitInView( items.first(), Qt::KeepAspectRatio );
items = ui.currentCandidate->scene()->items();
if ( items.count() == 1 )
ui.currentCandidate->fitInView( items.first(), Qt::KeepAspectRatio );
items = ui.bestCandidate->scene()->items();
if ( items.count() == 1 )
ui.bestCandidate->fitInView( items.first(), Qt::KeepAspectRatio );
}
void Triangles::updateCandidateView()
{
QGraphicsPixmapItem *item = 0;
if ( ui.bestCandidate->scene()->items().count() == 0 )
item = ui.bestCandidate->scene()->addPixmap( QPixmap::fromImage( m_bestCandidate ) );
else
{
item = dynamic_cast< QGraphicsPixmapItem * > ( ui.bestCandidate->scene()->items().first() );
if ( item )
{
item->setPixmap( QPixmap::fromImage( m_bestCandidate ) );
}
}
ui.bestCandidate->fitInView( item, Qt::KeepAspectRatio );
if ( ui.currentCandidate->scene()->items().count() == 0 )
item = ui.currentCandidate->scene()->addPixmap( QPixmap::fromImage( m_currentCandidate ) );
else
{
item = dynamic_cast< QGraphicsPixmapItem * > ( ui.currentCandidate->scene()->items().first() );
if ( item )
{
item->setPixmap( QPixmap::fromImage( m_currentCandidate ) );
}
}
ui.currentCandidate->fitInView( item, Qt::KeepAspectRatio );
}
bool Triangles::removeDir(const QString &dirName)
{
bool result = true;
QDir dir(dirName);
if (dir.exists(dirName)) {
Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) {
if (info.isDir()) {
result = removeDir(info.absoluteFilePath());
}
else {
result = QFile::remove(info.absoluteFilePath());
}
if (!result) {
return result;
}
}
result = dir.rmdir(dirName);
}
return result;
}
void Triangles::setMethodFrame()
{
if (ui.useTriangles->isChecked())
ui.inputFrameGroup->setCurrentIndex(0);
else
ui.inputFrameGroup->setCurrentIndex(1);
}
void Triangles::setFitnessFrame()
{
if (ui.usePsfw->isChecked())
ui.fitnessFrameGroup->setCurrentIndex(0);
if (ui.usePs->isChecked())
ui.fitnessFrameGroup->setCurrentIndex(1);
if (ui.useSsim->isChecked())
ui.fitnessFrameGroup->setCurrentIndex(2);
}
void Triangles::populateDeviceList()
{
ui.openclDevice->clear();
foreach( std::string device, m_oclWrapper.DeviceNames( ui.openclPlatform->currentIndex() ) )
ui.openclDevice->addItem( QString::fromStdString( device ) );
}