public void ScalarMultiplication() { IVector <DoubleComponent> ScalarMultiplicationArgument = MatrixFactory <DoubleComponent> .CreateVector3D(5.0f, 4.0f, 3.0f); Assert.IsTrue(ScalarMultiplicationArgument.Multiply(-.5) .Equals( MatrixFactory <DoubleComponent> .CreateVector3D(2.5f, 2.0f, 1.5f).Negative()) ); Assert.IsTrue( ScalarMultiplicationArgument.Multiply(-.5) .Equals( MatrixFactory <DoubleComponent> .CreateVector3D(2.5f, 2.0f, 1.5f).Negative()) ); Assert.IsTrue( ScalarMultiplicationArgument.Multiply(5) .Equals( MatrixFactory <DoubleComponent> .CreateVector3D(25.0f, 20.0f, 15.0f)) ); IVector <DoubleComponent> Point3 = MatrixFactory <DoubleComponent> .CreateVector3D(2, 3, 4); Point3.MultiplyEquals(6); Assert.IsTrue(IComparable <IVector <DoubleComponent> > .Equals(Point3, MatrixFactory <DoubleComponent> .CreateVector3D(12, 18, 24))); }
public void Normalize() { IVector <DoubleComponent> Point3 = MatrixFactory <DoubleComponent> .CreateVector3D(3, -4, 5); Point3 = Point3.Normalize(); Assert.IsTrue(Point3.GetMagnitude().GreaterThan(0.99) && Point3.GetMagnitude().LessThan(1.01)); }
public void DotProduct() { IVector <DoubleComponent> Test1 = MatrixFactory <DoubleComponent> .CreateVector3D(10, 1, 2); IVector <DoubleComponent> Test2 = MatrixFactory <DoubleComponent> .CreateVector3D(1, 0, 0); DoubleComponent DotResult = Test2.Dot(Test1); Assert.IsTrue(DotResult.Equals(10)); }
public static void glRotatef(double AngleDegrees, T AxisX, T AxisY, T AxisZ) { //if (s_BeginMode != 0) throw new System.FormatException("You cannot call this inside a Begin - End section."); //Vector3D Axis = new Vector3D(AxisX, AxisY, AxisZ); //Matrix4X4 Matrix = new Matrix4X4(); //double AngleRadians = Helper.DegToRad(AngleDegrees); //Matrix.Rotate(Axis, AngleRadians); //glMultMatrixf(Matrix.GetElements()); m_CurEditingMatrix.RotateAlong(MatrixFactory <T> .CreateVector3D(AxisX, AxisY, AxisZ), Helper.DegToRad(AngleDegrees)); }
public void CrossProduct() { IVector <DoubleComponent> Test1 = MatrixFactory <DoubleComponent> .CreateVector3D(10, 0, 0); IVector <DoubleComponent> Test2 = MatrixFactory <DoubleComponent> .CreateVector3D(1, 1, 0); IVector <DoubleComponent> CrossResult = Test2.Cross(Test1); Assert.IsTrue(CrossResult[0].Equals(0)); Assert.IsTrue(CrossResult[1].Equals(0)); Assert.IsTrue(CrossResult[2].LessThan(0)); }
public static void glTranslatef(T x, T y, T z) { //if (s_BeginMode != 0) throw new System.FormatException("You cannot call this inside a Begin - End section."); //double[] NewMatrix = //{ // 1.0f, 0.0f, 0.0f, 0.0f, // 0.0f, 1.0f, 0.0f, 0.0f, // 0.0f, 0.0f, 1.0f, 0.0f, // x, y, z, 1.0f, //}; //glMultMatrixf(NewMatrix); m_CurEditingMatrix.Translate(MatrixFactory <T> .CreateVector3D(x, y, z)); }
public void Reset() { Random Rand = new Random(); //reset the sweepers positions m_vPosition = MatrixFactory <T> .CreateVector3D(M.New <T>((Rand.NextDouble() * m_WindowWidth)), M.New <T>(Rand.NextDouble() * m_WindowHeight), M.Zero <T>()); //and the fitness m_dFitness = 0; //and the rotation m_dRotation = Rand.NextDouble() * (System.Math.PI * 2); return; }
//used to Transform the sweepers vertices prior to rendering public void WorldTransform(List <IVector <T> > sweeper) { ////create the world transformation matrix //Matrix4X4 matTransform = new Matrix4X4(); ////scale //matTransform.Scale(m_dScale, m_dScale, 1); ////rotate //matTransform.AddRotate(2, (double)m_dRotation); ////and translate //matTransform.AddTranslate(m_vPosition.x, m_vPosition.y, 0); ////now Transform the ships vertices //for (int i = 0; i < sweeper.Count; i++) //{ // Vector3D Temp = sweeper[i]; // matTransform.TransformVector(ref Temp); // sweeper[i] = Temp; //} //create the world transformation matrix IAffineTransformMatrix <T> matTransform = MatrixFactory <T> .NewIdentity(VectorDimension.Three); //scale matTransform.Scale(MatrixFactory <T> .CreateVector3D(m_dScale, m_dScale, M.One <T>())); //rotate //matTransform.AddRotate(2, (double)m_dRotation); IVector <T> axis = null;/// need to work out which axis this should be matTransform.RotateAlong(axis, m_dRotation); //and translate matTransform.Translate(MatrixFactory <T> .CreateVector3D(m_vPosition[0], m_vPosition[1], M.Zero <T>())); //now Transform the ships vertices for (int i = 0; i < sweeper.Count; i++) { IVector <T> Temp = sweeper[i]; Temp = matTransform.TransformVector(Temp); sweeper[i] = Temp; } }
public void VectorAdditionAndSubtraction() { IVector <DoubleComponent> Point1 = MatrixFactory <DoubleComponent> .CreateVector3D(1, 1, 1); //new Vector3D(); //Point1.Set(1, 1, 1); IVector <DoubleComponent> Point2 = MatrixFactory <DoubleComponent> .CreateVector3D(2, 2, 2); //Point2.Set(2, 2, 2); IVector <DoubleComponent> Point3;; Point3 = Point1.Add(Point2); Assert.IsTrue(Point3.Equals(MatrixFactory <DoubleComponent> .CreateVector3D(3, 3, 3))); Point3 = Point1.Subtract(Point2); Assert.IsTrue(Point3.Equals(MatrixFactory <DoubleComponent> .CreateVector3D(-1, -1, -1))); Point3.AddEquals(Point1); Assert.IsTrue(Point3.Equals(MatrixFactory <DoubleComponent> .CreateVector3D(0, 0, 0))); Point3.AddEquals(Point2); Assert.IsTrue(Point3.Equals(MatrixFactory <DoubleComponent> .CreateVector3D(2, 2, 2))); Point3.SetFrom(MatrixFactory <DoubleComponent> .CreateVector3D(3, -4, 5)); Assert.IsTrue(Point3.GetMagnitude().GreaterThan(7.07) && Point3.GetMagnitude().LessThan(7.08)); IVector <DoubleComponent> InlineOpLeftSide = MatrixFactory <DoubleComponent> .CreateVector3D(5.0f, -3.0f, .0f); IVector <DoubleComponent> InlineOpRightSide = MatrixFactory <DoubleComponent> .CreateVector3D(-5.0f, 4.0f, 1.0f); Assert.IsTrue( InlineOpLeftSide.Add(InlineOpRightSide) .Equals( MatrixFactory <DoubleComponent> .CreateVector3D(.0f, 1.0f, 1.0f) )); Assert.IsTrue( InlineOpLeftSide.Subtract(InlineOpRightSide) .Equals( MatrixFactory <DoubleComponent> .CreateVector3D(10.0f, -7.0f, -1.0f)) ); }
public CMinesweeper(int WindowWidth, int WindowHeight, T SweeperScale, double dMaxTurnRate) { m_ItsBrain = new CNeuralNet(4, 2, 1, 6, -1, 1); //m_ItsBrain = new CNeuralNet(4, 2, 3, 8, -1, 1); Random Rand = new Random(); m_WindowWidth = WindowWidth; m_WindowHeight = WindowHeight; m_dMaxTurnRate = dMaxTurnRate; m_dRotation = (Rand.NextDouble() * (System.Math.PI * 2)); m_lTrack = (0.16); m_rTrack = (0.16); m_dFitness = (0); m_dScale = SweeperScale; m_iClosestMine = (0); //create a random start position m_vPosition = MatrixFactory <T> .CreateVector3D(M.New <T>(Rand.NextDouble() * WindowWidth), M.New <T>(Rand.NextDouble() * WindowHeight), M.One <T>()); m_vLookAt = MatrixFactory <T> .CreateVector3D(M.New <T>(Rand.NextDouble() * WindowWidth), M.New <T>(Rand.NextDouble() * WindowHeight), M.One <T>()); }
public static void glVertex3f(T x, T y, T z) { IVector <T> WorldPoint = MatrixFactory <T> .CreateVector3D(x, y, z); WorldPoint = m_CurAccumulatedMatrix.TransformVector(WorldPoint); T OneOverZ = M.One <T>().Divide(WorldPoint[2]); WorldPoint[0] = WorldPoint[0].Multiply(OneOverZ).Add(1).Multiply(s_GLViewport.X2 * .5f).Add(s_GLViewport.X1); WorldPoint[1] = WorldPoint[1].Multiply(OneOverZ).Add(1).Multiply(s_GLViewport.Y2 * .5f).Add(s_GLViewport.Y1); m_pVertexCache[m_NumCachedVertex].m_Position = WorldPoint; m_pVertexCache[m_NumCachedVertex].m_Color = m_LastSetColor; m_pVertexCache[m_NumCachedVertex].m_Normal = m_LastSetNormal; m_pVertexCache[m_NumCachedVertex].m_TextureCoordinate = m_LastSetTextureCoordinate; m_NumCachedVertex++; //double Dilation = 0; T Dilation = M.New <T>(.175); switch (s_BeginMode) { /* * case GL_POINTS: * { * RenderLine(m_pVertexCache[0].m_Position, m_pVertexCache[0].m_Position, m_LastSetColor); * m_NumCachedVertex3fs = 0; * } * break; * * case LINES: * { * if(m_NumCachedVertex3fs == 2) * { * // <WIP> use both vertex colors LBB [1/14/2002] * RenderLine(m_pVertexCache[0].m_Position, m_pVertexCache[1].m_Position, m_pVertexCache[0].m_Color); * m_NumCachedVertex3fs = 0; * } * } * break; */ case GL_TRIANGLES: if (m_NumCachedVertex == 3) { IVector <T> Winding = VectorUtilities <T> .Cross( m_pVertexCache[1].m_Position.Subtract(m_pVertexCache[0].m_Position), m_pVertexCache[2].m_Position.Subtract(m_pVertexCache[0].m_Position)); if (Winding[2].GreaterThan(0)) { s_GouraudSpanGen.Colors(m_pVertexCache[0].m_Color, m_pVertexCache[1].m_Color, m_pVertexCache[2].m_Color); s_GouraudSpanGen.Triangle(m_pVertexCache[0].m_Position[0], m_pVertexCache[0].m_Position[1], m_pVertexCache[1].m_Position[0], m_pVertexCache[1].m_Position[1], m_pVertexCache[2].m_Position[0], m_pVertexCache[2].m_Position[1], Dilation); s_Rasterizer.AddPath(s_GouraudSpanGen); //s_Rasterizer.gamma(new gamma_linear(0.0, 0.809)); //renderer_scanlines.render_scanlines_aa_solid(ras, sl, ren_base, m_pVertexCache[0].m_Color.Get_rgba8()); Renderer <T> .GenerateAndRender(s_Rasterizer, s_ScanlineUnpacked, s_ClippingPixelFormatProxy, s_SpanAllocator, s_GouraudSpanGen); } m_NumCachedVertex = 0; } break; case GL_QUADS: if (m_NumCachedVertex == 4) { IVector <T> Winding = VectorUtilities <T> .Cross( m_pVertexCache[1].m_Position.Subtract(m_pVertexCache[0].m_Position), m_pVertexCache[2].m_Position.Subtract(m_pVertexCache[0].m_Position)); if (Winding[2].GreaterThan(0)) { switch (s_ShadeModel) { case GL_FLAT: // this can be faster if we don't try to shade it (no span generator) s_SharedPathStorage.RemoveAll(); s_SharedPathStorage.MoveTo(m_pVertexCache[0].m_Position[0], m_pVertexCache[0].m_Position[1]); s_SharedPathStorage.LineTo(m_pVertexCache[1].m_Position[0], m_pVertexCache[1].m_Position[1]); s_SharedPathStorage.LineTo(m_pVertexCache[2].m_Position[0], m_pVertexCache[2].m_Position[1]); s_SharedPathStorage.LineTo(m_pVertexCache[3].m_Position[0], m_pVertexCache[3].m_Position[1]); s_Rasterizer.AddPath(s_SharedPathStorage); Renderer <T> .RenderSolid(s_ClippingPixelFormatProxy, s_Rasterizer, s_ScanlineUnpacked, m_pVertexCache[0].m_Color.GetAsRGBA_Bytes()); break; case GL_SMOOTH: s_GouraudSpanGen.Colors( m_pVertexCache[0].m_Color, m_pVertexCache[1].m_Color, m_pVertexCache[2].m_Color); s_GouraudSpanGen.Triangle( m_pVertexCache[0].m_Position[0], m_pVertexCache[0].m_Position[1], m_pVertexCache[1].m_Position[0], m_pVertexCache[1].m_Position[1], m_pVertexCache[2].m_Position[0], m_pVertexCache[2].m_Position[1], Dilation); s_Rasterizer.AddPath(s_GouraudSpanGen); //s_Rasterizer.gamma(new gamma_linear(0.0, 0.809)); Renderer <T> .RenderSolid(s_ClippingPixelFormatProxy, s_Rasterizer, s_ScanlineUnpacked, m_pVertexCache[0].m_Color.GetAsRGBA_Bytes()); s_GouraudSpanGen.Colors( m_pVertexCache[0].m_Color, m_pVertexCache[2].m_Color, m_pVertexCache[3].m_Color); s_GouraudSpanGen.Triangle( m_pVertexCache[0].m_Position[0], m_pVertexCache[0].m_Position[1], m_pVertexCache[2].m_Position[0], m_pVertexCache[2].m_Position[1], m_pVertexCache[3].m_Position[0], m_pVertexCache[3].m_Position[1], M.Zero <T>()); s_Rasterizer.AddPath(s_GouraudSpanGen); Renderer <T> .RenderSolid(s_ClippingPixelFormatProxy, s_Rasterizer, s_ScanlineUnpacked, m_pVertexCache[0].m_Color.GetAsRGBA_Bytes()); break; default: throw new System.NotImplementedException(); } } m_NumCachedVertex = 0; } break; default: // Be sure to call Begin before you pass Vertexes. LBB [1/12/2002] throw new System.NotImplementedException("You do not have a mode set or you have an invalid mode. LBB [1/12/2002]"); } }
public bool Update() { //run the sweepers through NumTicks amount of cycles. During //this loop each sweepers NN is constantly updated with the appropriate //information from its surroundings. The output from the NN is obtained //and the sweeper is moved. If it encounters a mine its fitness is //updated appropriately, int NumSweepers = m_vecSweepers.Count; if (m_TicksThisGeneration++ < m_NumTicksPerGeneration) { for (int i = 0; i < NumSweepers; ++i) { //update the NN and position if (!m_vecSweepers[i].Update(m_vecMines)) { //error in processing the neural net //MessageBox(m_hwndMain, "Wrong amount of NN inputs!", "Error", MB_OK); return(false); } //see if it's found a mine int GrabHit = m_vecSweepers[i].CheckForMine(m_vecMines, m_MineScale); if (GrabHit >= 0) { Random Rand = new Random(); //we have discovered a mine so increase fitness m_vecSweepers[i].IncrementFitness(); //mine found so replace the mine with another at a random //position m_vecMines[GrabHit] = MatrixFactory <T> .CreateVector3D( M.New <T>(Rand.NextDouble() * cxClient), M.New <T>(Rand.NextDouble() * cyClient), M.Zero <T>()); } //update the chromos fitness score m_vecThePopulation[i].Fitness = m_vecSweepers[i].Fitness(); } } //Another generation has been completed. //Time to run the GA and update the sweepers with their new NNs else { //update the stats to be used in our stat window m_vecAvFitness.Add(m_pGA.AverageFitness); m_vecBestFitness.Add(m_pGA.BestFitness); //increment the generation counter ++m_iGenerations; //reset cycles m_TicksThisGeneration = 0; //run the GA to create a new population m_vecThePopulation = m_pGA.Epoch(m_vecThePopulation); //insert the new (hopefully)improved brains back into the sweepers //and reset their positions etc for (int i = 0; i < NumSweepers; ++i) { m_vecSweepers[i].PutWeights(m_vecThePopulation[i].Weights); m_vecSweepers[i].Reset(); } } return(true); }
public CController(RasterBuffer hwndMain, int iNumSweepers, int iNumMines, double _dMutationRate, double _dCrossoverRate, double dMaxPerturbation, int NumElite, int NumCopiesElite, int NumTicksPerGeneration) { Random Rand = new Random(); m_MineScale = M.One <T>(); m_SweeperScale = M.New <T>(3); m_pGA = null; m_bFastRender = (false); m_TicksThisGeneration = 0; m_NumTicksPerGeneration = NumTicksPerGeneration; m_hwndMain = hwndMain; m_iGenerations = (0); cxClient = (int)hwndMain.Width; cyClient = (int)hwndMain.Height; dMutationRate = _dMutationRate; dCrossoverRate = _dCrossoverRate; //let's create the mine sweepers for (int i = 0; i < iNumSweepers; ++i) { m_vecSweepers.Add(new CMinesweeper <T>((int)hwndMain.Width, (int)hwndMain.Height, m_SweeperScale, .3)); } //get the total number of weights used in the sweepers //NN so we can initialise the GA m_NumWeightsInNN = m_vecSweepers[0].GetNumberOfWeights(); //initialize the Genetic Algorithm class m_pGA = new CGenAlg(iNumSweepers, dMutationRate, dCrossoverRate, m_NumWeightsInNN, dMaxPerturbation, NumElite, NumCopiesElite); //Get the weights from the GA and insert into the sweepers brains m_vecThePopulation = m_pGA.GetChromos(); for (int i = 0; i < iNumSweepers; i++) { m_vecSweepers[i].PutWeights(m_vecThePopulation[i].Weights); } //initialize mines in random positions within the application window for (int i = 0; i < iNumMines; ++i) { m_vecMines.Add(MatrixFactory <T> .CreateVector3D( M.New <T>(Rand.NextDouble() * cxClient), M.New <T>(Rand.NextDouble() * cyClient), M.Zero <T>())); } //create a pen for the graph drawing m_BlackPen = new RGBA_Bytes(0, 0, 0, 255); m_BluePen = new RGBA_Bytes(0, 0, 255); m_RedPen = new RGBA_Bytes(255, 0, 0); m_GreenPen = new RGBA_Bytes(0, 150, 0); //fill the vertex buffers for (int i = 0; i < sweeper.Length; ++i) { m_SweeperVB.Add(sweeper[i]); } for (int i = 0; i < mine.Length; ++i) { m_MineVB.Add(mine[i]); } }