/**
* Loads a neural net.
*/
BKPNeuralNet::BKPNeuralNet( char* filename )
{
	int i, j; /* Temporary counters. */
	int lixo;
	int layers, *nLayers;
	FILE *f;
	char name[128];
	char namebin[128];

	/* Checking arguments: */
	if ( filename == NULL )
		return;

	/* Inserting the correct extensions: */
	sprintf( name, "%s.nnt", filename );
	sprintf( namebin, "%s.bin", filename );

	f = fopen(name, "r");
	if ( f == NULL )
		return;

	/* Number of layers: */
	fscanf( f, "Number of layers: %d\n", &layers);

	/* Space to the number of neurons on each layer: */
	nLayers = (int*)malloc( layers * sizeof(int) );
	if( nLayers == NULL )
		return;

	/* Number of neurons on each layer: */
	for( i = 0; i < layers; i++ )
	{
		fscanf( f, "Neurons on layer %d: %d\n", &lixo, &(nLayers[i]) );
	}

	//BKPNeuralNet( layers, nLayers );
	CreateNet( layers, nLayers );

	/* Closing the text file: */
	fclose( f );

	/* Opening the binary file: */
	f = fopen( namebin, "rb" );
	if( f == NULL )
		return;

	/* Values of the neurons: */
	for( i = 0; i < _layers; i++ )
		fread( _neurons[i] , sizeof(float), _nLayers[i], f );

	/* Weight values */
	for( i = _layers - 2; i >= 0; i-- )			   
		for( j = 0; j <= _nLayers[i+1]; j++ )	
			fread( _weights[i][j], sizeof(float), _nLayers[i]+1, f );

	/* Closing file: */
	fclose( f );
}
Esempio n. 2
0
//------------------------------default ctor ----------------------------
//
//	creates a ANN based on the default values in params.ini
//-----------------------------------------------------------------------
CNeuralNet::CNeuralNet() 
{
	m_NumInputs	          =	CParams::iNumInputs;
	m_NumOutputs		      =	CParams::iNumOutputs;
	m_NumHiddenLayers	    =	CParams::iNumHidden;
	m_NeuronsPerHiddenLyr =	CParams::iNeuronsPerHiddenLayer;

	CreateNet();

}
Esempio n. 3
0
//------------------------------default ctor ----------------------------
//
//	creates a ANN based on the default values in params.ini
//-----------------------------------------------------------------------
CNeuralNet::CNeuralNet() 
{
	m_NumInputs	          =	4;
	m_NumOutputs		      =	2;
	m_NumHiddenLayers	    =	1;
	m_NeuronsPerHiddenLyr =	6;

	CreateNet();

}
Esempio n. 4
0
//------------------------------- ctor -----------------------------------
//
//------------------------------------------------------------------------
CNeuralNet::CNeuralNet(int NumInputs,
                       int NumOutputs,
                       int HiddenNeurons,
                       double LearningRate):m_iNumInputs(NumInputs),
                                            m_iNumOutputs(NumOutputs),
                                            m_iNumHiddenLayers(1),
                                            m_iNeuronsPerHiddenLyr(HiddenNeurons),
                                            m_dLearningRate(LearningRate),
                                            m_dErrorSum(9999),
                                            m_bTrained(false),
                                            m_iNumEpochs(0)
{
	CreateNet();
}
Esempio n. 5
0
void Scene2Controller::Setup() {
  // Box2D
  CreateBorder();
  CreateCourt();
  CreateNet();
  scene_begin_time = ofGetElapsedTimef();
  const ofPoint left(256-64, 200);
  const ofPoint right(768-64, 200);
  const float pause = 0.5;
  model_.dialogue
      .Speed(20.0)
      .Foreground(ofColor::white)
      .Background(ofColor(0, 0, 0, 0))
      .Position("left", left)
      .Position("right", right)
      .Message("You know why you're here, right?", "right").Pause(pause)
      .Message("No.", "left").Pause(pause)
      .Message("Because you're bad at tennis.\nI'm gonna make you good.", "right").Pause(2.0)
      .Clear();
}
Esempio n. 6
0
void Scene3Controller::Setup() {
  // Box2D
  CreateBorder();
  CreateCourt();
  CreateEight();
  CreateNet();
  model_.world.SetContactListener(this);
  const ofPoint right(768-256, 50);
  const float pause = 2.0;
  model_.dialogue
      .Speed(25.0)
      .PunctuationDelay(0.0)
      .FontSize(16.0)
      .Foreground(ofColor::black)
      .Position("court", ofPoint(768, 600))
      .Position("right", right)
      .Position("below", right + ofVec2f(0, 100)).Then([this] () {
        std::vector<float> volume_targets;
        volume_targets.push_back(0.1); //bass sound
        volume_targets.push_back(0.5); //high arpeg v1
        volume_targets.push_back(0.0); //high arpeg v2
        volume_targets.push_back(0.0); //low arpeg v1
        volume_targets.push_back(0.0); //low arpeg v2
        volume_targets.push_back(0.0); //words 1
        volume_targets.push_back(0.0); //words 2
        volume_targets.push_back(0.0); //words 3
        scene_manager.GetMusic().GetSoundEffect<LoopSet>("opponents")->SetVolumeTargets(volume_targets);
      }).Message("I don't believe in gravity, \nI will surely win this match!", "right").Then([this] () {
        model_.served = false;
        model_.ball_in_play = true;
        model_.opponent_index = 0;
      })
      .Barrier("point").Then([this] () {
        std::vector<float> volume_targets;
        volume_targets.push_back(0.2); //bass sound
        volume_targets.push_back(0.5); //high arpeg v1
        volume_targets.push_back(0.0); //high arpeg v2
        volume_targets.push_back(1.0); //low arpeg v1
        volume_targets.push_back(0.0); //low arpeg v2
        volume_targets.push_back(0.0); //words 1
        volume_targets.push_back(0.0); //words 2
        volume_targets.push_back(0.0); //words 3
        scene_manager.GetMusic().GetSoundEffect<LoopSet>("opponents")->SetVolumeTargets(volume_targets);
      }).Clear().Pause(pause)
  .Message("I have time, \nI will surely win this match!", "right").Then([this] () {
    model_.served = false;
    model_.ball_in_play = true;
    model_.opponent_index = 1;
  })
  .Barrier("point").Then([this] () {
        std::vector<float> volume_targets;
        volume_targets.push_back(0.5); //bass sound
        volume_targets.push_back(0.3); //high arpeg v1
        volume_targets.push_back(0.0); //high arpeg v2
        volume_targets.push_back(1.0); //low arpeg v1
        volume_targets.push_back(0.0); //low arpeg v2
        volume_targets.push_back(0.0); //words 1
        volume_targets.push_back(0.0); //words 2
        volume_targets.push_back(0.0); //words 3
        scene_manager.GetMusic().GetSoundEffect<LoopSet>("opponents")->SetVolumeTargets(volume_targets);
      }).Clear().Pause(pause)
  .Message("I have many racquets, \nI will surely win this match!", "right").Then([this] () {
    model_.served = false;
    model_.ball_in_play = true;
    model_.opponent_index = 2;
  })
  .Barrier("point").Then([this] () {
        std::vector<float> volume_targets;
        volume_targets.push_back(0.5); //bass sound
        volume_targets.push_back(0.2); //high arpeg v1
        volume_targets.push_back(0.0); //high arpeg v2
        volume_targets.push_back(1.0); //low arpeg v1
        volume_targets.push_back(0.0); //low arpeg v2
        volume_targets.push_back(0.0); //words 1
        volume_targets.push_back(0.0); //words 2
        volume_targets.push_back(0.0); //words 3
        scene_manager.GetMusic().GetSoundEffect<LoopSet>("opponents")->SetVolumeTargets(volume_targets);
      }).Clear().Pause(pause)
  .Message("We're playing a whole different game,\nwe will surely win this match!", "right").Then([this] () {
    model_.served = true;
    model_.ball_in_play = true;
    model_.opponent_index = 3;
  })
  .Barrier("point").Then([this] () {
        std::vector<float> volume_targets;
        volume_targets.push_back(0.5); //bass sound
        volume_targets.push_back(0.2); //high arpeg v1
        volume_targets.push_back(0.0); //high arpeg v2
        volume_targets.push_back(0.9); //low arpeg v1
        volume_targets.push_back(0.0); //low arpeg v2
        volume_targets.push_back(0.0); //words 1
        volume_targets.push_back(0.3); //words 2
        volume_targets.push_back(0.0); //words 3
        scene_manager.GetMusic().GetSoundEffect<LoopSet>("opponents")->SetVolumeTargets(volume_targets);
      }).Clear().Pause(pause)
  .Message("I'm made of glass, \nI will surely win this match!", "right").Then([this] () {
    model_.served = false;
    model_.ball_in_play = true;
    model_.opponent_index = 4;
  })
  .Barrier("point").Then([this] () {
        std::vector<float> volume_targets;
        volume_targets.push_back(0.5); //bass sound
        volume_targets.push_back(0.0); //high arpeg v1
        volume_targets.push_back(0.5); //high arpeg v2
        volume_targets.push_back(0.0); //low arpeg v1
        volume_targets.push_back(1.0); //low arpeg v2
        volume_targets.push_back(0.0); //words 1
        volume_targets.push_back(0.3); //words 2
        volume_targets.push_back(0.0); //words 3
        scene_manager.GetMusic().GetSoundEffect<LoopSet>("opponents")->SetVolumeTargets(volume_targets);
  }).Clear().Pause(pause).Then([this] () {
    model_.served = true;
    model_.ball_in_play = true;
    model_.opponent_index = 5;
  })
  .Barrier("point").Then([this] () {
        std::vector<float> volume_targets;
        volume_targets.push_back(0.5); //bass sound
        volume_targets.push_back(0.0); //high arpeg v1
        volume_targets.push_back(0.5); //high arpeg v2
        volume_targets.push_back(1.0); //low arpeg v1
        volume_targets.push_back(0.0); //low arpeg v2
        volume_targets.push_back(0.6); //words 1
        volume_targets.push_back(0.0); //words 2
        volume_targets.push_back(0.0); //words 3
        scene_manager.GetMusic().GetSoundEffect<LoopSet>("opponents")->SetVolumeTargets(volume_targets);
      })
      .Message("I'm late, I will...", "court").Pause(0.5).Clear().Pause(0.5)
  .Message("I'm a blank slate,\nI will surely win this match!", "right").Then([this] () {
    model_.served = false;
    model_.ball_in_play = true;
    model_.opponent_index = 6;
  })
  .Barrier("point").Then([this] () {
        std::vector<float> volume_targets;
        volume_targets.push_back(0.7); //bass sound
        volume_targets.push_back(0.0); //high arpeg v1
        volume_targets.push_back(0.5); //high arpeg v2
        volume_targets.push_back(1.0); //low arpeg v1
        volume_targets.push_back(0.0); //low arpeg v2
        volume_targets.push_back(0.4); //words 1
        volume_targets.push_back(0.0); //words 2
        volume_targets.push_back(0.4); //words 3
        scene_manager.GetMusic().GetSoundEffect<LoopSet>("opponents")->SetVolumeTargets(volume_targets);
      }).Clear().Pause(pause)
  .Message("I've spent my life studying this game,\nI will surely win this match!", "right").Then([this] () {
    model_.served = false;
    model_.ball_in_play = true;
    model_.opponent_index = 7;
  })
  .Barrier("point").Then([this] () {
        std::vector<float> volume_targets;
        volume_targets.push_back(0.7); //bass sound
        volume_targets.push_back(0.0); //high arpeg v1
        volume_targets.push_back(0.3); //high arpeg v2
        volume_targets.push_back(0.5); //low arpeg v1
        volume_targets.push_back(0.0); //low arpeg v2
        volume_targets.push_back(0.4); //words 1
        volume_targets.push_back(0.0); //words 2
        volume_targets.push_back(0.4); //words 3
        scene_manager.GetMusic().GetSoundEffect<LoopSet>("opponents")->SetVolumeTargets(volume_targets);
      }).Clear().Pause(pause).Foreground(ofColor::white)
  .Message("I'm the score itself, \nI will surely win this match!", "right").Then([this] () {
    model_.eight_body->SetActive(true);
    model_.served = false;
    model_.ball_in_play = true;
    model_.opponent_index = 8;
  })
  .Barrier("point").Then([this] () {
        std::vector<float> volume_targets;
        volume_targets.push_back(0.9); //bass sound
        volume_targets.push_back(0.0); //high arpeg v1
        volume_targets.push_back(0.2); //high arpeg v2
        volume_targets.push_back(0.0); //low arpeg v1
        volume_targets.push_back(1.0); //low arpeg v2
        volume_targets.push_back(0.5); //words 1
        volume_targets.push_back(0.0); //words 2
        volume_targets.push_back(0.5); //words 3
        scene_manager.GetMusic().GetSoundEffect<LoopSet>("opponents")->SetVolumeTargets(volume_targets);
      }).Clear().Pause(pause).Foreground(ofColor::black)
  .Message("I'm you, \nI will surely win this match!", "right").Then([this] () {
    model_.eight_body->SetActive(false);
    model_.served = false;
    model_.ball_in_play = true;
    model_.opponent_index = 9;
  })
  .Barrier("score").Then([this] () {
    model_.served = false;
    model_.ball_in_play = true;
  }).Barrier("score").Then([this] () {
    model_.served = false;
    model_.ball_in_play = true;
  }).Barrier("point").Clear();
}
Esempio n. 7
0
void CPnet::CPMenu(void) {
	int selection;
	while(selection != 8) {
		cout << "=================================================================\n";
		cout << "\tPerceptron Neural Net using Perceptron learning rule\n";
		cout << "=================================================================\n\n";
		cout << "[1]. Create a new Neural Net" << endl;
		cout << "[2]. Train the current Neural Net" << endl;
		cout << "[3]. Load a Neural Net from file" << endl;
		cout << "[4]. Test Neural Net" << endl;
		cout << "[5]. Save the current Net" << endl;
		cout << "[6]. Set training display" << endl;
		cout << "[7]. Display Neural Net Info" << endl;
		cout << "[8]. Quit the program" << endl  << endl;
		cout << "selection: ";
		selection = getche() - '0';
		WAIT(0.5*SECOND);
		CLEAR_SCREEN();
		switch(selection) {
		case 1:
			SaveCurrentData();
			cout << "\nCreating Neural Net...";
			WAIT(1*SECOND);
			CreateNet();
			break;
		case 2:
			if (bNeuralNetCreated) {
				cout << "\nTraining \"" << szNeuralNetName << "\" function..." << endl;
				WAIT(0.7*SECOND);
				cout << "Enter Learning rate: ";
				cin >> LEARNING_RATE;
				WAIT(0.7*SECOND);
				cout << "Enter number of epochs: ";
				cin >> CPN_ITER;
				NormalizeInput();
				NormalizeTarget();
				CLEAR_SCREEN();
				WAIT(0.7*SECOND);
				cout << "\nTraining \"" << szNeuralNetName << "\" function..." << endl;
				WAIT(2*SECOND);
				CLEAR_SCREEN();
				TrainNetwork();
			} else {
				UpdateScreen();
			}
			break;
		case 3:
			SaveCurrentData();
			cout << "\n\nLoading Neural Net..." << endl;
			WAIT(0.85*SECOND);
			cout << "\nEnter the name of the Neural Net or the complete path where "
			     << "the Net data can be found\n: ";
			cin.sync();
			cin.getline(szNeuralNetName, MAX_PATH);
			LoadNet();
			WAIT(1.5*SECOND);
			CLEAR_SCREEN();
			cout << "\n\n\n\n\n\n\n\n\n\t\t\tNeural Net loaded successfuly!" << endl;
			UpdateScreen();
			break;
		case 4:
			if (bNeuralNetTrained) {
				cout << "\nTesting \"" << szNeuralNetName << "\" function..." << endl;
				cout << "1. default test" << endl;
				cout << "2. selective test" << endl;
				cout << "\nselection: ";
				int nNumOfPattern;
				int sel = getche();
				CLEAR_SCREEN();
				WAIT(0.7*SECOND);
				DeNormalizeInput();
				DeNormalizeTarget();
				switch(sel) {
				case '1':
					TestNetwork();
					break;
				case '2':
					cout << "\nEnter the number of patterns to be tested: ";
					cin >> nNumOfPattern;
					SelectiveTest(nNumOfPattern);
					UpdateScreen();
					break;
				default:
					cout << "\nunknown selection." << endl;
					UpdateScreen();
				}
			} else {
				UpdateScreen();
			}
			break;
		case 5:
			if (bNeuralNetCreated) {
				if (fExist(szNeuralNetName)) {
					CLEAR_SCREEN();
					WAIT(0.5*SECOND);
					cout << "\n\"" << szNeuralNetName << "\"";
					cout << ": this file already exist,do you want to overwrite it?" << endl;
					cout << "Yes(y) No(n): ";
					char response;
					cin >> response;
					response = tolower(response);
					if(response == 'y') {
						SaveNet();
					} else {
						WAIT(0.5*SECOND);
						cout << "\nPlease enter a new name or a complete file path where the " 
							<< "Neural Net will be saved\n: ";
						cin.sync();
						cin.getline(szNeuralNetName, MAX_PATH);
						SaveNet();
					}
				} else {
					SaveNet();
				}
				CLEAR_SCREEN();
				WAIT(1.5*SECOND);
				cout << "\n\n\n\n\n\n\n\n\n\t\t\tThe Neural Net was saved successfuly!" << endl;
			}
/**
  * Loads a neural net.
  */
int LoadNet( char* filename, PtNet *myNet )
{
	int i, j; /* Temporary counters. */
  int lixo;
  int layers, *nLayers;
	FILE *f;
  char name[128];
  char namebin[128];
  PtNet Net;


  /* Checking arguments: */
	if ( filename == NULL )
		return -1;

  /* Inserting the correct extensions: */
  sprintf( name, "%s.nnt", filename );
  sprintf( namebin, "%s.bin", filename );
  
  f = fopen(name, "r");
	if ( f == NULL )
		return -3;

	/* Number of layers: */
  fscanf(f, "Number of layers: %d\n", &layers);
  /* Space to the number of neurons on each layer: */
  nLayers = (int*)malloc(layers*sizeof(int));
  if (nLayers == NULL)
	  return -1;
	/* Number of neurons on each layer: */
	for ( i=0 ; i<layers ; i++ )
  {
		fscanf(f, "Neurons on layer %d: %d\n", &lixo, &(nLayers[i]));
    printf( "Neurons on layer %d: %d\n", lixo, (nLayers[i]));
  }

  CreateNet( &Net, layers, nLayers);

  /* Closing the text file: */
  fclose(f);

  /* Opening the binary file: */
  f = fopen(namebin, "rb");
	if ( f == NULL )
		return -3;


	/* Values of the neurons: */
	for ( i=0 ; i<Net->Layers ; i++ )
	{
    fread(Net->Neurons[i] , sizeof(float), Net->NLayers[i], f);
		/*for ( j=0 ; j<Net->NLayers[i] ; j++ )
		fscanf(f, "%f", &(Net->Neurons[i][j]));*/
	}

	/* Weight values */
	for( i=Net->Layers-2 ; i>=0 ; i-- )			   
		for( j=0 ; j<=Net->NLayers[i+1] ; j++ )	
		{
      fread(Net->Weights[i][j], sizeof(float), Net->NLayers[i]+1, f);
      /*for( k=0 ; k<=Net->NLayers[i] ; k++ )	
			{
				fscanf(f, "%f", &(Net->Weights[i][j][k]));
			}	*/		
		}
   /* Closing file: */
	fclose(f);
   /* Updating pointers: */
  *myNet = Net;
	return 0;
}
Esempio n. 9
0
NeuronalNet::NeuronalNet(){
	CreateNet();
}
BKPNeuralNet::BKPNeuralNet( int Layers, int *nLayers)
{
	CreateNet( Layers, nLayers);
}