Exemplo n.º 1
0
    public void Run()
    {
        // so here we're going to build the model
        var api = new ApiHelper <Matrix.MatrixRequest, Matrix.MatrixResponse>("matrix-vyv95n7wchpl", configFile);

        // create a solve request
        Matrix.MatrixRequest sr = new Matrix.MatrixRequest();

        // add locations to the matrix request
        foreach (var d in data)
        {
            sr.Locations.Add(new Matrix.Location
            {
                Id      = d.id,
                Geocode = new Matrix.Geocode
                {
                    Longitude = d.X,
                    Latitude  = d.Y
                }
            });
            sr.Sources.Add(d.id); // this will ensure a complete matrix. because destinations are empty, we assume you're asking for sources:sources
        }

        // configure the distance metric (although road network is the default)
        sr.distanceUnit = Matrix.MatrixRequest.eDistanceUnit.Kilometres;
        sr.durationUnit = Matrix.MatrixRequest.eDurationUnit.Minutes;

        string requestId = api.Post(sr); // send the model to the api

        Solution = api.Get(requestId);   // get the response (which it typed, so that's cool)

        System.Console.WriteLine(Solution.Elements.Count + " elements returned by the api");
        // lets make a matrix!
        Dictionary <string, int> idToIndex = new Dictionary <string, int>();

        for (int i = 0; i < data.Count; i++)
        {
            idToIndex.Add(data[i].id, i);
        }
        double[][] DM = new double[data.Count][];
        double[][] TM = new double[data.Count][];
        for (int i = 0; i < data.Count; i++)
        {
            DM[i] = new double[data.Count]; // make a square matrix
            TM[i] = new double[data.Count]; // make a square matrix
        }
        foreach (var item in Solution.Elements)
        {
            DM[idToIndex[item.fromId]][idToIndex[item.toId]] = item.Distance;
            TM[idToIndex[item.fromId]][idToIndex[item.toId]] = item.Distance;
        }
        printMatrix(DM, "Distance matrix");
        printMatrix(TM, "Time Matrix");
        return;
    }
Exemplo n.º 2
0
    public void Run()
    {
        // so here we're going to build the model
        var api = new ApiHelper <Matrix.MatrixRequest, Matrix.MatrixResponse>("matrix-vyv95n7wchpl", configFile);

        // create a solve request
        Matrix.MatrixRequest sr = new Matrix.MatrixRequest();


        // add points to the matrix request
        foreach (var d in data)
        {
            sr.Locations.Add(new Matrix.Location
            {
                Id      = d.id,
                Geocode = new Matrix.Geocode
                {
                    Longitude = d.X,
                    Latitude  = d.Y
                }
            });
        }

        // in this example we add the first two locations as sources and the balance as destinations
        for (int i = 0; i < data.Count; i++)
        {
            if (i < 2)
            {
                sr.Sources.Add(data[i].id);
            }
            else
            {
                sr.Destinations.Add(data[i].id);
            }
        }

        // configure the distance metric (although road network is the default)
        sr.distanceUnit = Matrix.MatrixRequest.eDistanceUnit.Kilometres;
        sr.durationUnit = Matrix.MatrixRequest.eDurationUnit.Minutes;

        string requestId = api.Post(sr); // send the model to the api

        Solution = api.Get(requestId);   // get the response (which it typed, so that's cool)

        Console.WriteLine(Solution.Elements.Count + " elements returned by the api");

        // We'll write this one out in long form.
        int maxchar = 0;

        foreach (var d in data)
        {
            maxchar = Math.Max(d.id.Length, maxchar); // so that the table displays nicely in the console :-)
        }
        string formatLine = "|{0,-" + maxchar + "}|{1,-" + maxchar + "}|{2,15}|{3,15}|";

        Console.WriteLine(String.Format(formatLine, "From", "To", "Distance", "Time"));
        foreach (var item in Solution.Elements)
        {
            Console.WriteLine(String.Format(formatLine, item.fromId, item.toId, item.Distance, item.Duration));
        }

        return;
    }