Home > Enterprise >  .NET Framework C# Console App doesn't recognise OR-Tools directive
.NET Framework C# Console App doesn't recognise OR-Tools directive

Time:11-28

I have a problem with OR-Tools. I'm using VS 2019 and I want use OR-Tools (with GMap if it's possible on Windows Forms App .NET Framework in the near future). I downloaded it from Manage NuGet Packages and when I pasted code from this link: https://developers.google.com/optimization/introduction/dotnet#complete-program

I've got 7 errors: Errors

I also downloaded Google.Protobufs because I think it was required, but nothing changed: Dependencies

I don't know how to use Google OR-Tools on C#, I use this for the first time. I hope that there is a way to use it with GMap, because I want combine these two elements (OR-Tools and GMaps) to click points on the map and make a route based on travelling salesman problem.

CodePudding user response:

using Google.OrTools.LinearSolver; is the key.

CodePudding user response:

using System;
using Google.OrTools.LinearSolver;

public class BasicExample
{
    static void Main()
    {
        // Create the linear solver with the GLOP backend.
        Solver solver = Solver.CreateSolver("GLOP");
        if (solver is null)
        {
            return;
        }

        // Create the variables x and y.
        Variable x = solver.MakeNumVar(0.0, 1.0, "x");
        Variable y = solver.MakeNumVar(0.0, 2.0, "y");

        Console.WriteLine("Number of variables = "   solver.NumVariables());

        // Create a linear constraint, 0 <= x   y <= 2.
        Constraint ct = solver.MakeConstraint(0.0, 2.0, "ct");
        ct.SetCoefficient(x, 1);
        ct.SetCoefficient(y, 1);

        Console.WriteLine("Number of constraints = "   solver.NumConstraints());

        // Create the objective function, 3 * x   y.
        Objective objective = solver.Objective();
        objective.SetCoefficient(x, 3);
        objective.SetCoefficient(y, 1);
        objective.SetMaximization();

        solver.Solve();

        Console.WriteLine("Solution:");
        Console.WriteLine("Objective value = "   solver.Objective().Value());
        Console.WriteLine("x = "   x.SolutionValue());
        Console.WriteLine("y = "   y.SolutionValue());
    }
}
  • Related