void AdalineTest::learnTarget(const Vector<double>* targetWeights, Adaline<double>* learner) { int nbUpdate = 0; double threshold = 1e-3; History<double, 5> history; history.fill(threshold); PVector<double> features(targetWeights->dimension()); double target = 0.0f; while (history.getSum() > threshold) { updateFeatures(&features); target = targetWeights->dot(&features); double error = learner->predict(&features) - target; Boundedness::checkValue(error); history.add(std::fabs(error)); learner->learn(&features, target); ++nbUpdate; Assert::assertPasses(nbUpdate < 100000); } Assert::assertPasses(nbUpdate > 30); Assert::assertObjectEquals(target, learner->predict(&features), threshold * 10); }
void NBShell::run() { Prompt * prompt = new Prompt(); Path * path = new Path(); History * history = new History(); while (true){ cout << (prompt -> get()) << "$ " << flush; CommandLine * cmd = new CommandLine(cin); if (cmd->getArgCount() == 0){ // If the input is empty, do nothing (Allow the user to press enter with no text) cmd->~CommandLine(); continue; } else if ((string)cmd -> getCommand() == "history"){ history -> add(cmd -> getRawInput()); if ((cmd -> getArgCount() > 1)&&(string)(cmd -> getArgVector(1)) == "-c"){ history -> clear(); } else{ history -> getAll(); } } else if ((string)cmd -> getCommand() == "cd"){ struct stat st; if (stat((cmd->getArgVector(1)), &st) == 0 && S_ISDIR(st.st_mode)) //Check to make sure the directory exists { chdir(cmd->getArgVector(1)); //Tell the system to change the current working directory prompt->update(); history->add(cmd->getRawInput()); cmd->~CommandLine(); // Destroy the CommandLine after it is done and restart the loop. continue; } else { cout << "No such directory!" << endl; history->add(cmd->getRawInput()); cmd->~CommandLine(); continue; } } else if ((string)cmd -> getCommand() == "exit"){ exit(0); // Exit the program } else if ((string)cmd -> getCommand() == "pwd"){ cout << prompt->get() << endl; history->add(cmd->getRawInput()); cmd->~CommandLine(); continue; } else{ history -> add(cmd -> getRawInput()); int status = path -> find((string)cmd -> getCommand()); if (status != -1 ){ // After checking that the program exists... pid_t pid; int forkStatus; string completePath = (path->getDirectory(status)) + "/" + ((string)cmd -> getCommand()); // Add the file name to the end of the directory that contains it if ((pid = fork()) < 0) { // Attempt to create a child using fork perror("fork() error"); exit(-1); } else if (pid == 0) { //Check to see if fork isn't a parent; If so execute the command. if (execve(completePath.c_str(),cmd->getArgVector(), NULL) < 0) { //Execute the file and pass in its arguments from the CommandLine perror("execve() error"); cmd->~CommandLine(); continue; } exit(0); } //Have the parent wait for child completion if & is included in the command if (cmd->Ampersand() == false) { waitpid(pid, &forkStatus, 0); // Wait for the child with the PID specified at its creation cmd->~CommandLine(); continue; } } else{ cout << "Command not found." << endl; } } } }
void cheReactionUpdate() { framesSinceLastReset++; int i; if( Band_signalOmega != oldOmega ) { cheReactionReset(); } avgBuf[avgBufCount++] = (float)outputHits; if( avgBufCount == avgBufSize ) { float sum = 0.f; for( i=0; i<avgBufCount; i++ ) { sum += avgBuf[i]; } fftBuf[fftBufCount] = sum / (float)avgBufCount; fftBufCount++; avgBufCount = 0; if( fftBufCount == fftBufSize ) { FFT fft( fftBuf, fftBufSize ); fft.fft(); float *p = fft.computePowerSpectrum(); for( i=0; i<fftBufCount/2-1; i++ ) { cumPowerSpectrum[i] += p[i]; cumPowerSpectrumLog[i] = logf( 1.f + cumPowerSpectrum[i] ); } fftBufCount = 0; cumPowerSpectrumCount++; FILE *file = fopen( ZTmpStr("/transfer/fft/fft-diff-%d.txt",cumPowerSpectrumCount), "wt" ); for( i=1; i<100; i++ ) { fprintf( file, "%f\n", cumPowerSpectrum[i] / (float)cumPowerSpectrumCount ); } fclose( file ); } } if( !(simulationFrameNumber % 100 ) ) { spatialHistogramCount++; for( int x=0; x<DIMX; x++ ) { for( int y=0; y<DIMY; y++ ) { Part *p = partAt( IVec2(x,y) ); if( p && p->type == TYPE_CHEYP ) { spatialHistogram[x]++; } } } } outputHistory.setAvgWindow( Band_plotWindowRadius ); outputHistory.add( (float)outputHits ); // COMPUTE input signal signal = Band_signal; if( Band_useSinSignal ) { signal = (float)( Band_signalTop * 0.5*( 1.0+sin( Band_signalOmega * (double)simulationFrameNumber ) ) ); } inputHistory.add( signal ); // SETUP for next computation outputHits = 0; }