File: dialog_functions.cpp

package info (click to toggle)
evolvotron 0.6.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,420 kB
  • ctags: 1,367
  • sloc: cpp: 10,462; python: 162; sh: 147; makefile: 9
file content (308 lines) | stat: -rw-r--r-- 11,621 bytes parent folder | download | duplicates (2)
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
/**************************************************************************/
/*  Copyright 2012 Tim Day                                                */
/*                                                                        */
/*  This file is part of Evolvotron                                       */
/*                                                                        */
/*  Evolvotron is free software: you can redistribute it and/or modify    */
/*  it under the terms of the GNU General Public License as published by  */
/*  the Free Software Foundation, either version 3 of the License, or     */
/*  (at your option) any later version.                                   */
/*                                                                        */
/*  Evolvotron 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 General Public License for more details.                          */
/*                                                                        */
/*  You should have received a copy of the GNU General Public License     */
/*  along with Evolvotron.  If not, see <http://www.gnu.org/licenses/>.   */
/**************************************************************************/

/*! \file
  \brief Implementation of class DialogFunctions.
*/

#include "libevolvotron_precompiled.h"

#include "dialog_functions.h"

#include "evolvotron_main.h"
#include "function_registry.h"

/*! Dialog controls function weightings and related parameters.
 */
DialogFunctions::DialogFunctions(EvolvotronMain* parent,MutationParametersQObject* mp)
  :QDialog(parent)
  ,_parent(parent)
  ,_mutation_parameters(mp)
{
  assert(_parent!=0);

  setWindowTitle("Functions");
  setMinimumSize(480,360);     // Have to scroll through tabs if don't set this
  setSizeGripEnabled(true);

  setLayout(new QVBoxLayout);

  QTabWidget*const tabs=new QTabWidget;
  layout()->addWidget(tabs);

  QPushButton*const ok=new QPushButton("OK");
  layout()->addWidget(ok);
  ok->setDefault(true);

  for (int c=-1;c<FnClassifications;c++)
    {
      QWidget*const tab_content=new QWidget;
      QVBoxLayout*const tab_content_layout=new QVBoxLayout;
      tab_content->setLayout(tab_content_layout);
      tabs->addTab(tab_content,(c==-1 ? "All" : function_classification_name[c]));

      QWidget*const buttons=new QWidget;
      buttons->setLayout(new QHBoxLayout);
      tab_content->layout()->addWidget(buttons);
      QPushButton*const button_less=new QPushButton("Less");
      QPushButton*const button_rand=new QPushButton("Randomize");
      QPushButton*const button_more=new QPushButton("More");
      buttons->layout()->addWidget(button_less);
      buttons->layout()->addWidget(button_rand);
      buttons->layout()->addWidget(button_more);

      SignalExpanderClickedUint*const bx_rand=new SignalExpanderClickedUint(this,(c==-1 ? 0xffffffff : (1<<c)));

      connect(button_rand,SIGNAL(clicked()),bx_rand,SLOT(clicked()));
      connect(bx_rand,SIGNAL(clicked(uint)),this,SLOT(clicked_button_rand(uint)));

      QScrollArea*const scrollarea=new QScrollArea;
      scrollarea->setWidgetResizable(true);
      tab_content->layout()->addWidget(scrollarea);
      tab_content_layout->setStretchFactor(scrollarea,1);
      
      QWidget*const scrollcontent=new QWidget;
      scrollcontent->setLayout(new QVBoxLayout);
      scrollarea->setWidget(scrollcontent);

      for (FunctionRegistry::Registrations::const_iterator it=_parent->mutation_parameters().function_registry().registrations().begin();
	   it!=_parent->mutation_parameters().function_registry().registrations().end();
	   it++)
	{
	  const FunctionRegistration& fn=
#if BOOST_VERSION >= 103400
	    *(it->second);
#else
	    *it;
#endif
	  if (c==-1 || fn.classification()&(1<<c))
	    {
	      QGroupBox*const g=new QGroupBox(fn.name().c_str());
	      scrollarea->widget()->layout()->addWidget(g);
	      g->setLayout(new QHBoxLayout);
	      
	      QSizePolicy spx(QSizePolicy::Expanding,QSizePolicy::Preferred);
	      g->setSizePolicy(spx);
	      
	      g->layout()->addWidget(new QLabel("2^-10"));
	      QSlider*const s=new QSlider(Qt::Horizontal);
	      g->layout()->addWidget(s);
	      s->setMinimum(-10);
	      s->setMaximum(0);
	      s->setValue(0);
	      s->setTickInterval(1);
	      s->setTickPosition(QSlider::TicksBothSides);
	      s->setSizePolicy(spx);
	      g->layout()->addWidget(new QLabel("1"));
	      
	      _slider_to_function.insert(std::make_pair(s,&fn));

	      SignalExpanderValueChangedQSlider*const sx=new SignalExpanderValueChangedQSlider(this,s);
	      connect(
		      s,SIGNAL(valueChanged(int)),
		      sx,SLOT(valueChanged(int))
		      );
	      connect(
		      sx,SIGNAL(valueChanged(QSlider*,int)),
		      this,SLOT(changed_function_weighting(QSlider*,int))
		      );

	      // Wire up buttons
	      connect(
		      button_less,SIGNAL(clicked()),
		      s,SLOT(subtractStep())
		      );
	      connect(button_more,SIGNAL(clicked()),
		      s,SLOT(addStep())
		      );
	    }
	}
    }

  // And add another tab for all the branching-ratio/dilution controls
  {
    QWidget*const vbox=new QWidget;
    vbox->setLayout(new QVBoxLayout);
    tabs->addTab(vbox,"Dilution");
    
    _branching_ratio=new QLabel;
    vbox->layout()->addWidget(_branching_ratio);
    
    QGroupBox*const c0=new QGroupBox("Required branching ratio after dilution");
    c0->setLayout(new QHBoxLayout);
    vbox->layout()->addWidget(c0);

    c0->layout()->addWidget(new QLabel("0.1"));
    _slider_target_branching_ratio=new QSlider(Qt::Horizontal);
    c0->layout()->addWidget(_slider_target_branching_ratio);
    _slider_target_branching_ratio->setMinimum(10);
    _slider_target_branching_ratio->setMaximum(90);
    _slider_target_branching_ratio->setValue(50);
    _slider_target_branching_ratio->setTickInterval(10);
    _slider_target_branching_ratio->setTickPosition(QSlider::TicksBothSides);
    _slider_target_branching_ratio->setToolTip("The branching ratio must be diluted to <1.0 to prevent formation of infinitely large function-trees.\nWarning: setting a high value results in complex function trees taking a long time to compute.\nSetting a low value results in very simple images.");
    c0->layout()->addWidget(new QLabel("0.9"));

    QGroupBox*const c1=new QGroupBox("Of diluting nodes, proportion constant:");
    c1->setLayout(new QHBoxLayout);
    vbox->layout()->addWidget(c1);
    c1->layout()->addWidget(new QLabel("0.0"));
    _slider_proportion_constant=new QSlider(Qt::Horizontal,c1);
    c1->layout()->addWidget(_slider_proportion_constant);
    _slider_proportion_constant->setMinimum(0);
    _slider_proportion_constant->setMaximum(100);
    _slider_proportion_constant->setValue(50);
    _slider_proportion_constant->setTickInterval(10);
    _slider_proportion_constant->setTickPosition(QSlider::TicksBothSides);
    _slider_proportion_constant->setToolTip("This specifies the proportion of diluting nodes which will be constant.");
    c1->layout()->addWidget(new QLabel("1.0"));
    
    QGroupBox*const c2=new QGroupBox("Of non-constant diluting nodes, proportion transforms");
    c2->setLayout(new QHBoxLayout);
    vbox->layout()->addWidget(c2);
    c2->layout()->addWidget(new QLabel("0.0"));
    _slider_identity_supression=new QSlider(Qt::Horizontal,c2);
    c2->layout()->addWidget(_slider_identity_supression);
    _slider_identity_supression->setMinimum(0);
    _slider_identity_supression->setMaximum(100);
    _slider_identity_supression->setValue(50);
    _slider_identity_supression->setTickInterval(10);
    _slider_identity_supression->setTickPosition(QSlider::TicksBothSides);
    _slider_identity_supression->setToolTip("This specifies the proportion of non-constant diluting nodes which will be transforms (c.f identity nodes).");
    c2->layout()->addWidget(new QLabel("1.0"));

  connect(
	  _slider_target_branching_ratio,SIGNAL(valueChanged(int)),
	  this,SLOT(changed_target_branching_ratio(int))
	  );

  connect(
          ok,SIGNAL(clicked()),
          this,SLOT(hide())
          );

  connect(
	  _slider_proportion_constant,SIGNAL(valueChanged(int)),
	  this,SLOT(changed_proportion_constant(int))
	  );

  connect(
	  _slider_identity_supression,SIGNAL(valueChanged(int)),
	  this,SLOT(changed_identity_supression(int))
	  );
  }

  setup_from_mutation_parameters();

  connect(
	  _mutation_parameters,SIGNAL(changed()),
	  this,SLOT(mutation_parameters_changed())
	  );
}

DialogFunctions::~DialogFunctions()
{}

void DialogFunctions::setup_from_mutation_parameters()
{
  const real b=_mutation_parameters->random_function_branching_ratio();
  const real s=_mutation_parameters->proportion_basic();
  const real br=(1.0-s)*b;

  std::stringstream msg;
  msg 
    << "Undiluted random function branching ratio is " << b << "\n"
    << "Diluting in proportion " << s << " to obtain required branching ratio.";
  _branching_ratio->setText(msg.str().c_str());

  _slider_target_branching_ratio->setValue(static_cast<int>(100.0*br+0.5));
  _slider_proportion_constant->setValue(static_cast<int>(0.5+100.0*_mutation_parameters->proportion_constant()));
  _slider_identity_supression->setValue(static_cast<int>(0.5+100.0*_mutation_parameters->identity_supression()));

  for (std::map<QSlider*,const FunctionRegistration*>::const_iterator it=_slider_to_function.begin();
       it!=_slider_to_function.end();
       it++
       )
    {
      const real w=_mutation_parameters->get_weighting((*it).second);
      const int iw=static_cast<int>(floor(0.5+(log(w)*(1.0/M_LN2))));
      if (iw!=(*it).first->value())
	{
	  (*it).first->setValue(iw);
	}
    }
}

void DialogFunctions::changed_target_branching_ratio(int v)
{
  const real target_branching_ratio=v/100.0;
  
  // Want to solve tgt=proportion_basic*0.0+(1.0-proportion_basic)*random_function_branching_ratio
  const real proportion_basic=1.0-target_branching_ratio/_mutation_parameters->random_function_branching_ratio();

  std::clog 
    << "Basic-node dilution proportion set to " 
    << proportion_basic 
    << " giving a branching ratio of "
    << (1.0-proportion_basic)*_mutation_parameters->random_function_branching_ratio()
    << "\n";

  _mutation_parameters->proportion_basic(proportion_basic);

  setup_from_mutation_parameters();
}

void DialogFunctions::changed_proportion_constant(int v)
{
  _mutation_parameters->proportion_constant(v/100.0);
}

void DialogFunctions::changed_identity_supression(int v)
{
  _mutation_parameters->identity_supression(v/100.0);
}

void DialogFunctions::changed_function_weighting(QSlider* s,int v)
{
  std::map<QSlider*,const FunctionRegistration*>::const_iterator it=_slider_to_function.find(s);
  if (it==_slider_to_function.end())
    {
      std::clog << "DialogFunctions::changed_function_weighting : unknown source slider, ignoring\n";
    }
  else
    {
      const FunctionRegistration* fn=(*it).second;
      
      const real w=pow(2,v);
      std::clog << fn->name() << " weighting changed to " << w << "\n";
      _mutation_parameters->change_function_weighting(fn,w);
    }
}

void DialogFunctions::clicked_button_rand(uint mask)
{
  std::clog << "Randomizing functions matching mask " << mask << "\n";
  _mutation_parameters->randomize_function_weightings_for_classifications(mask);
}

void DialogFunctions::mutation_parameters_changed()
{
  //std::clog << "[DialogFunctions::mutation_parameters_changed()]\n";
  setup_from_mutation_parameters();  
}