Browse Source

commit before changing mario neural stuff

vlod 1 year ago
parent
commit
83dd0e35c3

+ 8 - 8
Pika/gameplay/containers.h

@@ -22,9 +22,9 @@ Container *getContainer(const char* name, pika::memory::MemoryArena *memoryArena
 #include "pluggins/sushiViewer/sushiViewer.h"
 
 #include "containers/mario/mario.h"
-//#include "containers/mario/marioEditor.h"
-//#include "containers/mario/marioNeuralVizualizer.h"
-//#include "containers/mario/marioNeuralTrainer.h"
+#include "containers/mario/marioEditor.h"
+#include "containers/mario/marioNeuralVizualizer.h"
+#include "containers/mario/marioNeuralTrainer.h"
 #include "containers/minecraftDungeons/mcDungeonsEditor.h"
 #include "containers/minecraftDungeons/mcDungeonsgameplay.h"
 #include "containers/minecraftDungeons/mcDungeonsMenu.h"
@@ -50,7 +50,6 @@ Container *getContainer(const char* name, pika::memory::MemoryArena *memoryArena
 	//PIKA_DECLARE_CONTAINER(McDungeonsMenu) 
 	//PIKA_DECLARE_CONTAINER(Gameplay) 
 
-
 #else
 
 #define PIKA_ALL_CONTAINERS() \
@@ -68,13 +67,14 @@ Container *getContainer(const char* name, pika::memory::MemoryArena *memoryArena
 	PIKA_DECLARE_CONTAINER(Mario) \
 	PIKA_DECLARE_CONTAINER(Holloknight)\
 	PIKA_DECLARE_CONTAINER(Bezie)\
-	PIKA_DECLARE_CONTAINER(IsometricGame)
+	PIKA_DECLARE_CONTAINER(IsometricGame) \
+	PIKA_DECLARE_CONTAINER(MarioEditor) \
+	PIKA_DECLARE_CONTAINER(MarioNeuralTrainer) \
+	PIKA_DECLARE_CONTAINER(MarioNeuralVizualizer) 
 	//PIKA_DECLARE_CONTAINER(McDungeonsGameplay) \
 	//PIKA_DECLARE_CONTAINER(McDungeonsEditor)
 	//PIKA_DECLARE_CONTAINER(McDungeonsMenu)
 
-	//PIKA_DECLARE_CONTAINER(MarioEditor) \
-	//PIKA_DECLARE_CONTAINER(MarioNeuralTrainer) \
-	//PIKA_DECLARE_CONTAINER(MarioNeuralVizualizer) \
+	
 
 #endif

+ 14 - 14
Pika/gameplay/containers/bezie/bezie.h

@@ -214,20 +214,20 @@ struct Bezie : public Container
 			t = renderer3D.getEntityTransform(sphereEntity);
 			float beziePoint = anim;
 
-			//for (int i = 0; i < 100; i++)
-			//{
-			//	auto rez = bezie(pg1, pg2, pg3, pg4, i / 100.f);
-			//
-			//	auto closeness = (rez.x - anim);
-			//
-			//	if (closeness < 0.01)
-			//	{
-			//		beziePoint = rez.y;
-			//		graphPoint = rez;
-			//	}
-			//}
-			beziePoint = bezie(pg1, pg2, pg3, pg4, anim).y;
-			graphPoint = bezie(pg1, pg2, pg3, pg4, anim);
+			for (int i = 0; i < 100; i++)
+			{
+				auto rez = bezie(pg1, pg2, pg3, pg4, i / 100.f);
+			
+				auto closeness = (rez.x - anim);
+			
+				if (closeness < 0.01)
+				{
+					beziePoint = rez.y;
+					graphPoint = rez;
+				}
+			}
+			//beziePoint = bezie(pg1, pg2, pg3, pg4, anim).y;
+			//graphPoint = bezie(pg1, pg2, pg3, pg4, anim);
 
 			t.position = bezie(point1, point2, point3, point4, beziePoint);
 			renderer3D.setEntityTransform(sphereEntity, t);

+ 2 - 2
Pika/gameplay/containers/mario/mario.cpp

@@ -373,7 +373,7 @@ void getVision(char vision[visionSizeX * visionSizeY], mario::GameplaySimulation
 			auto b = simulator.
 				getMapBlockSafe(
 				x + p.p.position.getCenter().x - 1,
-				y + p.p.position.getCenter().y - visionSizeY + 4);
+				y + p.p.position.getCenter().y - visionSizeY + 10);
 
 			vision[x + y * visionSizeX] = b.isCollidable();
 		}
@@ -459,7 +459,7 @@ void renderNeuralNetwork(gl2d::Renderer2D &renderer, char vision[mario::visionSi
 	renderer.renderRectangle(
 		glm::vec4(
 		1,
-		mario::visionSizeY - 4,
+		mario::visionSizeY - 10,
 		PLAYER_SIZE) *
 		(float)blockSizePreview, {0,0,1,0.5});
 

+ 3 - 3
Pika/gameplay/containers/mario/marioCommon.h

@@ -136,7 +136,7 @@ struct GameplaySimulation
 
 		//phisics
 		{
-			player.moveVelocityX(5 * deltaTime * player.input);
+			player.move({18 * deltaTime * player.input, 0});
 
 			player.applyGravity(200.f * deltaTime);
 
@@ -287,8 +287,8 @@ struct GameplayRenderer
 
 };
 
-static constexpr int visionSizeX = 6;
-static constexpr int visionSizeY = 9;
+static constexpr int visionSizeX = 7;
+static constexpr int visionSizeY = 15;
 
 static constexpr int visionTotal = visionSizeX * visionSizeY;
 

+ 86 - 8
Pika/gameplay/containers/mario/marioNeuralTrainer.h

@@ -18,6 +18,7 @@ struct MarioNeuralTrainer: public Container
 	mario::GameplaySimulation simulator;
 	pika::pikaImgui::FileSelector currentMap;
 	pika::pikaImgui::FileSelector rezFile;
+	pika::pikaImgui::FileSelector graphFile;
 
 	//todo user can request imgui ids; shortcut manager context; allocators
 	static ContainerStaticInfo containerInfo()
@@ -64,6 +65,28 @@ struct MarioNeuralTrainer: public Container
 			requestedInfo.writeEntireFileBinary(rezFile.file, &deadSimulations[0].network, sizeof(deadSimulations[0].network));
 		}
 
+		if (graphFile.file[0] != '\0' && save)
+		{
+
+			::pika::memory::pushCustomAllocatorsToStandard();
+			{
+				std::ofstream file(graphFile.file);
+
+				if (file.is_open())
+				{
+					for (auto &g : generations)
+					{
+						file << g.leastFit << " " << g.averageFit << " " << g.fitest << "\n";
+					}
+				};
+
+				file.close();
+			}
+			::pika::memory::popCustomAllocatorsToStandard();
+
+		}
+
+		//we keep the first people unchanged
 		std::vector<SimulationNetwork> fittest;
 		fittest.push_back(deadSimulations[0]);
 		fittest.push_back(deadSimulations[1]);
@@ -79,10 +102,23 @@ struct MarioNeuralTrainer: public Container
 			simulations.push_back(i); 
 		}
 
-		while (simulations.size() < 1000)
+		//we also add some of the less fit people
+		for (int i = 0; i < 20; i++)
 		{
-			int a = mario::getRandomInt(rng, 0, 4);
-			int b = mario::getRandomInt(rng, 0, 4);
+			float a = mario::getRandomFloat(rng, 0, 1);
+			float b = mario::getRandomFloat(rng, 0, 1);
+
+			a *= b; //we bias the people to keep the fitter ones
+
+			fittest.push_back(deadSimulations[5 + a * (deadSimulations.size()/2)]);
+		}
+
+		int size = fittest.size() - 1;
+
+		while (simulations.size() < 10000)
+		{
+			int a = mario::getRandomInt(rng, 0, size);
+			int b = mario::getRandomInt(rng, 0, size);
 
 			SimulationNetwork newNetwork = fittest[a];
 			newNetwork.network.combine(rng, fittest[b].network);
@@ -91,7 +127,7 @@ struct MarioNeuralTrainer: public Container
 			newNetwork.player.p.position.position = {1,1};
 			newNetwork.player.p.lastPos = {1,1};
 
-
+			//combine the 2 individuals
 			if (mario::getRandomChance(rng, 0.25))
 			{
 				int type = mario::getRandomInt(rng, 0, 100);
@@ -133,6 +169,7 @@ struct MarioNeuralTrainer: public Container
 		currentMap.setInfo("Training map", PIKA_RESOURCES_PATH "/mario", {".mario"});
 
 		rezFile.setInfo("Neural network file", PIKA_RESOURCES_PATH "/mario", {".neural"});
+		graphFile.setInfo("Graph File", PIKA_RESOURCES_PATH "/mario", {".txt"});
 
 
 		if (commandLineArgument.size() != 0)
@@ -170,6 +207,14 @@ struct MarioNeuralTrainer: public Container
 		return v - (int)v;
 	}
 
+	struct Generation
+	{
+		float fitest = 0;
+		float leastFit = 0;
+		float averageFit = 0;
+	};
+
+	std::vector<Generation> generations;
 
 	bool update(pika::Input input, pika::WindowState windowState,
 		RequestedContainerInfo &requestedInfo)
@@ -182,9 +227,15 @@ struct MarioNeuralTrainer: public Container
 		float maxFit = 0;
 		for (int i = 0; i < simulations.size(); i++)
 		{
-			if (simulations[i].player.p.position.position.x - simulations[i].player.jumpCount / 10.f > maxFit)
+			//if (simulations[i].player.p.position.position.x - simulations[i].player.jumpCount / 10.f > maxFit)
+			//{
+			//	maxFit = simulations[i].player.p.position.position.x - simulations[i].player.jumpCount/10.f;
+			//	maxIndex = i;
+			//}
+
+			if (simulations[i].player.maxFit > maxFit)
 			{
-				maxFit = simulations[i].player.p.position.position.x - simulations[i].player.jumpCount/10.f;
+				maxFit = simulations[i].player.maxFit;
 				maxIndex = i;
 			}
 		}
@@ -211,7 +262,7 @@ struct MarioNeuralTrainer: public Container
 
 		if (fast)
 		{
-			newDelta = 1.f / 50.f;
+			newDelta = 1.f / 20.f;
 		}
 
 		for (int i = 0; i < simulations.size(); i++)
@@ -231,6 +282,32 @@ struct MarioNeuralTrainer: public Container
 
 		if (simulations.empty()) 
 		{
+
+			Generation result = {};
+
+			result.fitest = 0;
+			result.leastFit = 999999999999;
+			result.averageFit = 0;
+
+			for (auto &g : deadSimulations)
+			{
+				if (g.player.maxFit > result.fitest)
+				{
+					result.fitest = g.player.maxFit;
+				}
+
+				if (g.player.maxFit < result.leastFit)
+				{
+					result.leastFit = g.player.maxFit;
+				}
+
+				result.averageFit += g.player.maxFit;
+			}
+
+			result.averageFit /= deadSimulations.size();
+
+			generations.push_back(result);
+
 			recreateGenerations(requestedInfo);
 		}
 
@@ -260,7 +337,8 @@ struct MarioNeuralTrainer: public Container
 			ImGui::Checkbox("fixed framerate", &fast);
 
 			rezFile.run(2);
-			ImGui::SameLine();
+			graphFile.run(3);
+			//ImGui::SameLine();
 			ImGui::Checkbox("save", &save);
 
 		}

+ 2 - 6
Pika/resources/logs.txt

@@ -1,6 +1,2 @@
-#2024-05-26 17:19:13: Created container: Bezie
-#2024-05-26 17:19:25[warning]: Couldn't reloaded dll
-#2024-05-26 17:19:26: Reloaded dll
-#2024-05-26 17:25:00[warning]: Couldn't reloaded dll
-#2024-05-26 17:25:01: Reloaded dll
-#2024-05-26 17:26:48: Destroyed continer: Bezie #1
+#2024-06-09 13:26:30: Created container: MarioNeuralTrainer
+#2024-06-09 13:35:34: Destroyed continer: MarioNeuralTrainer #1

+ 4 - 2
Pika/resources/mario/postProcess.frag

@@ -23,8 +23,10 @@ void main()
 	newSamplePoint /= 2;
 	color = v_color * texture2D(u_sampler, newSamplePoint);
 
+	
+
 	//color.rgb = vec3(length(color.rgb));
 
-	//color = texture2D(u_sampler, v_texture);
-	//color.a = 1;
+	color = texture2D(u_sampler, v_texture);
+	color.a = 1;
 }

BIN
Pika/resources/mario/rez.neural


BIN
Pika/resources/mario/test1.mario


BIN
Pika/resources/mario/test2.mario