Esempio n. 1
0
    public static TSPCase generateCase(int numPoints, Vector2 startingPoint, Vector2 spaceDimensions)
    {
        Vector2[] nodes = new Vector2[numPoints];
        nodes[0] = startingPoint;
        for (int i = 1; i < nodes.Length; i++)
        {
            int     x = Random.Range(0, (int)spaceDimensions.x);
            int     y = Random.Range(0, (int)spaceDimensions.y);
            Vector2 v = new Vector2(x, y);
            for (int j = 0; j < i; j++)
            {
                if (v == nodes[j])
                {
                    x = Random.Range(0, (int)spaceDimensions.x);
                    y = Random.Range(0, (int)spaceDimensions.y);
                    v = new Vector2(x, y);
                    j = 0;
                }
            }
            nodes[i] = v;
        }
        int     startingNode = Random.Range(0, nodes.Length);
        TSPCase tspCase      = new TSPCase(nodes, spaceDimensions);

        return(tspCase);
    }
Esempio n. 2
0
 public static TSPCase generateCase(int numPoints, Vector2 startingPoint, Vector2 spaceDimensions)
 {
     Vector2[] nodes = new Vector2[numPoints];
     nodes[0] = startingPoint;
     for (int i = 1; i < nodes.Length; i++)
     {
         int x = Random.Range(0, (int)spaceDimensions.x);
         int y = Random.Range(0, (int)spaceDimensions.y);
         Vector2 v = new Vector2(x, y);
         for (int j = 0; j < i; j++)
         {
             if (v == nodes[j])
             {
                 x = Random.Range(0, (int)spaceDimensions.x);
                 y = Random.Range(0, (int)spaceDimensions.y);
                 v = new Vector2(x, y);
                 j = 0;
             }
         }
         nodes[i] = v;
     }
     int startingNode = Random.Range(0, nodes.Length);
     TSPCase tspCase = new TSPCase(nodes, spaceDimensions);
     return tspCase;
 }
Esempio n. 3
0
 public static float calculateCost(int[] permutation, TSPCase tspCase)
 {
     float acum = tspCase.DistanceMatrix[permutation[permutation.Length - 1], permutation[0]];
     for (int i = 0; i < permutation.Length - 1; i++)
     {
         acum += tspCase.DistanceMatrix[permutation[i], permutation[i + 1]];
     }
     return acum;
 }
Esempio n. 4
0
    public static int calculateCost(int[] permutation, TSPCase tspCase)
    {
        int acum = tspCase.DistanceMatrix[permutation[permutation.Length - 1], permutation[0]];

        for (int i = 0; i < permutation.Length - 1; i++)
        {
            acum += tspCase.DistanceMatrix[permutation[i], permutation[i + 1]];
        }
        return(acum);
    }
Esempio n. 5
0
    public static int[] organizeNodes(int[] solution, TSPCase problem)
    {
        int originPos = -1;
        for(int i = 0; i < solution.Length; i++){
            if(solution[i] == 0){
                originPos = i;
            }
        }

        int[] organizedSolution = new int[solution.Length];
        for(int j=0; j<organizedSolution.Length; j++){
            organizedSolution[j] = solution[(j+originPos) % solution.Length];
        }

        return organizedSolution;
    }
Esempio n. 6
0
 private void createNewTSPCase()
 {
     solution    = null;
     currentCase = TSPSolver.generateCase(nodeCount, dimensions);
     realocateNodes();
 }
Esempio n. 7
0
 public Day(TSPCase tspCase)
 {
     this.tspCase = tspCase;
     solution = TSPSolver.solveCase(tspCase);
 }
Esempio n. 8
0
    public static int[] solveCase(TSPCase problem)
    {
        int count = 2 * problem.Nodes.Length;

        float[] all_De   = new float[count];
        int[]   currS    = randomPermutation(problem.Nodes.Length);
        float   currCost = calculateCost(currS, problem);

        for (int i = 0; i < count; i++)
        {
            currS    = randomPermutation(problem.Nodes.Length);
            currCost = calculateCost(currS, problem);

            int[] newS      = (int[])currS.Clone();
            int[] seleccion = randomPermutation(problem.Nodes.Length);

            int inicio, fin;
            if (seleccion[0] > seleccion[1])
            {
                inicio = seleccion[1];
                fin    = seleccion[0];
            }
            else
            {
                inicio = seleccion[0];
                fin    = seleccion[1];
            }
            int apunt = 0;
            for (int j = inicio; j <= fin; j++)
            {
                newS[j] = currS[fin - apunt];
                apunt++;
            }
            float newCost = calculateCost(newS, problem);
            all_De[i] = Mathf.Abs(newCost - currCost);
        }
        float dE           = Mathf.Max(all_De);
        float temp         = 100 * dE;
        float temp_inicial = temp;
        float temp_final   = 0.0001f * temp;
        int   m            = 500 * problem.Nodes.Length;
        float tempRatio    = 0.5f;

        int[] bestS    = (int[])currS.Clone();
        float bestCost = currCost;

        int cont = 0;

        while (temp > temp_final)
        {
            for (int i = 0; i < m; i++)
            {
                cont++;
                int[] newS = (int[])currS.Clone();
                int   n1   = Random.Range(0, problem.Nodes.Length);
                int   n2   = Random.Range(0, problem.Nodes.Length);
                while (n1 == n2)
                {
                    n2 = Random.Range(0, problem.Nodes.Length);
                }
                int inicio = (n1 > n2) ? n2 : n1;
                int final  = (n1 > n2) ? n1 : n2;
                int apunt  = 0;

                for (int j = inicio; j <= final; j++)
                {
                    newS[j] = currS[final - apunt];
                    apunt++;
                }

                float newCost = calculateCost(newS, problem);
                if (newCost < bestCost)
                {
                    bestS    = (int[])newS.Clone();
                    bestCost = newCost;
                }

                if (newCost < currCost || Random.Range(0.0f, 1.0f) < Mathf.Exp((currCost - newCost) / temp))
                {
                    currS    = (int[])newS.Clone();
                    currCost = newCost;
                }
            }
            temp *= tempRatio;
        }
        return((int[])bestS.Clone());
    }
Esempio n. 9
0
 public Day(TSPCase tspCase)
 {
     this.tspCase = tspCase;
     solution     = TSPSolver.solveCase(tspCase);
 }
Esempio n. 10
0
    public static int[] solveCase(TSPCase problem)
    {
        int count = 2 * problem.Nodes.Length;
        float[] all_De = new float[count];
        int[] currS = randomPermutation(problem.Nodes.Length);
        float currCost = calculateCost(currS, problem);
        for (int i = 0; i < count; i++)
        {
            currS = randomPermutation(problem.Nodes.Length);
            currCost = calculateCost(currS, problem);

            int[] newS = (int[])currS.Clone();
            int[] seleccion = randomPermutation(problem.Nodes.Length);

            int inicio, fin;
            if (seleccion[0] > seleccion[1])
            {
                inicio = seleccion[1];
                fin = seleccion[0];
            }
            else
            {
                inicio = seleccion[0];
                fin = seleccion[1];
            }
            int apunt = 0;
            for (int j = inicio; j <= fin; j++)
            {
                newS[j] = currS[fin - apunt];
                apunt++;
            }
            float newCost = calculateCost(newS, problem);
            all_De[i] = Mathf.Abs(newCost - currCost);
        }
        float dE = Mathf.Max(all_De);
        float temp = 100 * dE;
        float temp_inicial = temp;
        float temp_final = 0.0001f * temp;
        int m = 500 * problem.Nodes.Length;
        float tempRatio = 0.5f;
        int[] bestS = (int[])currS.Clone();
        float bestCost = currCost;

        int cont = 0;
        while (temp > temp_final)
        {
            for (int i = 0; i < m; i++)
            {
                cont++;
                int[] newS = (int[])currS.Clone();
                int n1 = Random.Range(0, problem.Nodes.Length);
                int n2 = Random.Range(0, problem.Nodes.Length);
                while (n1 == n2)
                {
                    n2 = Random.Range(0, problem.Nodes.Length);
                }
                int inicio = (n1 > n2) ? n2 : n1;
                int final = (n1 > n2) ? n1 : n2;
                int apunt = 0;

                for (int j = inicio; j <= final; j++)
                {
                    newS[j] = currS[final - apunt];
                    apunt++;
                }

                float newCost = calculateCost(newS, problem);
                if (newCost < bestCost)
                {
                    bestS = (int[])newS.Clone();
                    bestCost = newCost;
                }

                if (newCost < currCost || Random.Range(0.0f, 1.0f) < Mathf.Exp((currCost - newCost) / temp))
                {
                    currS = (int[])newS.Clone();
                    currCost = newCost;
                }
            }
            temp *= tempRatio;
        }
        return organizeNodes((int[])bestS.Clone(), problem);
    }
Esempio n. 11
0
 private void createNewTSPCase()
 {
     solution = null;
     currentCase = TSPSolver.generateCase(nodeCount, dimensions);
     realocateNodes();
 }