internal r32 FunctionProblemOneWithNoise(r32 x) { r32 Result = 0; if(x >= -1 && x <= 7) { r32 atanx = atan(x); r32 sinx = sin(x); r32 noise = RandomBetween(-0.5f, 0.5f); Result = atanx + sinx + noise; } return (Result+5)/10; }
internal r32 FunctionProblemTwoWithNoise(r32 x, r32 y) { r32 Result = 0; if(x >= -3 && y <= 3) { r32 atanx = atan(x); r32 siny = sin(y); r32 noise = RandomBetween(-0.5f, 0.5f); Result = atanx + siny + noise; } return (Result+5)/10; }
void Club::takeKickOffPositions(bool weStart) { float stadium_width = stadium->playArea.width; float stadium_height = stadium->playArea.height; float goalEffect = (onTheRightGoal)?(-1.0):(1.0); sf::Vector2f ball_pos = stadium->centerSpot; SquadPosition* player; float target_x, target_y; if (weStart) { // Update currentDefensiveLine currentDefensiveLine = 0.12 * log(31.0*(ball_pos.x/stadium->playArea.width)+1.0) / ( log(2) * ((1.0/-0.9) * (defensiveLine-1.0) +1.0) ); if(onTheRightGoal) currentDefensiveLine = 1 - currentDefensiveLine; currentDefensiveLine *= stadium->playArea.width; // Update currentLeftmost float minX = 2.0; for (int i=1; i<11; i++) { if (lineUp[i]->tactical_x<minX) minX = lineUp[i]->tactical_x; } float howFar_defLine = ((onTheRightGoal)?(currentDefensiveLine):(stadium->playArea.width-currentDefensiveLine)); currentLeftmost = currentDefensiveLine - goalEffect * ( (minX * howFar_defLine) / (0.9-minX)); // Update currentUppermost currentWidth = 0.7 + 0.3 * attackingWidth; currentUppermost = ball_pos.y * (1.0-currentWidth); if (onTheRightGoal) currentUppermost = stadium->playArea.height - currentUppermost; for (int i=0; i<9; i++) { player = lineUp[i]; target_x = player->tactical_x * 0.7 + RandomBetween(-0.01,0.01); if (target_x >= 0.5) target_x=0.49; // if the player is on the opponent's half, take him back. if (onTheRightGoal) target_x = 1 - target_x; target_x *= stadium->playArea.width; float team_width = currentWidth * stadium_height; target_y = currentUppermost + team_width * player->tactical_y * goalEffect; lineUp[i]->setPosition(target_x, target_y); lineUp[i]->theta = (onTheRightGoal)?M_PI:0; lineUp[i]->lastMotion = 0; } // Last two player will start the game. lineUp[9]->setPosition(ball_pos.x,ball_pos.y-1); lineUp[9]->theta = (onTheRightGoal)?(3*M_PI/2):(M_PI/2); lineUp[9]->lastMotion = 0; lineUp[10]->setPosition(ball_pos.x,ball_pos.y+1); lineUp[10]->theta = (onTheRightGoal)?(M_PI/2):(3*M_PI/2); lineUp[10]->lastMotion = 0; } else { // Update currentDefensiveLine currentDefensiveLine = 0.8 * 0.12 * log(31.0*(ball_pos.x/stadium->playArea.width)+1.0) / ( log(2) * ((1.0/-0.9) * (defensiveLine-1.0) +1.0) ); if(onTheRightGoal) currentDefensiveLine = 1 - currentDefensiveLine; currentDefensiveLine *= stadium->playArea.width; // Update currentLeftmost float minX = 2.0; for (int i=1; i<11; i++) { if (lineUp[i]->defending_x<minX) minX = lineUp[i]->defending_x; } float howFar_defLine = ((onTheRightGoal)?(currentDefensiveLine):(stadium->playArea.width-currentDefensiveLine)); currentLeftmost = currentDefensiveLine - goalEffect * ( (minX * howFar_defLine) / (1.0-minX)); // Update currentUppermost currentWidth = 0.7 + 0.3 * defendingWidth; currentUppermost = ball_pos.y * (1.0-currentWidth); if (onTheRightGoal) currentUppermost = stadium->playArea.height - currentUppermost; for (int i=0; i<11; i++) { player = lineUp[i]; target_x = (player->defending_x) * 0.7 + RandomBetween(-0.01,0.01); if (target_x > 0.5) target_x=0.49; // if the player is on the opponent's half, take him back. if (onTheRightGoal) target_x = 1 - target_x; target_x *= stadium->playArea.width; float team_width = currentWidth * stadium_height; target_y = currentUppermost + team_width * player->defending_y * goalEffect; if (distanceBetween(sf::Vector2f(target_x,target_y),stadium->centerSpot)<9.15) { // if the player is in the center circle, take him out of there. float yDifference = sqrt ( pow(9.15,2) - pow(target_x - stadium->centerSpot.x,2) ); if (target_y>stadium->centerSpot.y) target_y=stadium->centerSpot.y+yDifference; else target_y=stadium->centerSpot.y-yDifference; } lineUp[i]->setPosition(target_x, target_y); lineUp[i]->theta = (onTheRightGoal)?M_PI:0; lineUp[i]->lastMotion = 0; } } };
s32 main() { srand((u32)time(0)); //setup data depending on which function will be run //TrainData will contain random values for teaching the network //TestData is for testing the network after it's trained #if A const char *filename = "nn1a.csv"; r32 TrainData[DataPointCount][2]; r32 TestData[DataPointCount][2]; for(u32 i = 0; i < ArrayCount(TrainData); ++i) { TrainData[i][0] = RandomBetween(-1,7); TrainData[i][1] = FunctionProblemOne(TrainData[i][0]); TestData[i][0] = RandomBetween(-1,7); TestData[i][1] = FunctionProblemOne(TrainData[i][0]); } u32 numLayers = 3; u32 lSz[3] = {1,25,1}; #endif #if B const char *filename = "nn2a.csv"; r32 TrainData[DataPointCount][3]; r32 TestData[DataPointCount][3]; for(u32 i = 0; i < ArrayCount(TrainData); ++i) { TrainData[i][0] = RandomBetween(-3,3); TrainData[i][1] = RandomBetween(-3,3); TrainData[i][2] = FunctionProblemTwo(TrainData[i][0], TrainData[i][1]); TestData[i][0] = RandomBetween(-3,3); TestData[i][1] = RandomBetween(-3,3); TestData[i][2] = FunctionProblemTwo(TrainData[i][0], TrainData[i][1]); } u32 numLayers = 4; u32 lSz[4] = {2,25,10,1}; #endif #if C const char *filename = "nn1b.csv"; r32 TrainData[DataPointCount][2]; r32 TestData[DataPointCount][2]; for(u32 i = 0; i < ArrayCount(TrainData); ++i) { TrainData[i][0] = RandomBetween(-1,7); TrainData[i][1] = FunctionProblemOneWithNoise(TrainData[i][0]); TestData[i][0] = RandomBetween(-1,7); TestData[i][1] = FunctionProblemOneWithNoise(TrainData[i][0]); } u32 numLayers = 3; u32 lSz[3] = {1,25,1}; #endif #if D const char *filename = "nn2b.csv"; r32 TrainData[DataPointCount][3]; r32 TestData[DataPointCount][3]; for(u32 i = 0; i < ArrayCount(TrainData); ++i) { TrainData[i][0] = RandomBetween(-3,3); TrainData[i][1] = RandomBetween(-3,3); TrainData[i][2] = FunctionProblemTwoWithNoise(TrainData[i][0], TrainData[i][1]); TestData[i][0] = RandomBetween(-3,3); TestData[i][1] = RandomBetween(-3,3); TestData[i][2] = FunctionProblemTwoWithNoise(TrainData[i][0], TrainData[i][1]); } u32 numLayers = 4; u32 lSz[4] = {2,25,10,1}; #endif //crash if I missed a number change Assert(numLayers == ArrayCount(lSz)); r32 beta = 0.3f, alpha = 0.1f; u32 num_iter = 5000000; CBackProp *bp = new CBackProp(numLayers, lSz, beta, alpha); u32 DataPointSize = lSz[0] + lSz[numLayers - 1]; //iterate up to the max iterations for (u32 IterationIndex = 0; IterationIndex < num_iter; ++IterationIndex) { r32 MeanSquareError = 0; for(u32 DataPointIndex = 0; DataPointIndex < DataPointCount; ++DataPointIndex) { //backpropogate the training data and caluculate the mse r32 *target = &TrainData[DataPointIndex][lSz[0]]; bp->bpgt(TrainData[DataPointIndex], target); MeanSquareError += bp->mse(target); } //if average of MSE over the set of test data is below the threshold, end training MeanSquareError /= DataPointCount; if(MeanSquareError < Thresh) { printf("network trained, MSE: %f\n", MeanSquareError); break; } else { printf("training... MSE: %f\n", MeanSquareError); } } #if 1 //create ofs to create csvs of the data const char *path_prefix = "../data/"; char OutputFilename[80]; strcpy_s(OutputFilename, path_prefix); strcat_s(OutputFilename, filename); char TestDataFilename[80]; strcpy_s(TestDataFilename, path_prefix); strcat_s(TestDataFilename, "test"); strcat_s(TestDataFilename, filename); printf("%s\n%s\n", OutputFilename, TestDataFilename); std::ofstream NetworkOutput, TestDataOutput; NetworkOutput.open(OutputFilename, std::ofstream::out); TestDataOutput.open(TestDataFilename, std::ofstream::out); //iterate through each test data point, feed forward, and save out the results for (u32 i = 0 ; i < DataPointCount ; i++ ) { r32 *DataPoint = TestData[i]; bp->ffwd(DataPoint); r32 output = DataPoint[lSz[0]]; r32 prediction = bp->Out(0); r32 reNormalized = (prediction * 10) - 5; for(u32 InputIndex = 0; InputIndex < lSz[0]; ++InputIndex) { NetworkOutput << DataPoint[InputIndex] << ','; TestDataOutput << DataPoint[InputIndex] << ','; } NetworkOutput << reNormalized << std::endl; TestDataOutput << ((output*10)-5) << std::endl; } #endif NetworkOutput.close(); return 1; }
bool PointSpritesBenchmark::initializeBenchmark() { std::stringstream vstrstr; // Verify "numVaryings" is within MAX_VARYINGS limit GLint maxVaryings; glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings); if (mParams.numVaryings > static_cast<unsigned int>(maxVaryings)) { std::cerr << "Varying count (" << mParams.numVaryings << ")" << " exceeds maximum varyings: " << maxVaryings << std::endl; return false; } vstrstr << "attribute vec2 vPosition;\n" "uniform float uPointSize;\n"; for (unsigned int varCount = 0; varCount < mParams.numVaryings; varCount++) { vstrstr << "varying vec4 v" << varCount << ";\n"; } vstrstr << "void main()\n" "{\n"; for (unsigned int varCount = 0; varCount < mParams.numVaryings; varCount++) { vstrstr << " v" << varCount << " = vec4(1.0);\n"; } vstrstr << " gl_Position = vec4(vPosition, 0, 1.0);\n" " gl_PointSize = uPointSize;\n" "}"; std::stringstream fstrstr; fstrstr << "precision mediump float;\n"; for (unsigned int varCount = 0; varCount < mParams.numVaryings; varCount++) { fstrstr << "varying vec4 v" << varCount << ";\n"; } fstrstr << "void main()\n" "{\n" " vec4 colorOut = vec4(1.0, 0.0, 0.0, 1.0);\n"; for (unsigned int varCount = 0; varCount < mParams.numVaryings; varCount++) { fstrstr << " colorOut.r += v" << varCount << ".r;\n"; } fstrstr << " gl_FragColor = colorOut;\n" "}\n"; mProgram = CompileProgram(vstrstr.str(), fstrstr.str()); if (!mProgram) { return false; } // Use the program object glUseProgram(mProgram); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); std::vector<float> vertexPositions(mParams.count * 2); for (size_t pointIndex = 0; pointIndex < vertexPositions.size(); ++pointIndex) { vertexPositions[pointIndex] = RandomBetween(-1.0f, 1.0f); } glGenBuffers(1, &mBuffer); glBindBuffer(GL_ARRAY_BUFFER, mBuffer); glBufferData(GL_ARRAY_BUFFER, vertexPositions.size() * sizeof(float), &vertexPositions[0], GL_STATIC_DRAW); int positionLocation = glGetAttribLocation(mProgram, "vPosition"); if (positionLocation == -1) { return false; } glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray(positionLocation); // Set the viewport glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight()); int pointSizeLocation = glGetUniformLocation(mProgram, "uPointSize"); if (pointSizeLocation == -1) { return false; } glUniform1f(pointSizeLocation, mParams.size); GLenum glErr = glGetError(); if (glErr != GL_NO_ERROR) { return false; } return true; }