forked from tsavery/GAdeckbuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
125 lines (117 loc) · 4.9 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.SQLite;
using GeneticSharp.Domain.Chromosomes;
using GeneticSharp.Domain.Crossovers;
using GeneticSharp.Domain.Mutations;
using GeneticSharp.Domain.Populations;
using GeneticSharp.Domain.Selections;
using GeneticSharp.Domain.Terminations;
using GeneticSharp.Domain.Randomizations;
using GeneticSharp.Domain;
namespace GAdeckbuilder
{
class Program
{
static public List<Card> CardPool = new List<Card>();
static void Main(string[] args)
{
SQLiteConnection dbConnect;
SQLiteCommand cmd;
SQLiteDataReader dbread = null;
string inputstring;
string cmc = ""; string power = "";
StreamReader inputReader = new StreamReader("input.txt");
dbConnect = new SQLiteConnection("Data Source=C://Users/Korik/Documents/Visual Studio 2015/Projects/GAdeckbuilder/GAdeckbuilder/bin/Debug/mtg.db;");
dbConnect.Open();
string sql;
bool white = false, blue = false, black = false, red = false, green = false;
while (inputReader.Peek() != -1)
{
inputstring = inputReader.ReadLine();
sql = "SELECT * FROM cards INNER JOIN colors ON cards.name = colors.name where cards.setcode = 'BFZ' AND colors.setcode = 'BFZ' AND cards.name = '" + inputstring + "';";
// Console.WriteLine(sql);
cmd = new SQLiteCommand(sql, dbConnect);
dbread = cmd.ExecuteReader();
while (dbread.Read())
{
//Console.WriteLine(dbread[1].ToString());
white = false; blue = false; black = false; red = false; green = false;
if (Convert.ToBoolean(dbread["white"].ToString()))
{
white = true;
}
if (Convert.ToBoolean(dbread["blue"].ToString()))
{
blue = true;
}
if (Convert.ToBoolean(dbread["black"].ToString()))
{
black = true;
}
if (Convert.ToBoolean(dbread["red"].ToString()))
{
red = true;
}
if (Convert.ToBoolean(dbread["green"].ToString()))
{
green = true;
}
// Console.WriteLine(dbread["cmc"].ToString());
cmc = dbread["cmc"].ToString();
//Console.WriteLine(cmc);
power = dbread["PowerLevel"].ToString();
//Console.WriteLine(power);
}
// Console.WriteLine(inputstring + white + blue + red + black + green + cmc + power);
CardPool.Add(new Card(inputstring, white, blue, black, red, green, int.Parse(cmc), int.Parse(power)));
}
inputReader.Close();
dbread.Close();
dbConnect.Close();
foreach (Card c in CardPool)
{
// Console.WriteLine(c.White);
}
Console.ReadLine();
var selection = new EliteSelection();
var crossover = new OnePointCrossover();
var mutation = new ReverseSequenceMutation();
var fitness = new DeckFitness();
var chromosome = new DeckChromosome();
var population = new Population(400, 400, chromosome);
var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation);
ga.Termination = new FitnessStagnationTermination(4000);
ga.CrossoverProbability = 0.80f;
ga.MutationProbability = 1.00f;
Console.WriteLine("GA running...");
ga.Start();
printDecklist(ga.BestChromosome);
Console.WriteLine("Number of Generations " + ga.GenerationsNumber);
Console.ReadLine();
Console.ReadLine();
}
static void printDecklist(IChromosome chromosome)
{
int cardcount = 0;
int power = 0;
int cmc = 0;
for (int i = 0; i < CardPool.Count; i++)
{
if (chromosome.GetGenes()[i].Value.Equals(1))
{
var currentcard = Program.CardPool[i];
cardcount++;
power += currentcard.PowerLevel;
Console.WriteLine(CardPool[i].toString());
cmc += currentcard.ConvertedManaCost;
}
}
Console.WriteLine("Card count: {0} Total Power: {1} Fitness {2}, Average CMC: {3}", cardcount, power, chromosome.Fitness, cmc/cardcount);
}
}
}