public void Test_That_Knapsack_Load_Works() { Knapsack knapsack = new Knapsack(); knapsack.Load("Data/Knapsacks/json/mknapcb1/mknapcb1-1.json"); Assert.AreNotEqual(knapsack.weights.Count, 0); Console.WriteLine(JsonConvert.SerializeObject(knapsack, Formatting.Indented)); }
public void Test_That_Knapsack_Hill_Climbing_Works() { Knapsack knapsack = new Knapsack(); knapsack.Load(Constants.SAMPLE_MKNAPCB4_DATASET); HillClimb <List <int> > hillClimb = new HillClimb <List <int> >(); hillClimb.create(knapsack.getConfiguration()); List <int> finalSolution = hillClimb.fullIteration(); }
public void Test_That_Knapsack_Get_Initial_Solution_Works() { Knapsack knapsack = new Knapsack(); for (int i = 1; i <= 5; i++) { knapsack.Load($"Data/Knapsacks/json/mknapcb1/mknapcb1-{i}.json"); for (int length = 1; length <= 5; length++) { List <int> sol = knapsack.getInitialSolution(length); Console.WriteLine($"{i}\tSolution: " + JsonConvert.SerializeObject(sol)); Assert.AreEqual(sol.Count, length); double fitness = knapsack.getFitness(sol); Console.WriteLine($"{i}\tFitness: " + fitness); } } }
public List <Knapsack> readProblem(string filename) { int countMaxWeights = 0; int countItemsWeights = 0; int stage = 0; string[] lines = System.IO.File.ReadAllLines(filename); var ret = new List <Knapsack>(); Knapsack knapsack = new Knapsack(); int knapsacks_count = Convert.ToInt32(lines.FirstOrDefault()); for (int k = 1; k <= knapsacks_count; k++) { int i = 1; foreach (string s_loopVariable in lines.Skip(1)) { string s = s_loopVariable; s = s.Trim(); string[] ss = s.Split(' '); if (i == 1) { knapsack.noOfKnapsacks = Convert.ToInt32(ss[1]); knapsack.noOfItems = Convert.ToInt32(ss[0]); if (ss.Length > 2) { knapsack.goal = Convert.ToInt32(ss[2]); } i += 1; //first stage } else if (i == 2) { if (knapsack.items.Count() < knapsack.noOfItems) { foreach (string sss_loopVariable in ss) { string sss = sss_loopVariable; knapsack.items.Add(new Knapsack.Item(Convert.ToDouble(sss), new List <double>())); } } else { i += 1; } } if (i == 3) { if (countItemsWeights < knapsack.noOfKnapsacks) { for (int index = 0; index <= ss.Length - 1; index++) { knapsack.items[stage].weights.Add(Convert.ToDouble(ss[index])); stage += 1; } if (stage == knapsack.noOfItems) { countItemsWeights += 1; stage = 0; } } else { i += 1; } } if (i == 4) { if (countMaxWeights < knapsack.noOfKnapsacks) { foreach (string sss_loopVariable in ss) { string sss = sss_loopVariable; knapsack.weights.Add(Convert.ToDouble(sss)); countMaxWeights += 1; } } else { i += 1; } } } ret.Add(knapsack); } return(ret); }