Example #1
0
    // Reads all TSP instances from .txt files.
    // The instances are stored as tspinstances structs in an array called "tspinstances"
    private static GameManager.TSPInstance[] LoadTSPInstances(int numberOfInstances, string type)
    {
        GameManager.TSPInstance[] tempInstances = new GameManager.TSPInstance[numberOfInstances];

        for (int k = 0; k < numberOfInstances; k++)
        {
            // create a dictionary where all the variables and definitions are strings
            var dict = new Dictionary <string, string>();

            // Use streamreader to read the input files if there are lines to read
            using (StreamReader sr = new StreamReader(folderPathLoadInstances + type + (k + 1) + ".txt"))
            {
                ReadToDict(sr, dict);
            }

            // Convert (most of them) to integers, with variables and literals being arrays and the others single literals
            tempInstances[k].coordinatesx = Array.ConvertAll(dict["coordinatesx"].Substring(1, dict["coordinatesx"].Length - 2).Split(','), float.Parse);
            tempInstances[k].coordinatesy = Array.ConvertAll(dict["coordinatesy"].Substring(1, dict["coordinatesy"].Length - 2).Split(','), float.Parse);

            tempInstances[k].distancematrix = StringToMatrix(dict["distancevector"]);

            tempInstances[k].ncities = int.Parse(dict["ncities"]);

            tempInstances[k].solution = int.Parse(dict["solution"]);
        }

        return(tempInstances);
    }
Example #2
0
    // Funciton to set up regular & Metric TSP instance
    void SetTSP(GameManager.TSPInstance currentInstance)
    {
        // Display current distance
        DistanceText = GameObject.Find("DistanceText").GetComponent <Text>();

        // Coordinate of the cities
        cox        = currentInstance.coordinatesx;
        coy        = currentInstance.coordinatesy;
        unitycoord = BoardFunctions.CoordinateConvertor(cox, coy);

        // Number of objects
        ncities   = currentInstance.ncities;
        distances = currentInstance.distancematrix;

        solution = currentInstance.solution;

        // Store objects in a list
        items = new Item[ncities];
        for (int i = 0; i < ncities; i++)
        {
            for (int j = i; j < ncities; j++)
            {
                if (distances[i, j] != 0)
                {
                    DrawSlimLine(i, j, 0.01f);
                }
            }
        }

        for (int i = 0; i < ncities; i++)
        {
            items[i] = GenerateItem(i, unitycoord[i]);
        }
    }