/**
         * Main calculation
         *
         * @param stockLengthInput raw bar length in stock
         * @param sawWidthInput    width of saw
         * @param orderSetInputs   required bar set
         * @param minLeftover      the lower bound of unacceptable remaining stock length after cutting
         * @param maxLeftover      the upper bound of unacceptable remaining stock length after cutting
         * @return Dictionary of data with map between pattern and number of required stock for this pattern,
         *         if can't cut, return EMPTY dictionary
         */
        public static Dictionary <List <BarSet>, int> calRequiredBarCoreMinMax(
            double stockLengthInput,
            double sawWidthInput,
            List <BarSet> orderSetInputs,
            double minLeftover,
            double maxLeftover)
        {
            Console.WriteLine(string.Format("-----Bar len: {0} bound:{1}-{2}-----\n", stockLengthInput, minLeftover, maxLeftover));

            // Normalize problem by remove saw width to Cutting Stock Problem (CSP)
            Pair <double, List <BarSet> > pair = normalizeInput(stockLengthInput, sawWidthInput, orderSetInputs);
            double        stockLength          = pair.fst;
            List <BarSet> orderSets            = pair.snd;

            // Solve CSP
            MinMaxSolver solver = new MinMaxSolver(minLeftover, maxLeftover);
            Dictionary <List <BarSet>, int> rstMap = solver.solve(orderSets, stockLength);

            // Convert problem back to before normalized
            rstMap = denomalizeResult(rstMap, sawWidthInput);

            printToConsole(rstMap, stockLengthInput);

            return(rstMap);
        }
Example #2
0
        public void TestOptimizeToOneStock_WithCantCutCondition_ExpectNull(double stock, double minLeftover, double maxLeftover, double[] orderLens, int[] orderNums)
        {
            BarSets      orders = initPattern(orderLens, orderNums);
            MinMaxSolver sut    = new MinMaxSolver(minLeftover, maxLeftover);
            BarSets      ret    = sut.optimizeToOneStock(stock, orders);

            Assert.Null(ret);
        }
Example #3
0
        public void TestAddPatternToDictionary()
        {
            var     dic     = new Dictionary <BarSets, int>();
            BarSets bs1     = initPattern(new double[] { 35d }, new int[] { 1 });
            BarSets addedBs = initPattern(new double[] { 30d }, new int[] { 1 });

            dic.Add(bs1, 2);

            MinMaxSolver.addPatternToDictionary(dic, addedBs);

            Assert.False(bs1.compareEqualWith(addedBs));
            Assert.Equal(2, dic.Count);
        }
Example #4
0
        public void TestOptimizeToOneStock(double stock, double minLeftover, double maxLeftover, double[] orderLens, int[] orderNums, double[] expectedNewPttrnPopLens)
        {
            BarSets      orders  = initPattern(orderLens, orderNums);
            MinMaxSolver sut     = new MinMaxSolver(minLeftover, maxLeftover);
            BarSets      pattern = sut.optimizeToOneStock(stock, orders);

            pattern.sortAsc();

            Assert.Equal(expectedNewPttrnPopLens.Length, pattern.count());
            foreach (double expectedPopLen in expectedNewPttrnPopLens)
            {
                Assert.Equal(expectedPopLen, pattern.popLen());
            }
        }
Example #5
0
        public void TestCalKnapsack(double stock, double minLeftover, double maxLeftover, double[] orderLens, int[] orderNums, double[] expectedNewPttrnPopLens)
        {
            BarSets orders = initPattern(orderLens, orderNums);
            BarSets ret    = MinMaxSolver.calKnapsack(stock, orders);

            ret.sortAsc();

            // Assert.Equal("", ret.ToString());
            Assert.Equal(expectedNewPttrnPopLens.Length, ret.count());
            foreach (double expectedPopLen in expectedNewPttrnPopLens)
            {
                Assert.Equal(expectedPopLen, ret.popLen());
            }
        }
Example #6
0
        public void TestSolve_WithCantCutCodition(double stock, double minLeftover, double maxLeftover, double[] orderLens, int[] orderNums)
        {
            var orderSets = new List <BarSet>();

            for (var i = 0; i < orderLens.Length; i++)
            {
                orderSets.Add(new BarSet(orderLens[i], orderNums[i]));
            }

            // Exercise
            var sut = new MinMaxSolver(minLeftover, maxLeftover);
            var ret = sut.solve(orderSets, stock);

            Assert.Equal(0, ret.Count);
        }
Example #7
0
        public void TestSolve(double stock, double minLeftover, double maxLeftover, double[] orderLens, int[] orderNums, int nRequiredBar, double expectedWastedLen, double lossRatio)
        {
            var orderSets = new List <BarSet>();

            for (var i = 0; i < orderLens.Length; i++)
            {
                orderSets.Add(new BarSet(orderLens[i], orderNums[i]));
            }

            // Exercise
            var sut = new MinMaxSolver(minLeftover, maxLeftover);
            var ret = sut.solve(orderSets, stock);

            // Assert
            int nTotalBar = UtilStatistics.sumInt(new List <int>(ret.Values));
            var wastedLen = UtilStatistics.calWastedLen(ret, stock, maxLeftover);

            Assert.Equal(nRequiredBar, nTotalBar);
            Assert.Equal(expectedWastedLen, wastedLen);
            Assert.InRange(wastedLen / (nTotalBar * stock), lossRatio - 0.01, lossRatio);
        }