/* ============================================================================== This is an automatically generated GUI class created by the Introjucer! Be careful when adding custom code to these files, as only the code within the "//[xyz]" and "//[/xyz]" sections will be retained when the file is loaded and re-saved. Created with Introjucer version: 3.1.0 ------------------------------------------------------------------------------ The Introjucer is part of the JUCE library - "Jules' Utility Class Extensions" Copyright 2004-13 by Raw Material Software Ltd. ============================================================================== */ //[Headers] You can add your own extra header files here... #ifdef WIN32 #include #endif //[/Headers] #include "MainComponent.h" //[MiscUserDefs] You can add your own user definitions and misc code here... #ifdef WIN32 void mylog(const char* format, ...) { char log_buf[257]; va_list argptr; va_start(argptr, format); vsprintf(log_buf, format, argptr); va_end(argptr); OutputDebugStringA (log_buf); } #else void mylog(const char* format, ...) { char log_buf[257]; va_list argptr; va_start(argptr, format); vprintf(format, argptr); va_end(argptr); } #endif //[/MiscUserDefs] //============================================================================== MainComponent::MainComponent () : m_layers(this), m_pRbm(nullptr), Draw(nullptr), Draw2(nullptr), DrawWeights(nullptr), DrawHidden(nullptr) { addAndMakeVisible (trainButton = new TextButton ("Train button")); trainButton->setButtonText (TRANS("Train")); trainButton->addListener (this); addAndMakeVisible (addButton = new TextButton ("Add button")); addButton->setButtonText (TRANS("Add")); addButton->addListener (this); addAndMakeVisible (patterSlider = new Slider ("Pattern slider")); patterSlider->setRange (0, 0, 1); patterSlider->setSliderStyle (Slider::LinearHorizontal); patterSlider->setTextBoxStyle (Slider::TextBoxLeft, false, 80, 20); patterSlider->addListener (this); addAndMakeVisible (reconstructButton = new TextButton ("Reconstruct button")); reconstructButton->setButtonText (TRANS("Reconstruct")); reconstructButton->addListener (this); addAndMakeVisible (ShakeButton = new TextButton ("Shake button")); ShakeButton->setButtonText (TRANS("Shake")); ShakeButton->addListener (this); addAndMakeVisible (WeightsSlider = new Slider ("Weights slider")); WeightsSlider->setRange (0, 0, 1); WeightsSlider->setSliderStyle (Slider::LinearHorizontal); WeightsSlider->setTextBoxStyle (Slider::TextBoxLeft, false, 80, 20); WeightsSlider->addListener (this); addAndMakeVisible (numEpochslabel = new Label ("Num Epochs label", TRANS("99999"))); numEpochslabel->setFont (Font (15.00f, Font::plain)); numEpochslabel->setJustificationType (Justification::centred); numEpochslabel->setEditable (true, true, false); numEpochslabel->setColour (TextEditor::textColourId, Colours::black); numEpochslabel->setColour (TextEditor::backgroundColourId, Colour (0x00000000)); numEpochslabel->addListener (this); addAndMakeVisible (learningRateLabel = new Label ("Learning Rate label", TRANS("0.001"))); learningRateLabel->setFont (Font (15.00f, Font::plain)); learningRateLabel->setJustificationType (Justification::centred); learningRateLabel->setEditable (true, true, false); learningRateLabel->setColour (TextEditor::textColourId, Colours::black); learningRateLabel->setColour (TextEditor::backgroundColourId, Colour (0x00000000)); learningRateLabel->addListener (this); addAndMakeVisible (testButton = new TextButton ("Test button")); testButton->setButtonText (TRANS("Test")); testButton->addListener (this); addAndMakeVisible (numVisibleLabel = new Label ("Num Visible label", TRANS("99999"))); numVisibleLabel->setFont (Font (15.00f, Font::plain)); numVisibleLabel->setJustificationType (Justification::centred); numVisibleLabel->setEditable (true, true, false); numVisibleLabel->setColour (TextEditor::textColourId, Colours::black); numVisibleLabel->setColour (TextEditor::backgroundColourId, Colour (0x00000000)); numVisibleLabel->addListener (this); addAndMakeVisible (numHiddenLabel = new Label ("Num Hidden label", TRANS("99999"))); numHiddenLabel->setFont (Font (15.00f, Font::plain)); numHiddenLabel->setJustificationType (Justification::centred); numHiddenLabel->setEditable (true, true, false); numHiddenLabel->setColour (TextEditor::textColourId, Colours::black); numHiddenLabel->setColour (TextEditor::backgroundColourId, Colour (0x00000000)); numHiddenLabel->addListener (this); addAndMakeVisible (createButton = new TextButton ("Create button")); createButton->setButtonText (TRANS("Create")); createButton->addListener (this); addAndMakeVisible (projectNameLabel = new Label ("Project Name label", TRANS("ProjectName"))); projectNameLabel->setFont (Font (15.00f, Font::plain)); projectNameLabel->setJustificationType (Justification::centred); projectNameLabel->setEditable (true, true, false); projectNameLabel->setColour (TextEditor::textColourId, Colours::black); projectNameLabel->setColour (TextEditor::backgroundColourId, Colour (0x00000000)); projectNameLabel->addListener (this); addAndMakeVisible (loadButton = new TextButton ("Load button")); loadButton->setButtonText (TRANS("Load")); loadButton->addListener (this); addAndMakeVisible (saveButton = new TextButton ("Save button")); saveButton->setButtonText (TRANS("Save")); saveButton->addListener (this); addAndMakeVisible (numVisibleYLabel = new Label ("Num Visible Y label", TRANS("99999"))); numVisibleYLabel->setFont (Font (15.00f, Font::plain)); numVisibleYLabel->setJustificationType (Justification::centred); numVisibleYLabel->setEditable (true, true, false); numVisibleYLabel->setColour (TextEditor::textColourId, Colours::black); numVisibleYLabel->setColour (TextEditor::backgroundColourId, Colour (0x00000000)); numVisibleYLabel->addListener (this); addAndMakeVisible (loadTrainingButton = new TextButton ("Load Training button")); loadTrainingButton->setButtonText (TRANS("Load T")); loadTrainingButton->addListener (this); addAndMakeVisible (saveTrainingButton = new TextButton ("Save Training button")); saveTrainingButton->setButtonText (TRANS("Save T")); saveTrainingButton->addListener (this); addAndMakeVisible (clearTrainingButton = new TextButton ("Clear Training button")); clearTrainingButton->setButtonText (TRANS("Clear T")); clearTrainingButton->addListener (this); addAndMakeVisible (removeTrainingButton = new TextButton ("Remove Training button")); removeTrainingButton->setButtonText (TRANS("Remove T")); removeTrainingButton->addListener (this); addAndMakeVisible (numGibbsSlider = new Slider ("Num. Gibbs slider")); numGibbsSlider->setRange (1, 10, 1); numGibbsSlider->setSliderStyle (Slider::LinearHorizontal); numGibbsSlider->setTextBoxStyle (Slider::TextBoxLeft, false, 80, 20); numGibbsSlider->addListener (this); addAndMakeVisible (reconstructEquButton = new TextButton ("Reconstruct Equilibrium button")); reconstructEquButton->setButtonText (TRANS("Reconst Equ.")); reconstructEquButton->addListener (this); addAndMakeVisible (rbmUseExpectationsToggleButton = new ToggleButton ("rbmUseExpectations toggle button")); rbmUseExpectationsToggleButton->setButtonText (TRANS("Use Expectations")); rbmUseExpectationsToggleButton->addListener (this); addAndMakeVisible (rbmDoRaoBlackwellToggleButton = new ToggleButton ("rbmDoRaoBlackwell toggle button")); rbmDoRaoBlackwellToggleButton->setButtonText (TRANS("Rao-Blackwell")); rbmDoRaoBlackwellToggleButton->addListener (this); addAndMakeVisible (rbmDoRobbinsMonroToggleButton = new ToggleButton ("rbmDoRobbinsMonro toggle button")); rbmDoRobbinsMonroToggleButton->setButtonText (TRANS("Robbins-Monro")); rbmDoRobbinsMonroToggleButton->addListener (this); addAndMakeVisible (rbmReduceVarianceToggleButton = new ToggleButton ("rbmReduceVariance toggle button")); rbmReduceVarianceToggleButton->setButtonText (TRANS("Reduce Variance")); rbmReduceVarianceToggleButton->addListener (this); //[UserPreSize] m_vNumX = 16; m_vNumY = 16; m_hNum = 64; m_vNumX_next = 16; m_vNumY_next = 16; m_hNum_next = 64; m_numGibbs = 1; m_weights.setUnits(m_vNumX*m_vNumY, m_hNum); create(); //[/UserPreSize] setSize (600, 400); //[Constructor] You can add your own custom stuff here.. projectNameLabel->setText(String("TestPrj"), dontSendNotification ); numEpochslabel->setText(String(100), dontSendNotification ); learningRateLabel->setText(String(0.2), dontSendNotification ); rbmUseExpectationsToggleButton->setToggleState(false, sendNotification); rbmDoRaoBlackwellToggleButton->setToggleState(false, sendNotification); rbmDoRobbinsMonroToggleButton->setToggleState(false, sendNotification); rbmReduceVarianceToggleButton->setToggleState(false, sendNotification); //[/Constructor] } MainComponent::~MainComponent() { //[Destructor_pre]. You can add your own custom destruction code here.. //[/Destructor_pre] trainButton = nullptr; addButton = nullptr; patterSlider = nullptr; reconstructButton = nullptr; ShakeButton = nullptr; WeightsSlider = nullptr; numEpochslabel = nullptr; learningRateLabel = nullptr; testButton = nullptr; numVisibleLabel = nullptr; numHiddenLabel = nullptr; createButton = nullptr; projectNameLabel = nullptr; loadButton = nullptr; saveButton = nullptr; numVisibleYLabel = nullptr; loadTrainingButton = nullptr; saveTrainingButton = nullptr; clearTrainingButton = nullptr; removeTrainingButton = nullptr; numGibbsSlider = nullptr; reconstructEquButton = nullptr; rbmUseExpectationsToggleButton = nullptr; rbmDoRaoBlackwellToggleButton = nullptr; rbmDoRobbinsMonroToggleButton = nullptr; rbmReduceVarianceToggleButton = nullptr; //[Destructor]. You can add your own custom destruction code here.. Draw = nullptr; Draw2 = nullptr; DrawWeights = nullptr; DrawHidden = nullptr; m_pRbm = nullptr; //[/Destructor] } //============================================================================== void MainComponent::paint (Graphics& g) { //[UserPrePaint] Add your own custom painting code here.. //[/UserPrePaint] g.fillAll (Colours::white); //[UserPaint] Add your own custom painting code here.. //[/UserPaint] } void MainComponent::resized() { trainButton->setBounds (120, 312, 72, 24); addButton->setBounds (24, 160, 72, 24); patterSlider->setBounds (224, 312, 184, 24); reconstructButton->setBounds (24, 312, 72, 24); ShakeButton->setBounds (120, 352, 72, 24); WeightsSlider->setBounds (224, 352, 184, 24); numEpochslabel->setBounds (24, 280, 72, 24); learningRateLabel->setBounds (120, 280, 72, 24); testButton->setBounds (120, 160, 72, 24); numVisibleLabel->setBounds (440, 256, 72, 24); numHiddenLabel->setBounds (488, 288, 72, 24); createButton->setBounds (488, 320, 72, 24); projectNameLabel->setBounds (472, 224, 96, 24); loadButton->setBounds (440, 192, 72, 24); saveButton->setBounds (520, 192, 72, 24); numVisibleYLabel->setBounds (512, 256, 72, 24); loadTrainingButton->setBounds (440, 48, 72, 24); saveTrainingButton->setBounds (520, 48, 72, 24); clearTrainingButton->setBounds (520, 88, 72, 24); removeTrainingButton->setBounds (440, 88, 72, 24); numGibbsSlider->setBounds (224, 272, 184, 24); reconstructEquButton->setBounds (24, 352, 72, 24); rbmUseExpectationsToggleButton->setBounds (24, 232, 128, 24); rbmDoRaoBlackwellToggleButton->setBounds (24, 200, 112, 24); rbmDoRobbinsMonroToggleButton->setBounds (168, 232, 120, 24); rbmReduceVarianceToggleButton->setBounds (168, 200, 128, 24); //[UserResized] Add your own custom resize handling here.. Draw->setBounds (16, 16, 100, 100); Draw2->setBounds (110+16, 16, 100, 100); DrawWeights->setBounds (220+16, 16, 100, 100); DrawHidden->setBounds (16, 16+110, 320, 20); //[/UserResized] } void MainComponent::buttonClicked (Button* buttonThatWasClicked) { //[UserbuttonClicked_Pre] //[/UserbuttonClicked_Pre] if (buttonThatWasClicked == trainButton) { //[UserButtonCode_trainButton] -- add your button handler code here.. m_pRbm->train(m_layers, numEpochslabel->getText().getIntValue(), learningRateLabel->getText().getFloatValue(), m_numGibbs, m_rbmUseExpectations, m_rbmDoRaoBlackwell, m_rbmReduceEstimatorVariance, m_rbmDoRobbinsMonro); //[/UserButtonCode_trainButton] } else if (buttonThatWasClicked == addButton) { //[UserButtonCode_addButton] -- add your button handler code here.. Draw2->setData(Draw->getData()); m_layers.add(Draw->getData(), m_vNumX*m_vNumY); patterSlider->setRange (0, m_layers.getSize()-1, 1); //[/UserButtonCode_addButton] } else if (buttonThatWasClicked == reconstructButton) { //[UserButtonCode_reconstructButton] -- add your button handler code here.. Draw2->setData(m_pRbm->toVisible(m_pRbm->toHidden(Draw->getData()))); DrawHidden->setData(m_pRbm->toHidden(Draw->getData())); //[/UserButtonCode_reconstructButton] } else if (buttonThatWasClicked == ShakeButton) { //[UserButtonCode_ShakeButton] -- add your button handler code here.. m_weights.shuffle(0.01); //[/UserButtonCode_ShakeButton] } else if (buttonThatWasClicked == testButton) { //[UserButtonCode_testButton] -- add your button handler code here.. Draw->setData(Draw2->getData()); //[/UserButtonCode_testButton] } else if (buttonThatWasClicked == createButton) { //[UserButtonCode_createButton] -- add your button handler code here.. m_vNumX = m_vNumX_next; m_vNumY = m_vNumY_next; m_hNum = m_hNum_next; numVisibleLabel->setText(String(m_vNumX), dontSendNotification ); numVisibleYLabel->setText(String(m_vNumY), dontSendNotification ); numHiddenLabel->setText(String(m_hNum), dontSendNotification ); m_weights.setUnits(m_vNumX*m_vNumY, m_hNum); create(); //[/UserButtonCode_createButton] } else if (buttonThatWasClicked == loadButton) { //[UserButtonCode_loadButton] -- add your button handler code here.. load(); //[/UserButtonCode_loadButton] } else if (buttonThatWasClicked == saveButton) { //[UserButtonCode_saveButton] -- add your button handler code here.. save(); //[/UserButtonCode_saveButton] } else if (buttonThatWasClicked == loadTrainingButton) { //[UserButtonCode_loadTrainingButton] -- add your button handler code here.. m_layers.clear(); m_layers.load((String(getBaseDir() + String(".trainingStates.dat"))).toUTF8()); //[/UserButtonCode_loadTrainingButton] } else if (buttonThatWasClicked == saveTrainingButton) { //[UserButtonCode_saveTrainingButton] -- add your button handler code here.. m_layers.save((String(getBaseDir() + String(".trainingStates.dat"))).toUTF8()); //[/UserButtonCode_saveTrainingButton] } else if (buttonThatWasClicked == clearTrainingButton) { //[UserButtonCode_clearTrainingButton] -- add your button handler code here.. m_layers.clear(); //[/UserButtonCode_clearTrainingButton] } else if (buttonThatWasClicked == removeTrainingButton) { //[UserButtonCode_removeTrainingButton] -- add your button handler code here.. m_layers.removeAt((int)patterSlider->getValue()); //[/UserButtonCode_removeTrainingButton] } else if (buttonThatWasClicked == reconstructEquButton) { //[UserButtonCode_reconstructEquButton] -- add your button handler code here.. uint32_t i; const double *pV, *pH; pV = Draw->getData(); for (i=0; i < 100; i++) { pH = m_pRbm->toHidden(pV); DrawHidden->setData(pH); pV = m_pRbm->toVisible(pH); Draw2->setData(pV); } //[/UserButtonCode_reconstructEquButton] } else if (buttonThatWasClicked == rbmUseExpectationsToggleButton) { //[UserButtonCode_rbmUseExpectationsToggleButton] -- add your button handler code here.. m_rbmUseExpectations = buttonThatWasClicked->getToggleState(); //[/UserButtonCode_rbmUseExpectationsToggleButton] } else if (buttonThatWasClicked == rbmDoRaoBlackwellToggleButton) { //[UserButtonCode_rbmDoRaoBlackwellToggleButton] -- add your button handler code here.. m_rbmDoRaoBlackwell = buttonThatWasClicked->getToggleState(); //[/UserButtonCode_rbmDoRaoBlackwellToggleButton] } else if (buttonThatWasClicked == rbmDoRobbinsMonroToggleButton) { //[UserButtonCode_rbmDoRobbinsMonroToggleButton] -- add your button handler code here.. m_rbmDoRobbinsMonro = buttonThatWasClicked->getToggleState(); //[/UserButtonCode_rbmDoRobbinsMonroToggleButton] } else if (buttonThatWasClicked == rbmReduceVarianceToggleButton) { //[UserButtonCode_rbmReduceVarianceToggleButton] -- add your button handler code here.. m_rbmReduceEstimatorVariance = buttonThatWasClicked->getToggleState(); //[/UserButtonCode_rbmReduceVarianceToggleButton] } //[UserbuttonClicked_Post] //[/UserbuttonClicked_Post] } void MainComponent::sliderValueChanged (Slider* sliderThatWasMoved) { //[UsersliderValueChanged_Pre] //[/UsersliderValueChanged_Pre] if (sliderThatWasMoved == patterSlider) { //[UserSliderCode_patterSlider] -- add your slider handling code here.. if (m_layers.getSize() > 0) { VisibleLayer &p = (VisibleLayer&)m_layers.getAt((int)sliderThatWasMoved->getValue()); Draw2->setData(p.getStates()); } //[/UserSliderCode_patterSlider] } else if (sliderThatWasMoved == WeightsSlider) { //[UserSliderCode_WeightsSlider] -- add your slider handling code here.. double **ppW = m_weights.getWeights(); if (!ppW) return; double *pW = ppW[(int)sliderThatWasMoved->getValue()]; double *pTemp = new double [m_vNumX*m_vNumY]; double min = +1E12; double max = -1E12; for (int i=0; i < m_vNumX*m_vNumY; i++) { pTemp[i] = pW[i]; min = std::min(min, pW[i]); max = std::max(max, pW[i]); } for (int i=0; i < m_vNumX*m_vNumY; i++) { pTemp[i] -= min; } for (int i=0; i < m_vNumX*m_vNumY; i++) { pTemp[i] /= (max-min); } DrawWeights->setData(pTemp); delete pTemp; //[/UserSliderCode_WeightsSlider] } else if (sliderThatWasMoved == numGibbsSlider) { //[UserSliderCode_numGibbsSlider] -- add your slider handling code here.. m_numGibbs = (uint32_t)sliderThatWasMoved->getValue(); //[/UserSliderCode_numGibbsSlider] } //[UsersliderValueChanged_Post] //[/UsersliderValueChanged_Post] } void MainComponent::labelTextChanged (Label* labelThatHasChanged) { //[UserlabelTextChanged_Pre] //[/UserlabelTextChanged_Pre] if (labelThatHasChanged == numEpochslabel) { //[UserLabelCode_numEpochslabel] -- add your label text handling code here.. //[/UserLabelCode_numEpochslabel] } else if (labelThatHasChanged == learningRateLabel) { //[UserLabelCode_learningRateLabel] -- add your label text handling code here.. //[/UserLabelCode_learningRateLabel] } else if (labelThatHasChanged == numVisibleLabel) { //[UserLabelCode_numVisibleLabel] -- add your label text handling code here.. m_vNumX_next = labelThatHasChanged->getText().getIntValue(); //[/UserLabelCode_numVisibleLabel] } else if (labelThatHasChanged == numHiddenLabel) { //[UserLabelCode_numHiddenLabel] -- add your label text handling code here.. m_hNum_next = labelThatHasChanged->getText().getIntValue(); //[/UserLabelCode_numHiddenLabel] } else if (labelThatHasChanged == projectNameLabel) { //[UserLabelCode_projectNameLabel] -- add your label text handling code here.. //[/UserLabelCode_projectNameLabel] } else if (labelThatHasChanged == numVisibleYLabel) { //[UserLabelCode_numVisibleYLabel] -- add your label text handling code here.. m_vNumY_next = labelThatHasChanged->getText().getIntValue(); //[/UserLabelCode_numVisibleYLabel] } //[UserlabelTextChanged_Post] //[/UserlabelTextChanged_Post] } //[MiscUserCode] You can add your own definitions of your custom methods or any other code here... void MainComponent::load () { m_weights.load((String(getBaseDir() + String(".weights.dat"))).toUTF8()); m_vNumX = m_vNumY = (int)sqrt((float)m_weights.getNumVisible()); m_hNum = m_weights.getNumHidden(); create(); } void MainComponent::save () { m_weights.save((String(getBaseDir() + String(".weights.dat"))).toUTF8()); } void MainComponent::create() { Draw = nullptr; Draw2 = nullptr; DrawWeights = nullptr; DrawHidden = nullptr; m_pRbm = nullptr; addAndMakeVisible (Draw = new DrawComponent (m_vNumX, m_vNumY)); Draw->setListener(this); addAndMakeVisible (Draw2 = new DrawComponent (m_vNumX, m_vNumY)); addAndMakeVisible (DrawWeights = new DrawComponent (m_vNumX, m_vNumY)); addAndMakeVisible (DrawHidden = new DrawComponent (m_hNum, 1)); DrawHidden->setListener(this); numVisibleLabel->setText(String(m_vNumX), dontSendNotification ); numVisibleYLabel->setText(String(m_vNumY), dontSendNotification ); numHiddenLabel->setText(String(m_hNum), dontSendNotification ); m_pRbm = new Rbm(m_weights, this); WeightsSlider->setRange(0, m_hNum-1, 1); resized(); } void MainComponent::destroy() { } const juce::String& MainComponent::getBaseDir() { m_baseDir = String("./") + projectNameLabel->getText(); return m_baseDir; } void MainComponent::onChanged(const LayerArray &obj) { patterSlider->setRange(0, std::max(0,(int)m_layers.getSize()-1), 1); } void MainComponent::onEpochTrained(const Rbm &obj) { m_trainingProgress = obj.getProgress(); mylog("Training %f %%\n", m_trainingProgress*100); } void MainComponent::onDraw(DrawComponent &obj) { if (&obj == DrawHidden) { Draw2->setData(m_pRbm->toVisible(obj.getData())); } if (&obj == Draw) { Draw2->setData(m_pRbm->toVisible(m_pRbm->toHidden(obj.getData()))); DrawHidden->setData(m_pRbm->toHidden(obj.getData())); } } //[/MiscUserCode] //============================================================================== #if 0 /* -- Introjucer information section -- This is where the Introjucer stores the metadata that describe this GUI layout, so make changes in here at your peril! BEGIN_JUCER_METADATA END_JUCER_METADATA */ #endif //[EndFile] You can add extra defines here... //[/EndFile]