24 #include "displaycontroller.h"
28 #include <QFileDialog>
29 #include <QMessageBox>
39 #pragma warning(disable:4996)
42 #define MAXFREEP 10000
46 NetworkDialog::NetworkDialog(Evonet* net, QWidget* parent, Qt::WindowFlags flags) : QWidget( parent, flags )
59 m_mainLayout =
new QVBoxLayout(
this);
64 m_toolBar =
new QToolBar(
this);
65 m_mainLayout->addWidget(m_toolBar);
68 this->enet->neuronlesions = 0;
72 rendNetwork =
new RendNetwork(
this );
73 m_mainLayout->addWidget( rendNetwork, 4 );
75 mixerDialog =
new MixerDialog( enet,
this );
76 m_mainLayout->addWidget( mixerDialog );
79 statusb =
new QLabel(
"",
this);
80 m_mainLayout->addWidget(statusb);
82 connect(rendNetwork, SIGNAL(selectedneuronsChanged()),
this, SLOT(updatetoolb()) );
86 NetworkDialog::~NetworkDialog()
122 void NetworkDialog::error(
const char *emessage)
124 QMessageBox::about(
this,
"ERROR", emessage);
127 void NetworkDialog::warning(
const char *emessage)
129 QMessageBox::about(
this,
"WARNING", emessage);
133 void NetworkDialog::createToolBars()
139 m_toolBar->addAction(openAct);
140 m_toolBar->addAction(saveAct);
142 cblockAct =
new QComboBox( m_toolBar );
143 cblockAct->addItems(QStringList() <<
"blocks list");
144 for (b=0; b < enet->net_nblocks; b++)
146 block_name(b, stblock);
147 cblockAct->addItems(QStringList() << stblock);
149 connect( cblockAct, SIGNAL(currentIndexChanged(
int)),
this, SLOT(selblock_changed(
int)) );
150 connect(
this, SIGNAL(selectedblockChanged()),
this, SLOT(updateblocktype()) );
152 m_toolBar->addWidget( cblockAct );
154 blocktypeAct =
new QComboBox( m_toolBar );
155 blocktypeAct->addItems(QStringList() <<
"block type");
156 blocktypeAct->addItems(QStringList() <<
"genetic" <<
"fixed" <<
"back-prop" );
157 connect(blocktypeAct, SIGNAL(currentIndexChanged(
int)),
this, SLOT(changeblocktype(
int)) );
158 m_toolBar->addWidget( blocktypeAct );
160 neurontypeAct =
new QComboBox( m_toolBar );
161 neurontypeAct->addItems( QStringList() <<
"neuron type");
162 neurontypeAct->addItems( QStringList() <<
"sigmoid" <<
"integrator" <<
"binary" <<
"sigmoid flat" );
163 connect( neurontypeAct, SIGNAL(currentIndexChanged(
int)),
this, SLOT(set_neurontype(
int)) );
164 m_toolBar->addWidget( neurontypeAct );
166 m_toolBar->addAction(display_labelAct);
167 m_toolBar->addAction(display_weightAct);
168 m_toolBar->addAction(display_deltaAct);
169 m_toolBar->addAction(display_biasAct);
170 m_toolBar->addAction(display_gainAct);
171 m_toolBar->addAction(set_neurondisplayAct);
172 m_toolBar->addAction(set_lesionAct);
173 m_toolBar->addAction(set_neuronbiasAct);
174 m_toolBar->addAction(set_neurongainAct);
175 m_toolBar->addAction(add_ublockAct);
176 m_toolBar->addAction(add_cblockAct);
177 m_toolBar->addAction(add_gblockAct);
178 m_toolBar->addAction(erase_Act);
181 void NetworkDialog::createActions()
185 openAct =
new QAction(QIcon(
":/evorobot/open.png"), tr(
"&Open"),
this);
188 openAct->setStatusTip(tr(
"Open a network architecture or a phenotype file"));
189 connect(openAct, SIGNAL(triggered()),
this, SLOT(open()));
191 saveAct =
new QAction(QIcon(
":/evorobot/save.png"), tr(
"&Save"),
this);
192 saveAct->setStatusTip(tr(
"Save the network architecture or the phenotype from a file"));
193 connect(saveAct, SIGNAL(triggered()),
this, SLOT(save()));
195 set_neurondisplayAct =
new QAction(QIcon(
":/evorobot/show.png"), tr(
"&Set/Unset neurons to be displayed as graph"),
this);
196 set_neurondisplayAct->setShortcut(tr(
"Ctrl+N"));
197 set_neurondisplayAct->setStatusTip(tr(
"Select/Deselect neurons to be displayed as graph"));
198 connect(set_neurondisplayAct, SIGNAL(triggered()),
this, SLOT(set_neurondisplay()));
200 set_neurongainAct =
new QAction(QIcon(
":/evorobot/gain.png"), tr(
"&Add/Remove neurons gain"),
this);
201 set_neurongainAct->setShortcut(tr(
"Ctrl+G"));
202 set_neurongainAct->setStatusTip(tr(
"Add/Remove neurons gain"));
203 connect(set_neurongainAct, SIGNAL(triggered()),
this, SLOT(set_neurongain()));
205 set_neuronbiasAct =
new QAction(QIcon(
":/evorobot/bias.png"), tr(
"&Add/Remove neurons biases"),
this);
206 set_neuronbiasAct->setShortcut(tr(
"Ctrl+B"));
207 set_neuronbiasAct->setStatusTip(tr(
"Add/Remove neurons bias"));
208 connect(set_neuronbiasAct, SIGNAL(triggered()),
this, SLOT(set_neuronbias()));
210 set_lesionAct =
new QAction(QIcon(
":/evorobot/lesion.png"), tr(
"&Lesion/Restore neurons"),
this);
211 set_lesionAct->setShortcut(tr(
"Ctrl+B"));
212 set_lesionAct->setStatusTip(tr(
"Lesion/Restore neurons"));
213 connect(set_lesionAct, SIGNAL(triggered()),
this, SLOT(set_lesion()));
215 display_labelAct =
new QAction(QIcon(
":/evorobot/label.png"), tr(
"&Display/Undisplay neuron labels"),
this);
216 display_labelAct->setShortcut(tr(
"Ctrl+L"));
217 display_labelAct->setStatusTip(tr(
"Display neuron labels"));
218 connect(display_labelAct, SIGNAL(triggered()),
this, SLOT(display_label()));
220 display_weightAct =
new QAction(QIcon(
":/evorobot/weight.png"), tr(
"&Display/Undisplay weights"),
this);
221 display_weightAct->setShortcut(tr(
"Ctrl+W"));
222 display_weightAct->setStatusTip(tr(
"Display the incoming and outcoming weight of a neuron"));
223 connect(display_weightAct, SIGNAL(triggered()),
this, SLOT(display_weight()));
225 display_deltaAct =
new QAction(QIcon(
":/evorobot/ddelta.png"), tr(
"&Display/Undisplay neurons delta"),
this);
226 display_deltaAct->setShortcut(tr(
"Ctrl+Y"));
227 display_deltaAct->setStatusTip(tr(
"Display neurons delta"));
228 connect(display_deltaAct, SIGNAL(triggered()),
this, SLOT(display_delta()));
230 display_biasAct =
new QAction(QIcon(
":/evorobot/dbias.png"), tr(
"&Display/Undisplay biases"),
this);
231 display_biasAct->setShortcut(tr(
"Ctrl+Z"));
232 display_biasAct->setStatusTip(tr(
"Display neurons biases"));
233 connect(display_biasAct, SIGNAL(triggered()),
this, SLOT(display_bias()));
235 display_gainAct =
new QAction(QIcon(
":/evorobot/dgain.png"), tr(
"&Display/Undisplay gains"),
this);
236 display_gainAct->setShortcut(tr(
"Ctrl+G"));
237 display_gainAct->setStatusTip(tr(
"Display neurons gains"));
238 connect(display_gainAct, SIGNAL(triggered()),
this, SLOT(display_gain()));
240 erase_Act =
new QAction(QIcon(
":/evorobot/erase.png"), tr(
"&Erase"),
this);
241 erase_Act->setShortcut(tr(
"Ctrl+E"));
242 erase_Act->setStatusTip(tr(
"Erase all or selected properties"));
243 connect(erase_Act, SIGNAL(triggered()),
this, SLOT(erase()));
245 add_ublockAct =
new QAction(QIcon(
":/evorobot/addublock.png"), tr(
"&Add Update Block"),
this);
246 add_ublockAct->setShortcut(tr(
"Ctrl+A"));
247 add_ublockAct->setStatusTip(tr(
"add an update block"));
248 connect(add_ublockAct, SIGNAL(triggered()),
this, SLOT(add_ublock()));
250 add_cblockAct =
new QAction(QIcon(
":/evorobot/addblock.png"), tr(
"&Add Connection Block"),
this);
251 add_cblockAct->setShortcut(tr(
"Ctrl+A"));
252 add_cblockAct->setStatusTip(tr(
"add a connection block"));
253 connect(add_cblockAct, SIGNAL(triggered()),
this, SLOT(add_cblock()));
255 add_gblockAct =
new QAction(QIcon(
":/evorobot/gainblock.png"), tr(
"&Add a gain block"),
this);
256 add_gblockAct->setShortcut(tr(
"Ctrl+G"));
257 add_gblockAct->setStatusTip(tr(
"add a gain block"));
258 connect(add_gblockAct, SIGNAL(triggered()),
this, SLOT(add_gblock()));
260 increasevAct =
new QShortcut(tr(
"+"),
this);
261 connect(increasevAct, SIGNAL(activated()),
this, SLOT(increasev()));
263 decreasevAct =
new QShortcut(tr(
"-"),
this);
264 connect(decreasevAct, SIGNAL(activated()),
this, SLOT(decreasev()));
271 int NetworkDialog::getcblock()
273 return(cblockAct->currentIndex() - 1);
279 void NetworkDialog::updatetoolb()
286 neurontypeAct->setCurrentIndex(enet->neurontype[cneuron[0]]+1);
293 void NetworkDialog::updateblocktype()
298 cb = cblockAct->currentIndex();
301 blocktypeAct->setCurrentIndex(enet->net_block[cb][5]+1);
308 void NetworkDialog::changeblocktype(
int t)
314 cb = cblockAct->currentIndex();
316 if (t > 0 && cb > 0 )
318 oldt = enet->net_block[cb][5];
319 enet->net_block[cb][5] = t - 1;
322 statusb->setText(QString(
"The type of the selected block has been modified"));
328 void NetworkDialog::set_neurondisplay()
334 if (enet->neurondisplay[cneuron[0]] == 1)
335 enet->neurondisplay[cneuron[0]] = 0;
337 enet->neurondisplay[cneuron[0]] = 1;
341 if (enet->neurondisplay[cneuron[0]] == 1)
342 for (i=cneuron[0]; i <= cneuron[1]; i++)
343 enet->neurondisplay[i] = 0;
345 for (i=cneuron[0]; i <= cneuron[1]; i++)
346 enet->neurondisplay[i] = 1;
348 if (cneuronn < 1 || cneuronn > 2)
349 warning(
"you should select one neuron or one block of neurons first");
351 statusb->setText(QString(
"Neurons to be monitored have been updated"));
354 rendNetwork->update();
358 void NetworkDialog::set_neurontype(
int t)
365 oldt = enet->neurontype[cneuron[0]];
368 enet->neurontype[cneuron[0]] = t-1;
372 for (
int i=cneuron[0]; i <= cneuron[1]; i++)
374 enet->neurontype[i] = t-1;
377 if (cneuronn < 1 || cneuronn > 2)
378 warning(
"you should select one neuron or one block of neurons first");
381 if (oldt != enet->neurontype[cneuron[0]])
382 statusb->setText(QString(
"Neurons' type updated"));
385 rendNetwork->update();
393 void NetworkDialog::selblock_changed(
int t)
396 statusb->setText(QString(
"Block selected"));
397 emit selectedblockChanged();
398 rendNetwork->update();
402 void NetworkDialog::set_neurongain()
406 if (cneuronn < 1 || cneuronn > 2)
408 warning(
"you should select one neuron or one block of neurons first");
414 if (enet->neurongain[cneuron[0]] == 1)
416 enet->neurongain[cneuron[0]] = 0;
417 statusb->setText(QString(
"Neuron's gain eliminated"));
421 enet->neurongain[cneuron[0]] = 1;
422 statusb->setText(QString(
"Neuron's gain added"));
427 if (enet->neurongain[cneuron[0]] == 1)
429 for (i=cneuron[0]; i <= cneuron[1]; i++)
430 enet->neurongain[i] = 0;
431 statusb->setText(QString(
"Neurons' gain eliminated"));
435 for (i=cneuron[0]; i <= cneuron[1]; i++)
436 enet->neurongain[i] = 1;
437 statusb->setText(QString(
"Neurons' gain added"));
444 rendNetwork->update();
450 void NetworkDialog::set_neuronbias()
454 if (cneuronn < 1 || cneuronn > 2)
456 warning(
"you should select one neuron or one block of neurons first");
462 if (enet->neuronbias[cneuron[0]] == 1)
464 enet->neuronbias[cneuron[0]] = 0;
465 statusb->setText(QString(
"Neuron's bias eliminated"));
469 enet->neuronbias[cneuron[0]] = 1;
470 statusb->setText(QString(
"Neuron's bias added"));
475 if (enet->neuronbias[cneuron[0]] == 1)
477 for (i=cneuron[0]; i <= cneuron[1]; i++)
478 enet->neuronbias[i] = 0;
479 statusb->setText(QString(
"Neurons' bias eliminated"));
483 for (i=cneuron[0]; i <= cneuron[1]; i++)
484 enet->neuronbias[i] = 1;
485 statusb->setText(QString(
"Neurons' bias added"));
489 rendNetwork->update();
497 void NetworkDialog::set_lesion()
500 if (enet->neuronlesions == 0)
504 enet->neuronlesion[cneuron[0]]=!enet->neuronlesion[cneuron[0]];
506 enet->neuronlesions = 1;
508 statusb->setText(QString(
"Neuron's lesioned"));
509 rendNetwork->update();
514 for(
int ls = cneuron[0]; ls <= cneuron[1]; ls++)
516 enet->neuronlesion[ls] = !enet->neuronlesion[ls];
517 enet->neuronlesions++;
521 statusb->setText(QString(
"Neurons' lesioned"));
522 rendNetwork->update();
526 warning(
"you should select one neuron or one block of neurons first");
531 enet->neuronlesions = 0;
532 for (
int i = 0; i < enet->nneurons; i++)
534 enet->neuronlesion[i]=
false;
535 enet->neuronlesionVal[i]=0.0;
538 statusb->setText(QString(
"Neurons un-lesioned"));
539 rendNetwork->update();
544 void NetworkDialog::display_label()
549 pseudo_activate_net();
550 statusb->setText(QString(
"Neurons labels displayed"));
551 rendNetwork->update();
558 void NetworkDialog::display_delta()
561 if (cneuronn == 1 || cneuronn == 2)
564 pseudo_activate_net();
567 statusb->setText(QString(
"Neurons' timeconstants weight displayed"));
568 rendNetwork->update();
572 warning(
"you should select one neuron or one block of neurons first");
578 void NetworkDialog::display_weight()
584 pseudo_activate_net();
585 statusb->setText(QString(
"Connections weights displayed"));
588 rendNetwork->update();
592 warning(
"you should select the receiving and sending block of neurons first (4 neurons)");
597 void NetworkDialog::display_bias()
600 if (cneuronn == 1 || cneuronn == 2)
603 pseudo_activate_net();
604 statusb->setText(QString(
"Biases weight displayed"));
605 rendNetwork->update();
610 warning(
"you should select one neuron or one block of neurons first");
616 void NetworkDialog::display_gain()
620 if (cneuronn == 1 || cneuronn == 2)
623 pseudo_activate_net();
626 statusb->setText(QString(
"Neurons' gain weight displayed"));
627 rendNetwork->update();
631 warning(
"you should select one neuron or one block of neurons first");
637 void NetworkDialog::erase()
645 cblock = getcblock();
650 enet->net_nblocks = 0;
652 cblockAct->addItems(QStringList() <<
"blocks list");
653 for(i=0;i < enet->nneurons; i++)
655 enet->neuronbias[i] = 0;
656 enet->neurontype[i] = 0;
657 enet->neurongain[i] = 0;
658 enet->neurondisplay[i] = 1;
660 statusb->setText(QString(
"All blocks erased and neurons' properties reset"));
665 cblockAct->removeItem(cblock+1);
666 cblockAct->setCurrentIndex(0);
667 for (bb=cblock; bb < (enet->net_nblocks - 1); bb++)
668 for (ii=0; ii < 6; ii++)
669 enet->net_block[bb][ii] = enet->net_block[bb+1][ii];
671 statusb->setText(QString(
"Selected block erased"));
673 rendNetwork->update();
678 void NetworkDialog::add_ublock()
685 if (cneuronn != 1 && cneuronn != 2)
687 warning(
"you should select a a lock (2 neurons) first");
691 cblock = getcblock();
693 cblock = enet->net_nblocks;
697 for(i=enet->net_nblocks; i > cblock; i--)
698 for(ii=0; ii<6; ii++)
699 enet->net_block[i][ii] = enet->net_block[i-1][ii];
700 enet->net_block[cblock][0] = 1;
701 enet->net_block[cblock][1] = cneuron[0];
702 enet->net_block[cblock][2] = 1;
703 enet->net_block[cblock][3] = 0;
704 enet->net_block[cblock][4] = 0;
705 enet->net_block[cblock][5] = 0;
706 block_name(cblock, stblock);
707 cblockAct->insertItems(cblock+1, QStringList() << stblock);
714 for(i=enet->net_nblocks; i > cblock; i--)
715 for(ii=0; ii<6; ii++)
716 enet->net_block[i][ii] = enet->net_block[i-1][ii];
717 enet->net_block[cblock][0] = 1;
718 enet->net_block[cblock][1] = cneuron[0];
719 enet->net_block[cblock][2] = cneuron[1] - cneuron[0] + 1;
720 enet->net_block[cblock][3] = 0;
721 enet->net_block[cblock][4] = 0;
722 enet->net_block[cblock][5] = 0;
723 block_name(cblock, stblock);
724 cblockAct->insertItems(cblock+1, QStringList() << stblock);
729 cblockAct->setCurrentIndex(0);
730 statusb->setText(QString(
"Update block added"));
731 rendNetwork->update();
736 void NetworkDialog::add_cblock()
746 warning(
"you should select the receiving and sending blocks (4 neurons) first");
750 cblock = getcblock();
751 printf(
"cblock %d\n", cblock);
753 cblock = enet->net_nblocks;
758 for(i=enet->net_nblocks; i > cblock; i--)
759 for(ii=0; ii<6; ii++)
760 enet->net_block[i][ii] = enet->net_block[i-1][ii];
761 enet->net_block[cblock][0] = 0;
762 enet->net_block[cblock][1] = cneuron[0];
763 enet->net_block[cblock][2] = cneuron[1] - cneuron[0] + 1;
764 enet->net_block[cblock][3] = cneuron[2];
765 enet->net_block[cblock][4] = cneuron[3] - cneuron[2] + 1;
766 enet->net_block[cblock][5] = 0;
767 block_name(cblock, stblock);
768 cblockAct->insertItems(cblock+1, QStringList() << stblock);
773 cblockAct->setCurrentIndex(0);
774 statusb->setText(QString(
"Connection block added"));
775 rendNetwork->update();
779 void NetworkDialog::add_gblock()
786 if (cneuronn != 2 && cneuronn != 3)
788 warning(
"you should select a block (2 neurons) and eventually a third neuron first");
792 cblock = getcblock();
794 cblock = enet->net_nblocks;
800 for(i=enet->net_nblocks; i > cblock; i--)
801 for(ii=0; ii<6; ii++)
802 enet->net_block[i][ii] = enet->net_block[i-1][ii];
803 enet->net_block[cblock][0] = 2;
804 enet->net_block[cblock][1] = cneuron[0];
805 enet->net_block[cblock][2] = cneuron[1] - cneuron[0] + 1;
806 enet->net_block[cblock][3] = 0;
807 enet->net_block[cblock][4] = 0;
808 enet->net_block[cblock][5] = 0;
809 block_name(cblock, stblock);
810 cblockAct->insertItems(cblock+1, QStringList() << stblock);
817 for(i=enet->net_nblocks; i > cblock; i--)
818 for(ii=0; ii<6; ii++)
819 enet->net_block[i][ii] = enet->net_block[i-1][ii];
820 enet->net_block[cblock][0] = 3;
821 enet->net_block[cblock][1] = cneuron[0];
822 enet->net_block[cblock][2] = cneuron[1] - cneuron[0] + 1;
823 enet->net_block[cblock][3] = cneuron[2];
824 enet->net_block[cblock][4] = 0;
825 enet->net_block[cblock][5] = 0;
826 block_name(cblock, stblock);
827 cblockAct->insertItems(cblock+1, QStringList() << stblock);
832 cblockAct->setCurrentIndex(0);
833 statusb->setText(QString(
"Gain block added"));
834 rendNetwork->update();
841 void NetworkDialog::open()
849 QString fileName = QFileDialog::getOpenFileName(
this,
850 "Choose a filename to open",
855 if (fileName.endsWith(
"net"))
857 filen = fileName.toAscii();
861 statusb->setText(QString(
"File %1 loaded").arg(filename));
864 if (fileName.endsWith(
"phe"))
866 filen = fileName.toAscii();
870 statusb->setText(QString(
"File %1 loaded").arg(filename));
878 void NetworkDialog::save()
886 QString fileName = QFileDialog::getSaveFileName(
this,
887 "Choose a filename to save",
892 if (fileName.endsWith(
"net"))
894 filen = fileName.toAscii();
897 enet->save_net_blocks(filename, 0);
898 statusb->setText(QString(
"File %1 saved").arg(filename));
902 if (fileName.endsWith(
"phe"))
904 filen = fileName.toAscii();
907 enet->save_net_blocks(filename, 1);
908 statusb->setText(QString(
"File %1 saved").arg(filename));
914 void NetworkDialog::increasev()
924 pseudo_activate_net();
925 if (enet->nselected == 1)
927 gp = enet->selectedp;
929 *v += ((enet->wrange * 2.0f) / 256.0f);
930 pseudo_activate_net();
931 rendNetwork->update();
938 if (pseudomode != 1 && pseudomode != 4)
940 pseudo_activate_net();
941 if (enet->nselected == 1)
943 gp = enet->selectedp;
945 *v += ((enet->wrange * 2.0f) / 256.0f);
946 pseudo_activate_net();
947 rendNetwork->update();
954 void NetworkDialog::decreasev()
965 pseudo_activate_net();
966 if (enet->nselected == 1)
968 gp = enet->selectedp;
970 *v -= ((enet->wrange * 2.0f) / 256.0f);
971 pseudo_activate_net();
972 rendNetwork->update();
979 if (pseudomode != 1 && pseudomode != 4)
981 pseudo_activate_net();
982 if (enet->nselected == 1)
984 gp = enet->selectedp;
986 *v -= ((enet->wrange * 2.0f) / 256.0f);
987 pseudo_activate_net();
988 rendNetwork->update();
995 void NetworkDialog::block_name(
int b,
char *st)
997 switch(enet->net_block[b][0])
1000 sprintf(st,
"c) %s_%s %s_%s", enet->neuronl[enet->net_block[b][1]], enet->neuronl[enet->net_block[b][1]+enet->net_block[b][2]-1],enet->neuronl[enet->net_block[b][3]],enet->neuronl[enet->net_block[b][3]+enet->net_block[b][4]-1]);
1003 sprintf(st,
"u) %s_%s", enet->neuronl[enet->net_block[b][1]], enet->neuronl[enet->net_block[b][1]+enet->net_block[b][2]-1]);
1006 sprintf(st,
"g) %s_%s", enet->neuronl[enet->net_block[b][1]], enet->neuronl[enet->net_block[b][1]+enet->net_block[b][2]-1]);
1009 sprintf(st,
"g) %s_%s %s", enet->neuronl[enet->net_block[b][1]], enet->neuronl[enet->net_block[b][1]+enet->net_block[b][2]-1],enet->neuronl[enet->net_block[b][3]]);
1012 sprintf(st,
"undefined block type");
1024 NetworkDialog::pseudo_activate_net()
1057 gp = enet->selectedp;
1058 enet->nselected = 0;
1060 for(i=0;i < enet->nneurons;i++)
1061 strcpy(neuroncl[i],enet->neuronl[i]);
1067 for(i=0;i < enet->nneurons;i++)
1069 if (enet->neurongain[i] == 1)
1071 if (dgain > 0 && ((cneuronn == 1 && i == cneuron[0]) || (cneuronn == 2 && i >= cneuron[0] && i <= cneuron[1])))
1073 gain = (fabs((
double) *p) / enet->wrange) * enet->grange;
1074 sprintf(neuroncl[i],
"%.1f",gain);
1084 for(i=0;i < enet->nneurons;i++)
1086 if (enet->neuronbias[i] == 1)
1088 if (dbias > 0 && ((cneuronn == 1 && i == cneuron[0]) || (cneuronn == 2 && i >= cneuron[0] && i <= cneuron[1])))
1090 bias = (fabs((
double) *p) / enet->wrange) * enet->brange;
1091 sprintf(neuroncl[i],
"%.1f",bias);
1101 for (b=0; b < enet->net_nblocks; b++)
1104 if (enet->net_block[b][0] == 0)
1106 for(t=enet->net_block[b][1]; t < enet->net_block[b][1] + enet->net_block[b][2];t++)
1107 for(i=enet->net_block[b][3]; i < enet->net_block[b][3] + enet->net_block[b][4];i++)
1110 if (dweight == 1 && cneuronn == 4 && (t >= cneuron[0] && t <= cneuron[1]) && (i >= cneuron[2] && i <= cneuron[3]))
1113 if (cneuron[0] == cneuron[1])
1114 sprintf(neuroncl[i],
"%.1f",*p);
1123 if (enet->net_block[b][0] == 1)
1125 for(t=enet->net_block[b][1]; t < (enet->net_block[b][1] + enet->net_block[b][2]); t++)
1128 if (enet->neurontype[t] != 1)
1135 delta = (float) (fabs((
double) *p) / enet->wrange);
1136 if (ddelta > 0 && ((cneuronn == 1 && t == cneuron[0]) || (cneuronn == 2 && t >= cneuron[0] && t <= cneuron[1])))
1138 sprintf(neuroncl[t],
"%.1f",delta);
1155 void NetworkDialog::openMixer()
1159 mixerDialog->setUpMixer();
1160 mixerDialog->show();
1161 rendNetwork->update();
1171 RendNetwork::RendNetwork( NetworkDialog* networkDialog, QWidget *parent)
1175 antialiased =
false;
1176 pixmap.load(
":/evorobot/qt-logo.png");
1178 setBackgroundRole(QPalette::Base);
1179 this->networkDialog = networkDialog;
1185 QSize RendNetwork::minimumSizeHint()
const
1187 return QSize(250, 250);
1190 QSize RendNetwork::sizeHint()
const
1192 return QSize(550, 500);
1195 void RendNetwork::setShape(Shape shape)
1197 this->shape = shape;
1201 void RendNetwork::setPen(
const QPen &pen)
1209 void RendNetwork::setBrush(
const QBrush &brush)
1211 this->brush = brush;
1215 void RendNetwork::setAntialiased(
bool antialiased)
1217 this->antialiased = antialiased;
1221 void RendNetwork::setTransformed(
bool transformed)
1223 this->transformed = transformed;
1228 void RendNetwork::paintEvent(QPaintEvent *)
1239 int selectedneurons[2];
1240 QPainter painter(
this);
1242 QRect labelxy(0,0,30,20);
1244 painter.setPen(pen);
1245 QPen pen(Qt::black, 1);
1246 painter.setPen(pen);
1247 selectedneurons[0] = selectedneurons[1] = -1;
1249 if (networkDialog->enet->drawnxmax > 0 && networkDialog->enet->drawnymax > 0)
1251 networkDialog->cscaley = height() / (float) networkDialog->enet->drawnymax;
1252 networkDialog->cscalex = width() / (float) networkDialog->enet->drawnxmax;
1253 painter.scale(networkDialog->cscalex, networkDialog->cscaley);
1257 painter.scale(1.0,1.0);
1259 painter.setPen(Qt::gray);
1261 p = networkDialog->enet->freep;
1262 for(i=0;i < networkDialog->enet->nneurons;i++)
1264 if (networkDialog->enet->neurongain[i] == 1)
1267 for(i=0;i < networkDialog->enet->nneurons;i++)
1269 if (networkDialog->enet->neuronbias[i] == 1)
1271 networkDialog->biases[i] = *p;
1276 for (i=0; i < networkDialog->enet->net_nblocks; i++)
1278 if (networkDialog->enet->net_block[i][0] == 0)
1279 for (t=networkDialog->enet->net_block[i][1]; t < (networkDialog->enet->net_block[i][1] + networkDialog->enet->net_block[i][2]); t++)
1280 for (b=networkDialog->enet->net_block[i][3]; b < (networkDialog->enet->net_block[i][3] + networkDialog->enet->net_block[i][4]); b++)
1283 if (networkDialog->getcblock() == i)
1288 if (w != networkDialog->enet->DEFAULT_VALUE)
1290 if (w > networkDialog->enet->wrange)
1291 w = networkDialog->enet->wrange;
1292 if (w < (0.0 - networkDialog->enet->wrange))
1293 w = 0.0 - networkDialog->enet->wrange;
1297 cg -= ((w / networkDialog->enet->wrange) * 255);
1298 cb -= ((w / networkDialog->enet->wrange) * 255);
1302 cg += ((w / networkDialog->enet->wrange) * 255);
1303 cr += ((w / networkDialog->enet->wrange) * 255);
1313 painter.setPen(QPen(QColor(cr,cg,cb,255), wid, Qt::SolidLine));
1315 if (abs(networkDialog->enet->neuronxy[b][1] - networkDialog->enet->neuronxy[t][1]) > 20)
1316 painter.drawLine((
float) networkDialog->enet->neuronxy[t][0], (
float) networkDialog->enet->neuronxy[t][1], (
float) networkDialog->enet->neuronxy[b][0], (
float) networkDialog->enet->neuronxy[b][1]);
1319 dx = abs(networkDialog->enet->neuronxy[t][0] - networkDialog->enet->neuronxy[b][0]);
1320 dy = abs(networkDialog->enet->neuronxy[t][1] - networkDialog->enet->neuronxy[b][1]);
1321 if (networkDialog->enet->neuronxy[t][0] < networkDialog->enet->neuronxy[b][0])
1322 sx = networkDialog->enet->neuronxy[t][0];
1324 sx = networkDialog->enet->neuronxy[b][0];
1325 if (networkDialog->enet->neuronxy[t][1] < networkDialog->enet->neuronxy[b][1])
1326 sy = networkDialog->enet->neuronxy[t][1];
1328 sy = networkDialog->enet->neuronxy[b][1];
1329 painter.drawArc((
float) sx,(
float) (sy-20),(
float) dx, (
float) (dy+40), 0 * 16, 180 * 16);
1333 if (networkDialog->enet->net_block[i][0] == 1)
1335 if (networkDialog->getcblock() == i)
1337 selectedneurons[0] = networkDialog->enet->net_block[i][1];
1338 selectedneurons[1] = networkDialog->enet->net_block[i][2];
1340 for(t=networkDialog->enet->net_block[i][1]; t < (networkDialog->enet->net_block[i][1] + networkDialog->enet->net_block[i][2]); t++)
1341 if (networkDialog->enet->neurontype[t] == 1)
1346 for (i=0; i < networkDialog->enet->nneurons; i++)
1348 if (networkDialog->enet->neuronbias[i] == 1)
1350 w = networkDialog->biases[i];
1351 if (w != networkDialog->enet->DEFAULT_VALUE)
1353 if (w > networkDialog->enet->wrange)
1354 w = networkDialog->enet->wrange;
1355 if (w < (0.0 - networkDialog->enet->wrange))
1356 w = 0.0 - networkDialog->enet->wrange;
1360 cg -= ((w / networkDialog->enet->wrange) * 255);
1361 cb -= ((w / networkDialog->enet->wrange) * 255);
1365 cg += ((w / networkDialog->enet->wrange) * 255);
1366 cr += ((w / networkDialog->enet->wrange) * 255);
1376 if (i >= selectedneurons[0] && i < selectedneurons[0]+selectedneurons[1])
1378 painter.setPen(QPen(QColor(cr,cg,cb,255), wid, Qt::SolidLine));
1383 if (i >= selectedneurons[0] && i < selectedneurons[0]+selectedneurons[1])
1385 painter.setPen(QPen(Qt::black, wid, Qt::SolidLine));
1388 if (networkDialog->enet->neurontype[i] == 0)
1389 painter.setBrush(QBrush(QColor(75, 75, 75, 255), Qt::SolidPattern));
1391 if (networkDialog->enet->neurontype[i] == 1)
1392 painter.setBrush(QBrush(QColor(255, 0, 0, 255), Qt::SolidPattern));
1394 if (networkDialog->enet->neurontype[i] == 2)
1395 painter.setBrush(QBrush(QColor(0, 0, 255, 255), Qt::SolidPattern));
1397 painter.setBrush(QBrush(QColor(150, 150, 150, 255), Qt::SolidPattern));
1399 painter.drawEllipse((
float) (networkDialog->enet->neuronxy[i][0] - 5), (
float) (networkDialog->enet->neuronxy[i][1] - 5), 10.0, 10.0);
1402 if (networkDialog->enet->neurongain[i] == 1)
1404 painter.setPen(QPen(QColor(255,255,255,255), wid, Qt::SolidLine));
1405 painter.setBrush(QBrush(QColor(255, 255, 255, 255), Qt::SolidPattern));
1406 painter.drawEllipse((
float) (networkDialog->enet->neuronxy[i][0] - 1), (
float) (networkDialog->enet->neuronxy[i][1] - 1), 2.0, 2.0);
1411 painter.setPen(Qt::black);
1412 for (i=0; i < networkDialog->enet->nneurons; i++)
1414 if (networkDialog->enet->neurondisplay[i] == 1)
1415 painter.setPen(Qt::black);
1417 painter.setPen(Qt::red);
1418 if (i < networkDialog->ninputs)
1419 labelxy.setRect((
float) (networkDialog->enet->neuronxy[i][0] - 5), (
float) (networkDialog->enet->neuronxy[i][1] + 5), 30, 30);
1421 labelxy.setRect((
float) (networkDialog->enet->neuronxy[i][0] - 5), (
float) (networkDialog->enet->neuronxy[i][1] - 18), 30, 30);
1422 painter.drawText(labelxy, networkDialog->neuroncl[i]);
1424 painter.setPen(Qt::darkRed);
1425 if (networkDialog->enet->neuronlesion[i]) painter.drawText(networkDialog->enet->neuronxy[i][0]-5,networkDialog->enet->neuronxy[i][1]-5,
"X");
1428 painter.setBrush(Qt::black);
1429 if (networkDialog->cneuronn == 1)
1430 painter.drawEllipse((
float) (networkDialog->enet->neuronxy[networkDialog->cneuron[0]][0] - 5), (
float) (networkDialog->enet->neuronxy[networkDialog->cneuron[0]][1] - 5), 10.0, 10.0);
1431 if (networkDialog->cneuronn >= 2)
1433 if (networkDialog->cneuron[0] <= networkDialog->cneuron[1])
1435 for (i=networkDialog->cneuron[0]; i <= networkDialog->cneuron[1]; i++)
1436 painter.drawEllipse((
float) (networkDialog->enet->neuronxy[i][0] - 5), (
float) (networkDialog->enet->neuronxy[i][1] - 5), 10.0, 10.0);
1440 painter.drawEllipse((
float) (networkDialog->enet->neuronxy[networkDialog->cneuron[0]][0] - 5), (
float) (networkDialog->enet->neuronxy[networkDialog->cneuron[0]][1] - 5), 10.0, 10.0);
1441 painter.drawEllipse((
float) (networkDialog->enet->neuronxy[networkDialog->cneuron[1]][0] - 5), (
float) (networkDialog->enet->neuronxy[networkDialog->cneuron[1]][1] - 5), 10.0, 10.0);
1444 painter.setBrush(Qt::blue);
1445 if (networkDialog->cneuronn == 3)
1446 painter.drawEllipse((
float) (networkDialog->enet->neuronxy[networkDialog->cneuron[2]][0] - 5), (
float) (networkDialog->enet->neuronxy[networkDialog->cneuron[2]][1] - 5), 10.0, 10.0);
1447 if (networkDialog->cneuronn == 4 && networkDialog->cneuron[2] <= networkDialog->cneuron[3])
1448 for (i=networkDialog->cneuron[2]; i <= networkDialog->cneuron[3]; i++)
1449 painter.drawEllipse((
float) (networkDialog->enet->neuronxy[i][0] - 5), (
float) (networkDialog->enet->neuronxy[i][1] - 5), 10.0, 10.0);
1460 RendNetwork::mousePressEvent(QMouseEvent *event)
1488 for(i=0; i < networkDialog->enet->nneurons; i++)
1490 if (mdist((
float) x,(
float) y, (
float) networkDialog->enet->neuronxy[i][0] * networkDialog->cscalex, (
float) networkDialog->enet->neuronxy[i][1] * networkDialog->cscaley) < 10.0)
1492 if (networkDialog->cneuronn >= 4)
1493 networkDialog->cneuronn = 0;
1494 networkDialog->cneuron[networkDialog->cneuronn] = i;
1495 networkDialog->cneuronn++;
1497 networkDialog->statusb->setText(QString(
"%1 neurons selected").arg(networkDialog->cneuronn));
1498 emit selectedneuronsChanged();
1505 for (i=0; i < networkDialog->enet->net_nblocks; i++)
1507 if (networkDialog->enet->net_block[i][0] == 0)
1508 for (t=networkDialog->enet->net_block[i][1]; t < (networkDialog->enet->net_block[i][1] + networkDialog->enet->net_block[i][2]); t++)
1509 for (b=networkDialog->enet->net_block[i][3]; b < (networkDialog->enet->net_block[i][3] + networkDialog->enet->net_block[i][4]); b++)
1511 cdist = segmentdist((
float) x, (
float) y, (
float) networkDialog->enet->neuronxy[t][0] * networkDialog->cscalex, (
float) networkDialog->enet->neuronxy[t][1] * networkDialog->cscaley, (
float) networkDialog->enet->neuronxy[b][0] * networkDialog->cscalex, (
float) networkDialog->enet->neuronxy[b][1] * networkDialog->cscaley);
1513 if (cdist < 1 && cdist < dist)
1516 networkDialog->cneuron[0] = t;
1517 networkDialog->cneuron[1] = t;
1518 networkDialog->cneuron[2] = b;
1519 networkDialog->cneuron[3] = b;
1520 networkDialog->cneuronn = 4;
1521 networkDialog->statusb->setText(QString(
"A single connection have been selected"));
1532 networkDialog->cneuronn = 0;
1533 networkDialog->pseudomode = 0;
1534 networkDialog->pseudo_activate_net();
1535 networkDialog->statusb->setText(QString(
"0 selected neurons"));
1536 emit selectedneuronsChanged();
1551 RendNetwork::mouseMoveEvent(QMouseEvent *event)
1567 networkDialog->cneuronn = 0;
1570 for(i=0, cn=-1; i < networkDialog->enet->nneurons; i++)
1571 if (mdist((
float) rnmousex,(
float) rnmousey, (
float) networkDialog->enet->neuronxy[i][0] * networkDialog->cscalex, (
float) networkDialog->enet->neuronxy[i][1] * networkDialog->cscaley) < 10.0)
1575 networkDialog->enet->neuronxy[cn][0] = (float) x / networkDialog->cscalex;
1576 networkDialog->enet->neuronxy[cn][1] = (
float) y / networkDialog->cscaley;
1581 networkDialog->statusb->setText(QString(
"neuron position modified"));
1595 RendNetwork::mouseReleaseEvent(QMouseEvent *event)
1606 if (networkDialog->grid > 0)
1609 rnmousex=
event->x();
1610 rnmousey=
event->y();
1616 for(i=0, cn=-1; i < networkDialog->enet->nneurons; i++)
1617 if (mdist((
float) rnmousex,(
float) rnmousey, networkDialog->enet->neuronxy[i][0], networkDialog->enet->neuronxy[i][1]) < 10.0)
1621 oldp1 = networkDialog->enet->neuronxy[cn][0];
1622 oldp2 = networkDialog->enet->neuronxy[cn][1];
1623 networkDialog->enet->neuronxy[cn][0] = (float) (((
int) networkDialog->enet->neuronxy[cn][0]) / networkDialog->grid * networkDialog->grid);
1624 networkDialog->enet->neuronxy[cn][1] = (float) (((
int) networkDialog->enet->neuronxy[cn][1]) / networkDialog->grid * networkDialog->grid);
1625 if ((oldp1 - networkDialog->enet->neuronxy[cn][0]) > (networkDialog->grid / 2))
1626 networkDialog->enet->neuronxy[cn][0] += networkDialog->grid;
1627 if ((oldp2 - networkDialog->enet->neuronxy[cn][1]) > (networkDialog->grid / 2))
1628 networkDialog->enet->neuronxy[cn][1] += networkDialog->grid;
1630 networkDialog->statusb->setText(QString(
"neuron's position modified"));
1640 *o = (float) (((
int) *o) / networkDialog->grid * networkDialog->grid);
1641 *(o + 1) = (
float) (((int) *(o + 1)) / networkDialog->grid * networkDialog->grid);
1642 if ((oldp1 - *o) > (networkDialog->grid / 2))
1643 *o += networkDialog->grid;
1644 if ((oldp2 - *(o + 1)) > (networkDialog->grid / 2))
1645 *(o + 1) += networkDialog->grid;
1646 networkDialog->statusb->setText(QString(
"neuron's position modified"));
1661 RendNetwork::mouseDoubleClickEvent(QMouseEvent* )
1669 RendNetwork::mdist(
float x,
float y,
float x1,
float y1)
1674 qdist = ((x-x1)*(x-x1)) + ((y-y1)*(y-y1));
1675 return(sqrt(qdist));
1685 RendNetwork::segmentdist(
float px,
float py,
float ax,
float ay,
float bx,
float by)
1689 dab = sqrt(((ax-bx)*(ax-bx)) + ((ay-by)*(ay-by)));
1690 dpa = sqrt(((px-ax)*(px-ax)) + ((py-ay)*(py-ay)));
1691 dpb = sqrt(((px-bx)*(px-bx)) + ((py-by)*(py-by)));
1693 if (dpa < dab && dpb < dab)
1694 return((dpa+dpb)-dab);
1700 EvoSlider::EvoSlider(
float *ref,
float rangemin ,
float rangemax, QWidget * parent)
1703 setRange((
int)(rangemin*100),(
int)(rangemax*100));
1704 setValue( (*vref)*100 );
1705 setOrientation(Qt::Vertical);
1706 connect(
this,SIGNAL(valueChanged(
int)),
this, SLOT(updateValue(
int)));
1709 void EvoSlider::updateValue(
int ival)
1711 *vref=(float)ival/100.0;
1712 emit networkChanged();
1716 MixerDialog::MixerDialog(Evonet *enet, NetworkDialog* netd)
1721 layout=
new QGridLayout(
this);
1724 void MixerDialog::setUpMixer()
1729 foreach(QObject* child, children()) {
1730 delete dynamic_cast<QWidget*
>(child);
1733 if(evonet->neuronlesions || evonet->nselected > 0)
1735 statusmixer =
new QLabel(
"",
this);
1736 if (netdl->pseudomode == 2)
1737 statusmixer->setText(QString(
"weights:"));
1738 if (netdl->pseudomode == 1)
1739 statusmixer->setText(QString(
"t-const:"));
1740 if (netdl->pseudomode == 3)
1741 statusmixer->setText(QString(
"biases:"));
1742 if (netdl->pseudomode == 4)
1743 statusmixer->setText(QString(
"gains:"));
1744 if (evonet->neuronlesions)
1745 statusmixer->setText(QString(
"neurons:"));
1746 layout->addWidget(statusmixer,0,0);
1747 Logger::info(QString(
"Mixer for n.%1 selected parameters\n").arg(evonet->neuronlesions+evonet->nselected));
1757 if (evonet->nselected > 0)
1759 gp = evonet->selectedp;
1761 if (netdl->pseudomode == 2)
1764 lt = netdl->cneuron[0];
1765 li = netdl->cneuron[2];
1767 for(
int i=0;i<evonet->nselected;i++,v++,li++)
1769 if (li > netdl->cneuron[3])
1772 li = netdl->cneuron[2];
1775 layout->addWidget(
new QLabel(evonet->neuronl[lt],
this),1,i);
1776 EvoSlider* slide =
new EvoSlider(v,(0 - evonet->getWrange()),evonet->getWrange(),
this);
1777 connect( slide, SIGNAL(networkChanged()), netdl->rendNetwork, SLOT(update()) );
1778 layout->addWidget(slide,2,i);
1779 layout->addWidget(
new QLabel(evonet->neuronl[li],
this),3,i);
1785 l = netdl->cneuron[0];
1786 for(
int i=0;i<evonet->nselected;i++,v++,l++)
1788 layout->addWidget(
new QLabel(evonet->neuronl[l],
this),1,i);
1789 EvoSlider* slide =
new EvoSlider(v,(0 - evonet->getWrange()),evonet->getWrange(),
this);
1790 connect( slide, SIGNAL(networkChanged()), netdl->rendNetwork, SLOT(update()) );
1791 layout->addWidget(slide,2,i);
1798 for(
int i=0;i<evonet->getNoNeurons();i++)
1799 if(evonet->neuronlesion[i])
1801 layout->addWidget(
new QLabel(evonet->neuronl[i],
this),1,i);
1802 EvoSlider* slide =
new EvoSlider(&evonet->neuronlesionVal[i],evonet->neuronrange[i][0],evonet->neuronrange[i][1],
this);
1803 connect( slide, SIGNAL(networkChanged()), netdl->rendNetwork, SLOT(update()) );
1804 layout->addWidget(slide,2,i);
1813 #if defined(_MSC_VER)
1814 #pragma warning(pop)