Exemple #1
0
        public void FindSolutionStartTest1()
        {
            var data       = new KnapsackProblemDataParser();
            var constraint = KnapsackProblemDataParser.FindSolutionStart("sol:", data);

            Assert.AreEqual(KnapsackProblemDataParser.ReadSolutionResourceOrFindSolutionEnd, constraint);
        }
Exemple #2
0
        public void ReadSolutionResourceOrFindSolutionEndTest2()
        {
            var data       = new KnapsackProblemDataParser();
            var constraint = KnapsackProblemDataParser.ReadSolutionResourceOrFindSolutionEnd("", data);

            Assert.IsNull(constraint);
        }
Exemple #3
0
        public void ReadResourceOrFindDataEndTest1()
        {
            var data       = new KnapsackProblemDataParser();
            var constraint = KnapsackProblemDataParser.ReadResourceOrFindDataEnd("end data", data);

            Assert.AreEqual(KnapsackProblemDataParser.FindSolutionStart, constraint);
        }
Exemple #4
0
        public void FindSolutionStartTest2()
        {
            var data       = new KnapsackProblemDataParser();
            var constraint = KnapsackProblemDataParser.FindSolutionStart("3369\t3712", data);

            Assert.AreEqual(KnapsackProblemDataParser.FindSolutionStart, constraint);
        }
Exemple #5
0
        public void FindDataStartTest1()
        {
            var data       = new KnapsackProblemDataParser();
            var constraint = KnapsackProblemDataParser.FindDataStart("begin data", data);

            Assert.AreEqual(KnapsackProblemDataParser.ReadResourceOrFindDataEnd, constraint);
        }
Exemple #6
0
        public void FindDataStartTest2()
        {
            var data       = new KnapsackProblemDataParser();
            var constraint = KnapsackProblemDataParser.FindDataStart("Something else", data);

            Assert.AreEqual(KnapsackProblemDataParser.FindDataStart, constraint);
        }
Exemple #7
0
        public void FindConstraintTest1()
        {
            var data       = new KnapsackProblemDataParser();
            var constraint = KnapsackProblemDataParser.FindConstraint("c: 123", data);

            Assert.AreEqual(KnapsackProblemDataParser.FindDataStart, constraint);
            Assert.AreEqual(123, data.MaxWeight);
        }
 public static FindMethod FindSolutionStart(string line, KnapsackProblemDataParser data)
 {
     if (line == "sol:")
     {
         return(ReadSolutionResourceOrFindSolutionEnd);
     }
     return(FindSolutionStart);
 }
        public static FindMethod FindDataStart(string line, KnapsackProblemDataParser data)
        {
            if (line != "begin data")
            {
                return(FindDataStart);
            }

            return(ReadResourceOrFindDataEnd);
        }
        public void Exnsd16Test()
        {
            var problemInfo = KnapsackProblemDataParser.LoadData(@"..\..\Data\exnsd16.ukp");
            var items       = problemInfo.Resources;
            var maxWeight   = problemInfo.MaxWeight;
            var maxVolume   = 0.0;
            var optimal     = GetFitness(problemInfo.Solution);

            FillKnapsack(items, maxWeight, maxVolume, optimal);
        }
Exemple #11
0
        public void ReadResourceOrFindDataEndTest2()
        {
            var data       = new KnapsackProblemDataParser();
            var constraint = KnapsackProblemDataParser.ReadResourceOrFindDataEnd("3369\t3712", data);

            Assert.AreEqual(KnapsackProblemDataParser.ReadResourceOrFindDataEnd, constraint);
            Assert.AreEqual("R1", data.Resources[0].Name);
            Assert.AreEqual(3712, data.Resources[0].Value);
            Assert.AreEqual(0, data.Resources[0].Volume);
            Assert.AreEqual(3369, data.Resources[0].Weight);
        }
        public static FindMethod ReadResourceOrFindDataEnd(string line, KnapsackProblemDataParser data)
        {
            if (line == "end data")
            {
                return(FindSolutionStart);
            }
            var parts   = line.Split('\t');
            var resouce = new Resource("R" + (data.Resources.Count + 1), int.Parse(parts[1]), int.Parse(parts[0]), 0);

            data.Resources.Add(resouce);
            return(ReadResourceOrFindDataEnd);
        }
        public static FindMethod FindConstraint(string line, KnapsackProblemDataParser data)
        {
            var parts = line.Split(' ');

            if (parts[0] != "c:")
            {
                return(FindConstraint);
            }

            data.MaxWeight = int.Parse(parts[1]);
            return(FindDataStart);
        }
Exemple #14
0
        public void ReadSolutionResourceOrFindSolutionEndTest1()
        {
            var data     = new KnapsackProblemDataParser();
            var resource = new Resource("R1", 5687, 6585, 0);

            data.Resources.Add(resource);

            var constraint =
                KnapsackProblemDataParser.ReadSolutionResourceOrFindSolutionEnd("\t1\t156\t5687\t6585", data);

            Assert.AreEqual(KnapsackProblemDataParser.ReadSolutionResourceOrFindSolutionEnd, constraint);
            Assert.AreSame(resource, data.Solution[0].Item);
            Assert.AreEqual(156, data.Solution[0].Quantity);
        }
        public static FindMethod ReadSolutionResourceOrFindSolutionEnd(string line, KnapsackProblemDataParser data)
        {
            if (string.IsNullOrWhiteSpace(line))
            {
                return(null);
            }

            var parts            = line.Split('\t').Where(s => !string.IsNullOrWhiteSpace(s)).Select(p => p).ToArray();
            var resourceIndex    = int.Parse(parts[0]) - 1;
            var resourceQuantity = int.Parse(parts[1]);
            var item             = new ItemQuantity(data.Resources[resourceIndex], resourceQuantity);

            data.Solution.Add(item);
            return(ReadSolutionResourceOrFindSolutionEnd);
        }
        public static KnapsackProblemData LoadData(string filename)
        {
            var        lines = File.ReadAllLines(filename);
            var        data  = new KnapsackProblemDataParser();
            FindMethod f     = FindConstraint;

            foreach (var line in lines)
            {
                f = f(line.Trim(), data);
                if (f == null)
                {
                    break;
                }
            }

            return(new KnapsackProblemData(data.Resources.ToArray(), data.MaxWeight, data.Solution.ToArray()));
        }
Exemple #17
0
        public void LoadDataTest()
        {
            var problemInfo = KnapsackProblemDataParser.LoadData(@"..\..\Data\exnsd16.ukp");

            Assert.AreEqual(889304, problemInfo.MaxWeight);
            Assert.AreEqual(2000, problemInfo.Resources.Length);
            Assert.AreEqual(2, problemInfo.Solution.Length);

            // nb    idx   w        p
            // 156 x R288, 5687, 0, 6585
            //   1 x R987, 2131, 0, 2420

            var iq1 = new ItemQuantity(problemInfo.Resources[288 - 1], 156);
            var iq2 = new ItemQuantity(problemInfo.Resources[987 - 1], 1);

            CollectionAssert.AreEquivalent(new[] { iq1, iq2 }, problemInfo.Solution);
        }