예제 #1
0
        public void VideoLecture01()
        {
            var lp = new LinearProgram
            {
                M               = 4,
                N               = 2,
                BasicIndices    = new[] { 0, 1, 4, 2, 6 },
                NonBasicIndices = new[] { 0, 3, 5 },
                Coefficients    = new[]
                {
                    new[] { 0.0, 0.0, 0.0 },
                    new[] { 2.0, 2.0 / 3.0, 1.0 / 3.0 },
                    new[] { 2.0, -1.0 / 3.0, -2.0 / 3.0 },
                    new[] { 2.0, 1.0 / 3.0, 2.0 / 3.0 },
                    new[] { 2.0, -2.0 / 3.0, -1.0 / 3.0 }
                },
                ObjectiveCoefficients = new[] { 9.0, 9.0, 9.0 }
            };

            var restorer = new SimpleRestoreObjective();

            restorer.Restore(lp, new[] { 0.0, 1.0, 2.0 }, new[] { 0, 1, 2 });

            lp.ObjectiveCoefficients.ShouldEqual(new[] { 6.0, 4.0 / 3.0, 5.0 / 3.0 }, .000001);
        }
예제 #2
0
        /// <summary>
        /// Reads the file provided and load in into the Liner Program Business Object. It will also filter out the header and not process it.
        /// </summary>
        /// <param name="filepath"></param>
        public virtual void LoadLinearProgramFileContent(string filepath)
        {
            //TODO:Dynamic type??? overkill??
            using (StreamReader _reader = new StreamReader(filepath))
            {
                string _line = null;
                while (null != (_line = _reader.ReadLine()))
                {
                    string[] _values = _line.Split(',');

                    //bypass the header - both files the first column header starts with Meter
                    if (_values[0].ToString().ToLower().Substring(0, 5) != "meter")
                    {
                        LinearProgram _tempLinearProgram = new LinearProgram();
                        _tempLinearProgram.FileName       = Path.GetFileName(filepath);
                        _tempLinearProgram.MeterPointCode = _values[0].ToString();
                        _tempLinearProgram.SerialNumber   = _values[1].ToString();
                        _tempLinearProgram.PlantCode      = _values[2].ToString();
                        _tempLinearProgram.Timestamp      = Convert.ToDateTime(_values[3].ToString());
                        _tempLinearProgram.DataType       = _values[4].ToString();
                        _tempLinearProgram.DataValue      = Convert.ToDecimal(_values[5].ToString());
                        _tempLinearProgram.Units          = (UnitType)Enum.Parse(typeof(UnitType), _values[6].ToString(), true);
                        _tempLinearProgram.Status         = _values[7].ToString();
                        _filteredLinearProgramList.Add(_tempLinearProgram);
                    }
                }
            }
        }
예제 #3
0
        private static void VerifyDual(LinearProgram primal, DualLinearProgram dual)
        {
            //verify coefficients
            dual.Coefficients.Length.ShouldEqual(primal.Coefficients[0].Length);

            //verify orig objective
            dual.OriginalObjectiveCoefficients.ShouldEqual(primal.ObjectiveCoefficients, Tolerance);

            for (var m = 1; m <= dual.M; m++)
            {
                dual.Coefficients[m][0].ShouldEqual(1, Tolerance);

                for (var n = 1; n <= dual.N; n++)
                {
                    dual.Coefficients[m][n].ShouldEqual(-primal.Coefficients[n][m], Tolerance);
                }
            }

            //verify objective coeficients
            dual.ObjectiveCoefficients[0].ShouldEqual(0.0);

            for (var n = 1; n <= dual.N; n++)
            {
                dual.ObjectiveCoefficients[n].ShouldEqual(-primal.Coefficients[n][0]);
            }

            //verify objective
            dual.ObjectiveValue.ShouldEqual(0.0);
        }
        public LinearProgamSolution Solve(LinearProgram linearProgram)
        {
            var pivotCount = 0;

            while (true)
            {
                //do this until no entering, or stop if unbounded
                _analyze.Analyze(linearProgram);

                //no entering, must be solved
                if (_analyze.Entering == 0)
                {
                    break;
                }

                //if unbounded
                if (_analyze.Leaving == 0)
                {
                    return(new LinearProgamSolution(0, 0, LinearProgramSolutionType.Unbounded));
                }

                //if here, we can pivot
                ++pivotCount;

                _pivotor.Pivot(_analyze.Entering, _analyze.Leaving, linearProgram);
            }

            return(new LinearProgamSolution(linearProgram.ObjectiveValue, pivotCount, LinearProgramSolutionType.Solved));
        }
예제 #5
0
        static void Main(string[] args)
        {
            LinearProgram[] linearProgrammes = new LinearProgram[6];

            LPParser parser = new LPParser();

            parser.SetObjectiveFunction("8x + 10y + 7z");
            parser.AddConstraint("x + 3y + 2z < 10");
            parser.AddConstraint("x + 5y + z < 8");
            linearProgrammes[0] = parser.LinearProgram;

            parser = new LPParser();
            parser.SetObjectiveFunction("x + 2y - z");
            parser.AddConstraint("2x + y + z < 14");
            parser.AddConstraint("4x + 2y + 3z < 28");
            parser.AddConstraint("2x + 5y + 5z < 30");
            linearProgrammes[1] = parser.LinearProgram;

            parser = new LPParser();
            parser.SetObjectiveFunction("3x + 4y");
            parser.AddConstraint("x + y < 4");
            parser.AddConstraint("2x + y < 5");
            linearProgrammes[2] = parser.LinearProgram;

            parser = new LPParser();
            parser.SetObjectiveFunction("-2x + y");
            parser.AddConstraint("x + 2y < 6");
            parser.AddConstraint("3x + 2y < 12");
            linearProgrammes[3] = parser.LinearProgram;

            parser = new LPParser();
            parser.SetObjectiveFunction("2x + 3y");
            parser.AddConstraint("x + y < 2000");
            parser.AddConstraint("8x + 14y < 20000");
            linearProgrammes[4] = parser.LinearProgram;

            parser = new LPParser();
            parser.SetObjectiveFunction("x + 0.8y");
            parser.AddConstraint("x + y < 1000");
            parser.AddConstraint("2x + y < 1500");
            parser.AddConstraint("3x + 2y < 2400");
            linearProgrammes[5] = parser.LinearProgram;

            for (int i = 0; i < linearProgrammes.Length; i++)
            {
                Console.WriteLine("----------------------------------");
                Console.WriteLine($"LP { i + 1 }");
                linearProgrammes[i].Output();
                Console.WriteLine();
                Console.WriteLine("Solution:");
                new Simplex(linearProgrammes[i]).Solve().Output();
            }
            Console.WriteLine("----------------------------------");
            Console.ReadKey();
        }
        //[Line 1] m n
        //[Line 2] B1 B2 ... Bm [the list of basic indices m integers]
        //[Line 3] N1 N2 ... Nn [the list of non-basic indices n integers]
        //[Line 4] b1 .. bm (m floating point numbers)
        //[Line 5] a11 ... a1n (first row coefficients. See dictionary notation above.)
        //....
        //[Line m+4] am1 ... amn (mth row coefficients. See dictionary notation above.)
        //[Line m+5] z0 c1 .. cn (objective coefficients (n+1 floating point numbers))

        public LinearProgram Map(DualLinearProgram dual)
        {
            var primal = new LinearProgram();

            primal.M = dual.N;
            primal.N = dual.M;

            primal.BasicIndices    = new int[dual.NonBasicIndices.Length];
            primal.NonBasicIndices = new int[dual.BasicIndices.Length];

            //set basic / non basic indices
            dual.NonBasicIndices.CopyTo(primal.BasicIndices, 0);
            dual.BasicIndices.CopyTo(primal.NonBasicIndices, 0);

            //set coefficients
            primal.Coefficients = new double[dual.Coefficients[0].Length][];

            primal.ObjectiveCoefficients = new double[dual.BasicIndices.Length];

            //map dual coefficients to primal coefficients
            var a = dual.Coefficients.Length;

            for (var i = 0; i < primal.Coefficients.Length; i++)
            {
                primal.Coefficients[i] = new double[a];//empty row for 1-based indexes
                if (i == 0)
                {
                    continue;
                }

                for (var k = 1; k < dual.Coefficients.Length; k++)
                {
                    primal.Coefficients[i][k] = -dual.Coefficients[k][i];
                }
            }

            //map dual objective coefficients to primal b-values
            for (var i = 1; i < dual.ObjectiveCoefficients.Length; i++)
            {
                primal.Coefficients[i][0] = -dual.ObjectiveCoefficients[i];
            }

            //map dual b-values to primal objective coefficients
            for (var i = 1; i < dual.Coefficients.Length; i++)
            {
                primal.ObjectiveCoefficients[i] = -dual.Coefficients[i][0];
            }
            primal.ObjectiveCoefficients[0] = -dual.ObjectiveCoefficients[0];

            return(primal);
        }
예제 #7
0
        public void TestSimplex()
        {
            double[,] A = new double[, ]
            {
                { 0, -1, -1 },
                { -2, -1, -2 },
                { -2, 1, -2 }
                //{0,1,1 },
                //{2,1,2 },
                //{0,-1,2}
            };
            double[] B = new double[] { -4, -6, -2 };
            double[] Z = new double[] { 3, 2, 1 };

            ILinearAlgoritm simplex = LinearProgram.GetDoubleSimplex(A, B, Z);

            simplex.Run();
        }
예제 #8
0
        public void TestJordanGayss()
        {
            double[,] A = new double[, ] {
                { 4, -7, 8 }, { 2, -4, 5 }, { -3, 11, 1 }
            };
            double[]        B      = new double[] { -23, -13, 16 };
            ILinearAlgoritm jordan = LinearProgram.GetJourdanGayssLinearAlgoritm(A, B);

            jordan.Run();
            var test = jordan.Value;

            double[,] expecteDoubles = new double[, ] {
                { 1, 0, 0, -2 }, { 0, 1, 0, 1 }, { 0, 0, 1, -1 }
            };
            for (int rowIndex = 0; rowIndex < test.GetLength(0); rowIndex++)
            {
                for (int columIndex = 0; columIndex < test.GetLength(1); columIndex++)
                {
                    Assert.AreEqual(expecteDoubles[rowIndex, columIndex], test[rowIndex, columIndex], delta: jordan.Delta);
                }
            }

            A = new double[, ] {
                { 3, 4 }, { 8, -10 }
            };
            B = new double[] { 4, 5 };

            expecteDoubles = new double[, ] {
                { 1, 0, 0.967 }, { 0, 1, 0.274 }
            };
            jordan   = LinearProgram.GetJourdanGayssLinearAlgoritm();
            jordan.A = A;
            jordan.B = B;
            jordan.Run();
            test = jordan.Value;

            for (int rowIndex = 0; rowIndex < test.GetLength(0); rowIndex++)
            {
                for (int columIndex = 0; columIndex < test.GetLength(1); columIndex++)
                {
                    Assert.AreEqual(expecteDoubles[rowIndex, columIndex], test[rowIndex, columIndex], delta: jordan.Delta);
                }
            }
        }
예제 #9
0
        static void Main(string[] args)
        {
            // This QuickStart Sample illustrates the three ways to create a Linear Program.

            // The first is in terms of matrices. The coefficients
            // are supplied as a matrix. The cost vector, right-hand side
            // and constraints on the variables are supplied as a vector.

            // The cost vector:
            var c = Vector.Create(-1.0, -3.0, 0.0, 0.0, 0.0, 0.0);
            // The coefficients of the constraints:
            var A = Matrix.Create(4, 6, new double[]
            {
                1, 1, 1, 0, 0, 0,
                1, 1, 0, -1, 0, 0,
                1, 0, 0, 0, 1, 0,
                0, 1, 0, 0, 0, 1
            }, MatrixElementOrder.RowMajor);
            // The right-hand sides of the constraints:
            var b = Vector.Create(1.5, 0.5, 1.0, 1.0);

            // We're now ready to call the constructor.
            // The last parameter specifies the number of equality
            // constraints.
            LinearProgram lp1 = new LinearProgram(c, A, b, 4);

            // Now we can call the Solve method to run the Revised
            // Simplex algorithm:
            var x = lp1.Solve();
            // The GetDualSolution method returns the dual solution:
            var y = lp1.GetDualSolution();

            Console.WriteLine("Primal: {0:F1}", x);
            Console.WriteLine("Dual:   {0:F1}", y);
            // The optimal value is returned by the Extremum property:
            Console.WriteLine("Optimal value:   {0:F1}", lp1.OptimalValue);

            // The second way to create a Linear Program is by constructing
            // it by hand. We start with an 'empty' linear program.
            LinearProgram lp2 = new LinearProgram();

            // Next, we add two variables: we specify the name, the cost,
            // and optionally the lower and upper bound.
            lp2.AddVariable("X1", -1.0, 0, 1);
            lp2.AddVariable("X2", -3.0, 0, 1);

            // Next, we add constraints. Constraints also have a name.
            // We also specify the coefficients of the variables,
            // the lower bound and the upper bound.
            lp2.AddLinearConstraint("C1", Vector.Create(1.0, 1.0), 0.5, 1.5);
            // If a constraint is a simple equality or inequality constraint,
            // you can supply a LinearProgramConstraintType value and the
            // right-hand side of the constraint.

            // We can now solve the linear program:
            x = lp2.Solve();
            y = lp2.GetDualSolution();
            Console.WriteLine("Primal: {0:F1}", x);
            Console.WriteLine("Dual:   {0:F1}", y);
            Console.WriteLine("Optimal value:   {0:F1}", lp2.OptimalValue);

            // Finally, we can create a linear program from an MPS file.
            // The MPS format is a standard format.
            LinearProgram lp3 = MpsReader.Read(@"..\..\..\..\data\sample.mps");

            // We can go straight to solving the linear program:
            x = lp3.Solve();
            y = lp3.GetDualSolution();
            Console.WriteLine("Primal: {0:F1}", x);
            Console.WriteLine("Dual:   {0:F1}", y);
            Console.WriteLine("Optimal value:   {0:F1}", lp3.OptimalValue);

            Console.Write("Press Enter key to exit...");
            Console.ReadLine();
        }