当前位置: 首页>>代码示例>>C#>>正文


C# Solver.Solve方法代码示例

本文整理汇总了C#中Solver.Solve方法的典型用法代码示例。如果您正苦于以下问题:C# Solver.Solve方法的具体用法?C# Solver.Solve怎么用?C# Solver.Solve使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Solver的用法示例。


在下文中一共展示了Solver.Solve方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SolveCaseLarge

        public void SolveCaseLarge()
        {
            Solver solver = new Solver();

            solver.Solve(1091.73252, 0.2, 15.44421, 656.09352);
            solver.Solve(1.03291, 0.2, 99.49224, 99999.91210);
        }
开发者ID:ajlopez,项目名称:TddRocks,代码行数:7,代码来源:SolverTests.cs

示例2: should_have_correct_number_of_unique_solutions

        public void should_have_correct_number_of_unique_solutions(int n, int numSolutions)
        {
            var solver = new Solver(n);
              var results = solver.Solve();

              Assert.AreEqual(numSolutions, results.Count);
        }
开发者ID:shawnewallace,项目名称:nQueens,代码行数:7,代码来源:UniqueSolutuionsCheck.cs

示例3: Index

        public ActionResult Index(int k = 0)
        {
            int?[,] numbers = new int?[9, 9];

            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    string key = string.Format("f{0}{1}", i, j);
                    int number;
                    if (int.TryParse(Request[key], out number))
                        numbers[i, j] = number;
                }
            }

            Game game = new Game(numbers);
            Solver solver = new Solver();

            try
            {
                DateTime start = DateTime.Now;
                solver.Solve(game);
                DateTime end = DateTime.Now;

                ViewBag.Interval = string.Format("Solved in {0} ms.", end.Subtract(start).Milliseconds);
            }
            catch (InvalidGameException)
            {
                ViewBag.Message = "Invalid entry.  There is no solution for the game!";
            }

            ViewBag.Numbers = game.Numbers;
            return View();
        }
开发者ID:ravdin,项目名称:Sudoku,代码行数:34,代码来源:HomeController.cs

示例4: CPisFun

    //  We don't need helper functions here
    //  Csharp syntax is easier than C++ syntax!

    private static void CPisFun (int kBase)
    {
        //  Constraint Programming engine
        Solver solver = new Solver ("CP is fun!");

        // Decision variables
        IntVar c = solver.MakeIntVar (1, kBase - 1, "C");
        IntVar p = solver.MakeIntVar (0, kBase - 1, "P");
        IntVar i = solver.MakeIntVar (1, kBase - 1, "I");
        IntVar s = solver.MakeIntVar (0, kBase - 1, "S");
        IntVar f = solver.MakeIntVar (1, kBase - 1, "F");
        IntVar u = solver.MakeIntVar (0, kBase - 1, "U");
        IntVar n = solver.MakeIntVar (0, kBase - 1, "N");
        IntVar t = solver.MakeIntVar (1, kBase - 1, "T");
        IntVar r = solver.MakeIntVar (0, kBase - 1, "R");
        IntVar e = solver.MakeIntVar (0, kBase - 1, "E");

        // We need to group variables in a vector to be able to use
        // the global constraint AllDifferent
        IntVar[] letters = new IntVar[] { c, p, i, s, f, u, n, t, r, e};

        // Check if we have enough digits
        if (kBase < letters.Length) {
          throw new Exception("kBase < letters.Length");
        }

        //  Constraints
        solver.Add (letters.AllDifferent ());

        // CP + IS + FUN = TRUE
        solver.Add (p + s + n + kBase * (c + i + u) + kBase * kBase * f ==
               e + kBase * u + kBase * kBase * r + kBase * kBase * kBase * t);

        SolutionCollector all_solutions = solver.MakeAllSolutionCollector();
        //  Add the interesting variables to the SolutionCollector
        all_solutions.Add(c);
        all_solutions.Add(p);
        //  Create the variable kBase * c + p
        IntVar v1 = solver.MakeSum(solver.MakeProd(c, kBase), p).Var();
        //  Add it to the SolutionCollector
        all_solutions.Add(v1);

        //  Decision Builder: hot to scour the search tree
        DecisionBuilder db = solver.MakePhase (letters,
                                               Solver.CHOOSE_FIRST_UNBOUND,
                                               Solver.ASSIGN_MIN_VALUE);
        solver.Solve(db, all_solutions);

        //  Retrieve the solutions
        int numberSolutions = all_solutions.SolutionCount();
        Console.WriteLine ("Number of solutions: " + numberSolutions);

        for (int index = 0; index < numberSolutions; ++index) {
            Assignment solution = all_solutions.Solution(index);
            Console.WriteLine ("Solution found:");
            Console.WriteLine ("v1=" + solution.Value(v1));
        }
    }
开发者ID:RickOne16,项目名称:or-tools,代码行数:61,代码来源:cp_is_fun2.cs

示例5: Main

    public static void Main(string[] args)
    {
        if (args.Length < 1)
        {
            PrintUsage();
            return;
        }

        var rulesetArg = args[0];
        Ruleset ruleset;

        switch (rulesetArg)
        {
            case "yahtzee":
                ruleset = new Yahtzee();
                break;
            case "yatzy":
                ruleset = new Yatzy();
                break;
            default:
                Console.WriteLine("Unknown ruleset " + rulesetArg);
                return;
        }

        var solver = new Solver(ruleset);

        if (args.Length > 1)
        {
            var startStep = int.Parse(args[1]);
            solver.Solve(startStep);
        }
        else
        {
            solver.Solve();
        }
    }
开发者ID:Castux,项目名称:yahtzee,代码行数:36,代码来源:Program.cs

示例6: SolveByCramersMethodReturnsCorrectResult

        public void SolveByCramersMethodReturnsCorrectResult()
        {
            var equations = new List<Equation>
            {
                new Equation("2x+y=3"),
                new Equation("y=4")
            };
            var equationsSystem = new EquationsSystem(equations);
            var solver = new Solver();
            var expectedResult = Vector.Build.Dense(new[] { -0.5, 4 });

            var result = solver.Solve(equationsSystem);

            Assert.Equal(expectedResult.ToString(), result.ToString());
        }
开发者ID:Sequenta,项目名称:MathMate,代码行数:15,代码来源:SolverTests.cs

示例7: Solve

  /**
   *
   * Volsay problem.
   *
   * From the OPL model volsay.mod.
   *
   *
   * Also see http://www.hakank.org/or-tools/volsay.py
   *
   */
  private static void Solve()
  {

    Solver solver = new Solver("Volsay", Solver.CLP_LINEAR_PROGRAMMING);

    //
    // Variables
    //

    Variable Gas = solver.MakeNumVar(0, 100000, "Gas");
    Variable Chloride = solver.MakeNumVar(0, 100000, "Cloride");

    Constraint c1 = solver.Add(Gas + Chloride <= 50);
    Constraint c2 = solver.Add(3 * Gas + 4 * Chloride <= 180);

    solver.Maximize(40 * Gas + 50 * Chloride);

    int resultStatus = solver.Solve();

    if (resultStatus != Solver.OPTIMAL) {
      Console.WriteLine("The problem don't have an optimal solution.");
      return;
    }

    Console.WriteLine("Objective: {0}", solver.ObjectiveValue());

    Console.WriteLine("Gas      : {0} ReducedCost: {1}",
                      Gas.SolutionValue(),
                      Gas.ReducedCost());

    Console.WriteLine("Chloride : {0} ReducedCost: {1}",
                      Chloride.SolutionValue(),
                      Chloride.ReducedCost());

    Console.WriteLine("c1       : DualValue: {0} Activity: {1}",
                      c1.DualValue(),
                      c1.Activity());

    Console.WriteLine("c2       : DualValue: {0} Activity: {1}",
                      c2.DualValue(),
                      c2.Activity());



    Console.WriteLine("\nWallTime: " + solver.WallTime());
    Console.WriteLine("Iterations: " + solver.Iterations());

  }
开发者ID:HanumathRao,项目名称:hakank,代码行数:58,代码来源:volsay.cs

示例8: SolveSimplifiesSystemAndReturnsCorrectResult

        public void SolveSimplifiesSystemAndReturnsCorrectResult()
        {
            var equations = new List<Equation>
            {
                new Equation("x+2y+1y-2z-1=4"),
                new Equation("x+2x+5y+6z+2=9"),
                new Equation("2x+4y+3z=8")
            };
            var equationsSystem = new EquationsSystem(equations);
            var solver = new Solver();
            var expectedResult = Vector.Build.Dense(new double[] { -15, 8, 2 });

            var result = solver.Solve(equationsSystem);

            Assert.Equal(expectedResult.ToString(), result.ToString());
        }
开发者ID:Sequenta,项目名称:MathMate,代码行数:16,代码来源:SolverTests.cs

示例9: Main

        static void Main(string[] args)
        {
            Console.Write("Mutation rate? - ");
            int mutationRate = int.Parse(Console.ReadLine());
            Console.WriteLine();

            Console.Write("Generation size? - ");
            int generationSize = int.Parse(Console.ReadLine());
            Console.WriteLine();

            Console.Write("Generations count? - ");
            int generationsCount = int.Parse(Console.ReadLine());
            Console.WriteLine();

            Console.Clear();
            Console.WriteLine("Running...");
            Console.WriteLine();

            Stopwatch sw = Stopwatch.StartNew();

            Solver solver = new Solver(mutationRate, generationSize, generationsCount)
            {
                NoMutations = false
            };

            List<Gene> alfas = solver.Solve();

            Console.WriteLine("Mutation rate: " + mutationRate);
            Console.WriteLine("Generation size: " + generationSize);
            Console.WriteLine("Generations count: " + generationsCount);
            Console.WriteLine("\n\t----Top 3 genes----\n");

            for (int j = 0; j < 3; j++)
            {
                Console.WriteLine(alfas[j].ToString());
                Console.WriteLine();
            }

            Console.WriteLine("Elapsed: " + Math.Round(sw.Elapsed.TotalSeconds, 2) + "s");
            Console.ReadKey();
        }
开发者ID:Babelz,项目名称:Knapsack-Problem-Solver,代码行数:41,代码来源:Program.cs

示例10: Main

        static void Main(string[] args)
        {
            var target = new int[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } };
            var origin = new int[,] { { 1, 3, 6 }, { 4, 2, 0 }, { 7, 5, 8 } };

            var targetState = new State(target);
            var originState = new State(origin);

            INode<State> pOrigin = new PuzzleNode(targetState, originState);
            INode<State> pTarget = new PuzzleNode(targetState, targetState);

            Solver<State> solver = new Solver<State>(pOrigin, pTarget);
            var result = solver.Solve();

            do
            {
                Console.WriteLine(result.Content.ToDisplayString() + Environment.NewLine);
                result = result.Parent;
            } while (result != null);

            Console.Read();
        }
开发者ID:BotterVN,项目名称:CodeSnippets,代码行数:22,代码来源:Program.cs

示例11: Main

        public static void Main(string[] args)
        {
            string inputname = args[0];
            string outputname = args[1];

            string[] lines = File.ReadAllLines(inputname);

            int ncases = int.Parse(lines[0]);
            IList<string> results = new List<string>();
            Solver solver = new Solver();

            for (int k = 0; k < ncases; k++)
            {
                var numbers = lines[k + 1].Trim().Split(' ').Select(n => double.Parse(n)).ToList();
                var solution = solver.Solve(numbers[0], 2.0, numbers[1], numbers[2]);

                Console.WriteLine(k + 1);
                results.Add(string.Format("Case #{0}: {1}", k + 1, solution));
            }

            File.WriteAllLines(outputname, results.ToArray());
        }
开发者ID:ajlopez,项目名称:TddRocks,代码行数:22,代码来源:Program.cs

示例12: Solve

  /**
   *
   * Volsay problem.
   *
   * From the OPL model volsay.mod.
   * This version use arrays and matrices
   *
   *
   * Also see
   *  http://www.hakank.org/or-tools/volsay2.cs
   *  http://www.hakank.org/or-tools/volsay3.py
   *
   */
  private static void Solve()
  {

    Solver solver = new Solver("Volsay3",
                               Solver.CLP_LINEAR_PROGRAMMING);


    int num_products = 2;
    IEnumerable<int> PRODUCTS = Enumerable.Range(0, num_products);
    String[] products = {"Gas", "Chloride"};
    String[] components = {"nitrogen", "hydrogen", "chlorine"};

    int[,] demand = { {1,3,0}, {1,4,1}};
    int[] profit = {30,40};
    int[] stock = {50,180,40};


    //
    // Variables
    //
    Variable[] production = new Variable[num_products];
    foreach(int p in PRODUCTS) {
      production[p] = solver.MakeNumVar(0, 100000, products[p]);
    }

    //
    // Constraints
    //
    int c_len = components.Length;
    Constraint[] cons = new Constraint[c_len];
    for(int c = 0; c < c_len; c++) {
      cons[c] = solver.Add( (from p in PRODUCTS
                             select (demand[p,c]*production[p])).
                            ToArray().Sum() <= stock[c]);
    }

    //
    // Objective
    //
    solver.Maximize( (from p in PRODUCTS
                      select (profit[p]*production[p])).
                     ToArray().Sum()
                    );



    if (solver.Solve() != Solver.OPTIMAL) {
      Console.WriteLine("The problem don't have an optimal solution.");
      return;
    }

    Console.WriteLine("Objective: {0}", solver.ObjectiveValue());
    foreach(int p in PRODUCTS) {
      Console.WriteLine("{0,-10}: {1} ReducedCost: {2}",
                        products[p],
                        production[p].SolutionValue(),
                        production[p].ReducedCost());
    }


    for(int c = 0; c < c_len; c++) {
      Console.WriteLine("Constraint {0} DualValue {1} Activity: {2} lb: {3} ub: {4}",
                        c,
                        cons[c].DualValue(),
                        cons[c].Activity(),
                        cons[c].Lb(),
                        cons[c].Ub());
    }


    Console.WriteLine("\nWallTime: " + solver.WallTime());
    Console.WriteLine("Iterations: " + solver.Iterations());

  }
开发者ID:HanumathRao,项目名称:hakank,代码行数:87,代码来源:volsay3.cs

示例13: TrySolve

 private static SolverEntry TrySolve(PatienceField field, TimeSpan timeout)
 {
     Console.WriteLine(timeout);
     var solver = new Solver(field, silent: false);
     SolverEntry result = solver.Solve(timeout);
     return result;
 }
开发者ID:kpvleeuwen,项目名称:Boggle-solver,代码行数:7,代码来源:Program.cs

示例14: SolveCaseFour

        public void SolveCaseFour()
        {
            Solver solver = new Solver();

            Assert.AreEqual(526.1904762, solver.Solve(500.0, 2.0, 4.0, 2000.0), 0.0000001);
        }
开发者ID:ajlopez,项目名称:TddRocks,代码行数:6,代码来源:SolverTests.cs

示例15: Solve

 public static long Solve(string number)
 {
     var solver = new Solver(number);
     return solver.Solve();
 }
开发者ID:davghouse,项目名称:SPOJ,代码行数:5,代码来源:ACODE_v1.cs


注:本文中的Solver.Solve方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。