فهرست منبع

[programming challenge, SSN] some restructurization, initial usage of FANN

mateuszb 13 سال پیش
والد
کامیت
d176153ac2
2فایلهای تغییر یافته به همراه106 افزوده شده و 24 حذف شده
  1. 6 4
      Odpalarka/Odpalarka.vcxproj
  2. 100 20
      Odpalarka/main.cpp

+ 6 - 4
Odpalarka/Odpalarka.vcxproj

@@ -68,7 +68,8 @@
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
     <OutDir>$(SolutionDir)</OutDir>
     <IntDir>$(Configuration)\</IntDir>
-    <LibraryPath>$(SolutionDir);$(LibraryPath)</LibraryPath>
+    <LibraryPath>$(SolutionDir);$(LibraryPath);G:\boost\FANN-2.2.0-Source\bin</LibraryPath>
+    <IncludePath>G:\boost\FANN-2.2.0-Source\src\include;$(IncludePath)</IncludePath>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
     <OutDir>$(SolutionDir)</OutDir>
@@ -77,7 +78,8 @@
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='RD|Win32'">
     <OutDir>$(SolutionDir)$(Configuration)\bin\</OutDir>
     <IntDir>$(Configuration)\</IntDir>
-    <LibraryPath>$(SolutionDir)$(SolutionDir)..\libs;\$(PlatformShortName);$(LibraryPath)</LibraryPath>
+    <LibraryPath>$(SolutionDir)$(SolutionDir)..\libs;\$(PlatformShortName);$(LibraryPath);G:\boost\FANN-2.2.0-Source\bin</LibraryPath>
+    <IncludePath>G:\boost\FANN-2.2.0-Source\src\include;$(IncludePath)</IncludePath>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='RD|x64'">
     <OutDir>$(SolutionDir)$(Configuration)\bin\</OutDir>
@@ -92,7 +94,7 @@
     </ClCompile>
     <Link>
       <GenerateDebugInformation>true</GenerateDebugInformation>
-      <AdditionalDependencies>VCMI_lib.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>VCMI_lib.lib;fannfloat.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@@ -120,7 +122,7 @@
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
-      <AdditionalDependencies>VCMI_lib.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>VCMI_lib.lib;fannfloat.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='RD|x64'">

+ 100 - 20
Odpalarka/main.cpp

@@ -3,6 +3,11 @@
 #include "../lib/VCMI_Lib.h"
 namespace po = boost::program_options;
 
+
+//FANN
+#include <floatfann.h>
+#include <fann_cpp.h>
+
 std::string leftAI, rightAI, battle, results, logsDir;
 bool withVisualization = false;
 std::string servername;
@@ -70,8 +75,37 @@ double playBattle(const DuelParameters &dp)
 	return code / 1000000.0;
 }
 
-void SSNRun()
+typedef std::map<int, CArtifactInstance*> TArtSet;
+
+double cmpArtSets(TArtSet setL, TArtSet setR)
 {
+	DuelParameters dp;
+	dp.bfieldType = 1;
+	dp.terType = 1;
+
+	for(int i = 0; i < 2 ; i++)
+	{
+		auto &side = dp.sides[i];
+		side.heroId = i;
+		side.heroPrimSkills.resize(4,0);
+		side.stacks[0] = DuelParameters::SideSettings::StackSettings(10+i, 40+i);
+	}
+
+	//lewa strona z art 0.9
+	//bez artefaktow -0.41
+	//prawa strona z art. -0.926
+
+	dp.sides[0].artifacts = setL;
+	dp.sides[1].artifacts = setR;
+
+	auto battleOutcome = playBattle(dp);
+	return battleOutcome;
+}
+
+std::vector<CArtifactInstance*> genArts()
+{
+	std::vector<CArtifactInstance*> ret;
+
 	CArtifact *nowy = new CArtifact();
 	nowy->description = "Cudowny miecz Towa gwarantuje zwyciestwo";
 	nowy->name = "Cudowny miecz";
@@ -85,35 +119,81 @@ void SSNRun()
 	artinst->addNewBonus(new Bonus(Bonus::PERMANENT, Bonus::PRIMARY_SKILL, Bonus::ARTIFACT_INSTANCE, +25, nowy->id, PrimarySkill::ATTACK));
 	artinst->addNewBonus(new Bonus(Bonus::PERMANENT, Bonus::PRIMARY_SKILL, Bonus::ARTIFACT_INSTANCE, +25, nowy->id, PrimarySkill::DEFENSE));
 
-	DuelParameters dp;
-	dp.bfieldType = 1;
-	dp.terType = 1;
-
-	for(int i = 0; i < 2 ; i++)
-	{
-		auto &side = dp.sides[i];
-		side.heroId = i;
-		side.heroPrimSkills.resize(4,0);
-		side.stacks[0] = DuelParameters::SideSettings::StackSettings(10+i, 40+i);
-	}
-
 	auto bonuses = artinst->getBonuses([](const Bonus *){ return true; });
 	BOOST_FOREACH(Bonus *b, *bonuses) 
 	{
 		std::cout << format("%s (%d) value:%d, description: %s\n") % bonusTypeToString(b->type) % b->subtype % b->val % b->Description();
 	}
 
-	
+	return ret;
+}
 
 
-	//lewa strona z art 0.9
-	//bez artefaktow -0.41
-	//prawa strona z art. -0.926
+//returns how good the artifact is for the neural network
+double runSSN(FANN::neural_net & net, CArtifactInstance * inst)
+{
 
-	dp.sides[0].artifacts[Arts::LEFT_HAND] = artinst;
+	return 0.0;
+}
 
-	auto battleOutcome = playBattle(dp);
-	int g = 4;
+void initNet(FANN::neural_net & ret)
+{
+	const float learning_rate = 0.7f;
+	const unsigned int num_layers = 3;
+	const unsigned int num_input = 2;
+	const unsigned int num_hidden = 3;
+	const unsigned int num_output = 1;
+	const float desired_error = 0.001f;
+	const unsigned int max_iterations = 300000;
+	const unsigned int iterations_between_reports = 1000;
+
+	ret.create_standard(num_layers, num_input, num_hidden, num_output);
+
+	ret.set_learning_rate(learning_rate);
+
+	ret.set_activation_steepness_hidden(1.0);
+	ret.set_activation_steepness_output(1.0);
+
+	ret.set_activation_function_hidden(FANN::SIGMOID_SYMMETRIC_STEPWISE);
+	ret.set_activation_function_output(FANN::SIGMOID_SYMMETRIC_STEPWISE);
+
+	ret.randomize_weights(0.0, 1.0);
+}
+
+void SSNRun()
+{
+	auto availableArts = genArts();
+	std::vector<std::pair<CArtifactInstance *, double> > artNotes;
+
+	TArtSet setL, setR;
+
+	FANN::neural_net network;
+	initNet(network);
+
+	for(int i=0; i<availableArts.size(); ++i)
+	{
+		artNotes.push_back(std::make_pair(availableArts[i], runSSN(network, availableArts[i])));
+	}
+	boost::range::sort(artNotes,
+		[](const std::pair<CArtifactInstance *, double> & a1, const std::pair<CArtifactInstance *, double> & a2)
+		{return a1.second > a2.second;});
+
+	//pick best arts into setL
+	BOOST_FOREACH(auto & ap, artNotes)
+	{
+		auto art = ap.first;
+		BOOST_FOREACH(auto slot, art->artType->possibleSlots)
+		{
+			if(setL.find(slot) != setL.end())
+			{
+				setL[slot] = art;
+				break;
+			}
+		}
+	}
+
+	//evaluate
+	double result = cmpArtSets(setL, setR);
 }
 
 int main(int argc, char **argv)