示例#1
0
        private SolvedModel BuildSolvedModel()
        {
            var x1 = new Variable {
                Name = "X1", Type = VariableType.Input
            };
            var x2 = new Variable {
                Name = "X2", Type = VariableType.Input
            };
            var c1 = new Constraint
            {
                Name           = "C1",
                ConstraintType = ConstraintType.GreaterThanOrEqualTo,
                Variables      = new List <ValueVariable>(new[]
                {
                    new ValueVariable {
                        Variable = x1, Coefficient = 2
                    },
                    new ValueVariable {
                        Variable = x2, Coefficient = 1
                    },
                }),
                ConstraintValue = 4
            };
            var c2 = new Constraint
            {
                Name           = "C2",
                ConstraintType = ConstraintType.GreaterThanOrEqualTo,
                Variables      = new List <ValueVariable>(new[]
                {
                    new ValueVariable {
                        Variable = x1, Coefficient = 1
                    },
                    new ValueVariable {
                        Variable = x2, Coefficient = 7
                    },
                }),
                ConstraintValue = 7
            };
            var objectiveFunction = new List <ValueVariable>(new[]
            {
                new ValueVariable {
                    Variable = x1, Coefficient = 1
                },
                new ValueVariable {
                    Variable = x2, Coefficient = 1
                },
            });

            var linearModel = new LinearModel
            {
                Objective         = Objective.MIN,
                Variables         = new List <Variable>(new[] { x1, x2 }),
                Constraints       = new List <Constraint>(new[] { c1, c2 }),
                ObjectiveFunction = objectiveFunction
            };

            var res = LinearModelSolver.Solve(linearModel);

            return(res);
        }
示例#2
0
        private void Solve()
        {
            var text = TextBox.Text;

            if (string.IsNullOrWhiteSpace(text))
            {
                return;
            }
            try
            {
                // var result = LinearSolver.Solve(text);
                // MessageBox.Show(result.Message, "Result", MessageBoxButton.OK);
                var result       = LinearModelSolver.Solve(LinearParser.Parse(text));
                var resultWindow = new LinearResultWindow(result);
                resultWindow.ShowDialog();
            }
            catch (InvalidInputException iie)
            {
                MessageBox.Show(iie.Message + $"\n Error Line: {iie.ParsedInput[iie.ErrorLine]}", "Input Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Unknown Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
示例#3
0
    [Test] public void TwoPhasesFromLinearModel()
    {
        var x1 = new Variable {
            Name = "X1", Type = VariableType.Input
        };
        var x2 = new Variable {
            Name = "X2", Type = VariableType.Input
        };
        var c1 = new Constraint
        {
            Name      = "C1", ConstraintType = ConstraintType.GreaterThanOrEqualTo,
            Variables = new List <ValueVariable>(new[]
            {
                new ValueVariable {
                    Variable = x1, Coefficient = 2
                },
                new ValueVariable {
                    Variable = x2, Coefficient = 1
                },
            }),
            ConstraintValue = 4
        };
        var c2 = new Constraint
        {
            Name      = "C2", ConstraintType = ConstraintType.GreaterThanOrEqualTo,
            Variables = new List <ValueVariable>(new[]
            {
                new ValueVariable {
                    Variable = x1, Coefficient = 1
                },
                new ValueVariable {
                    Variable = x2, Coefficient = 7
                },
            }),
            ConstraintValue = 7
        };
        var objectiveFunction = new List <ValueVariable>(new[]
        {
            new ValueVariable {
                Variable = x1, Coefficient = 1
            },
            new ValueVariable {
                Variable = x2, Coefficient = 1
            },
        });

        var linearModel = new LinearModel
        {
            Objective         = Objective.MIN,
            Variables         = new List <Variable>(new[] { x1, x2 }),
            Constraints       = new List <Constraint>(new[] { c1, c2 }),
            ObjectiveFunction = objectiveFunction
        };

        var res = LinearModelSolver.Solve(linearModel);

        Assert.AreEqual(new BigRational(31, 13), res.Z);
    }
示例#4
0
    [Test] public void LinearFromString5()
    {
        var inputString = @"MAX Z = X1 + X2
1.5X1 + 1.5X2 <= 3";

        var model       = LinearParser.Parse(inputString);
        var solvedModel = LinearModelSolver.Solve(model);

        Assert.AreEqual(SolvedResult.Optimized, solvedModel.Result);
        Assert.AreEqual(new BigRational(2, 1), solvedModel.Z);
    }
示例#5
0
    [Test] public void TwoPhasesInfeasibleFromLinearModel()
    {
        var x1 = new Variable {
            Name = "X1", Type = VariableType.Input
        };
        var x2 = new Variable {
            Name = "X2", Type = VariableType.Input
        };
        var x3 = new Variable {
            Name = "X3", Type = VariableType.Input
        };
        var c1 = new Constraint
        {
            ConstraintType = ConstraintType.EqualTo,
            Variables      = new List <ValueVariable>(new[]
            {
                x1.As(-2),
                x2.As(1),
                x3.As(3),
            }),
            ConstraintValue = 2
        };
        var c2 = new Constraint
        {
            ConstraintType = ConstraintType.EqualTo,
            Variables      = new List <ValueVariable>(new[]
            {
                x1.As(2),
                x2.As(3),
                x3.As(4),
            }),
            ConstraintValue = 1
        };
        var objectiveFunctions = new List <ValueVariable>(new[]
        {
            x1.As(1),
            x2.As(-2),
            x3.As(-3)
        });

        var linearModel = new LinearModel
        {
            Constraints       = new List <Constraint>(new[] { c1, c2 }),
            Objective         = Objective.MIN,
            ObjectiveFunction = objectiveFunctions,
            Variables         = new List <Variable>(new[] { x1, x2, x3 })
        };

        var res = LinearModelSolver.Solve(linearModel);

        Assert.IsTrue(res.Result == SolvedResult.Infeasible);
        Assert.IsTrue(res.TwoPhasesIterations.Count == 2);
    }
示例#6
0
    [Test] public void LinearFromString4()
    {
        var inputString = @"MAX Z = 3X1 + X2 + 3X3
2X1 + X2 + X3 <= 2
X1 + 2X2 + 3X3 <= 5
2X1 + 2X2 + X3 <= 6";

        var model       = LinearParser.Parse(inputString);
        var solvedModel = LinearModelSolver.Solve(model);

        Assert.AreEqual(SolvedResult.Optimized, solvedModel.Result);
        Assert.AreEqual(new BigRational(54, 10), solvedModel.Z);
    }
示例#7
0
    [Test] public void LinearFromString2()
    {
        var inputString = @"MAX Z = X1 + X2
3X1 + 2X2 <= 20
2X1 + 3X2 <= 20
X1 + 2X2 >= 2";

        var model       = LinearParser.Parse(inputString);
        var solvedModel = LinearModelSolver.Solve(model);

        Assert.AreEqual(SolvedResult.Optimized, solvedModel.Result);
        Assert.AreEqual(new BigRational(8, 1), solvedModel.Z);
    }
示例#8
0
    [Test] public void LinearFromString()
    {
        var inputString = @"MAX Z = 3X1 + 5X2
X1 <= 4
2X2 <= 12
3X1 + 2X2 <= 18";

        var model       = LinearParser.Parse(inputString);
        var solvedModel = LinearModelSolver.Solve(model);

        Assert.AreEqual(SolvedResult.Optimized, solvedModel.Result);
        Assert.AreEqual(new BigRational(36, 1), solvedModel.Z);
    }
示例#9
0
    [Test] public void LinearFromString3()
    {
        var inputString = @"MIN Z = -1X1 + 2X2 - 3X3
X1 + X2 + X3 = 6
-1X1 + X2 + 2X3 = 4
2X2 + 3X3 = 10
X3 <= 2";

        var model       = LinearParser.Parse(inputString);
        var solvedModel = LinearModelSolver.Solve(model);

        Assert.AreEqual(SolvedResult.Optimized, solvedModel.Result);
        Assert.AreEqual(new BigRational(-4, 1), solvedModel.Z);
    }
 public async Task Solve()
 {
     if (string.IsNullOrWhiteSpace(Text))
     {
         return;
     }
     try
     {
         var result       = LinearModelSolver.Solve(LinearParser.Parse(Text));
         var resultWindow = new SolvedView(MainWindow, result)
         {
             DataContext = new SolvedViewModel()
         };
         await resultWindow.ShowDialog(MainWindow);
     }
     catch (InvalidInputException iie)
     {
         await MessageBox.Show(MainWindow, iie.Message + $"\n Error Line: {iie.ParsedInput[iie.ErrorLine]}", "Input Error", MessageBox.MessageBoxButtons.Ok);
     }
     catch (Exception ex)
     {
         await MessageBox.Show(MainWindow, ex.Message, "Unknown Error", MessageBox.MessageBoxButtons.Ok);
     }
 }