示例#1
0
    //DEBUGED
    // MIN-MAX multiplication
    public DirtySet multiplication(int[][] B)
    {
        int[] A = dirtySet;
        int[] D = new int[N];

        for (int i = 0; i < N; i++)
        {
            int[] min = new int[N];
            for (int m = 0; m < N; m++)
            {
                if (A[m] < B[m][i])
                {
                    min[m] = A[m];
                }
                else
                {
                    min[m] = B[m][i];
                }
            }
            int max = -1;
            for (int m = 0; m < N; m++)
            {
                if (min[m] > max)
                {
                    max = min[m];
                }
            }

            D[i] = max;
        }

        DirtySet ret = new DirtySet(D);

        return(ret);
    }
示例#2
0
    public PEalgorithm(Graph g, int[] robotPosition)
    {
        graph = g;

        // Allocazione della memoria per tutte le strutture dati
        A = graph.adjacency;
        r = robotPosition;
        R = robotPosition.Length;

        N = graph.getnumnodes();

        d = new DirtySet[N][];
        for (int i = 0; i < N; i++)
        {
            d[i] = new DirtySet[N];
            for (int j = 0; j < N; j++)
            {
                d[i][j] = new DirtySet(N);
            }
        }

        v    = new int[N][];
        path = new Path[N][];
        for (int i = 0; i < N; i++)
        {
            path[i] = new Path[N];
            for (int j = 0; j < N; j++)
            {
                path[i][j] = new Path();
            }
            v[i] = new int[N];
        }
    }
示例#3
0
    //DEBUGED
    private int find(DirtySet d, ArrayList list)
    {
        for (int i = 0; i < list.Count; i++)
        {
            if (d.equal((DirtySet)list[i]))
            {
                return(i);
            }
        }

        return(-1);
    }
示例#4
0
    public Path PEstep(int MaxIteration)
    {
        inizializzation();

        // ALGORITHM
        int iteration = 0;

        while (Vbest > 0 && iteration < MaxIteration)
        {
            iteration++;

            // for each configuration of the robot
            for (int n1 = 0; n1 < N; n1++)
            {
                for (int n2 = 0; n2 < N; n2++)
                {
                    // for each future configuration of the robot
                    for (int m1 = 0; m1 < N; m1++)
                    {
                        if (isVisibleFrom(n1, m1))
                        {
                            for (int m2 = 0; m2 < N; m2++)
                            {
                                if (isVisibleFrom(n2, m2))
                                {
                                    // Calculate next step and, if it is a better value, update the matrix d, v and path
                                    int[][]  Ahat = modifyAdjacency(A, r);
                                    DirtySet Dhat = d[n1][n2].multiplication(Ahat);
                                    int      Vhat = Dhat.cardinality();

                                    if (Vhat < v[m1][m2])
                                    {
                                        path[m1][m2].add(m1, m2);
                                        d[m1][m2] = Dhat;
                                        v[m1][m2] = Vhat;
                                    }

                                    // check for the best path so far
                                    if (v[m1][m2] < Vbest)
                                    {
                                        Vbest = v[m1][m2];
                                        Pbest = path[m1][m2];
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        return(Pbest);
    }
示例#5
0
    private int find(DirtySet d, ArrayList list)
    {
        int index = -1;

        for (int i = 0; i < list.Count && index == -1; i++)
        {
            if (d.equal((DirtySet)list[i]))
            {
                index = i;
            }
        }

        return(index);
    }
示例#6
0
    public bool equal(DirtySet cmp)
    {
        bool e = true;

        for (int n = 0; n < N && e; n++)
        {
            if (dirtySet[n] != cmp.dirtySet[n])
            {
                e = false;
            }
        }

        return(e);
    }
示例#7
0
    private void findDirtySetTest()
    {
        DirtySet d1 = new DirtySet(N);
        DirtySet d2 = new DirtySet(N);
        DirtySet d3 = new DirtySet(N);
        DirtySet d4 = new DirtySet(N);

        d1.dirtySet = new int[] { 1, 0, 0, 0, 0 };
        d2.dirtySet = new int[] { 0, 0, 1, 0, 0 };
        d3.dirtySet = new int[] { 0, 0, 0, 0, 1 };
        d4.dirtySet = new int[] { 0, 0, 0, 0, 0 };

        ArrayList Q = new ArrayList();

        Q.Add(d1);
        Q.Add(d2);
        Q.Add(d3);

        Debug.Log(find(d4, Q));
    }
示例#8
0
    public void inizializzation()
    {
        // Assign first value to all data
        DirtySet d = new DirtySet(N);

        d.calculateDirtySet(r);
        d.updateVisibility(r, A);
        //d.print ();
        Q.Add(d);

        //for (int i = 0; i < Np2; i++) {
        x [0] [0] = r [0];
        x [0] [1] = r [1];

        //path[0].add (r);
        v[0] = d.cardinality();

        Vbest = v[0];
        Pbest = path[0];
    }
示例#9
0
    public void PEOneStep()
    {
        // for each configuration of the robot
        for (int n1 = 0; n1 < N; n1++)
        {
            for (int n2 = 0; n2 < N; n2++)
            {
                // for each future configuration of the robot
                for (int m1 = 0; m1 < N; m1++)
                {
                    if (isVisibleFrom(n1, m1))
                    {
                        for (int m2 = 0; m2 < N; m2++)
                        {
                            if (isVisibleFrom(n2, m2))
                            {
                                // Calculate next step and, if it is a better value, update the matrix d, v and path
                                int[][]  Ahat = modifyAdjacency(A, r);
                                DirtySet Dhat = d[n1][n2].multiplication(Ahat);
                                int      Vhat = Dhat.cardinality();

                                if (Vhat < v[m1][m2])
                                {
                                    path[m1][m2].add(m1, m2);
                                    d[m1][m2] = Dhat;
                                    v[m1][m2] = Vhat;
                                }

                                // check for the best path so far
                                if (v[m1][m2] < Vbest)
                                {
                                    Vbest = v[m1][m2];
                                    Pbest = path[m1][m2];
                                }
                            }
                        }
                    }
                }
            }
        }
    }
示例#10
0
    private void testMultiplication()
    {
        DirtySet d = new DirtySet(N);

        d.dirtySet = new int[] { 1, 0, 0, 0, 1 };
        int[] robots = new int[] { 4, 1 };

        int[][] Ahat = modifyAdjacency(A, robots);

        /*
         * for (int i=0; i<N; i++) {
         *      string log = "";
         *      for (int j=0; j<N; j++) {
         *              log += Ahat[i][j] + " ";
         *      }
         *      Debug.Log(log);
         * }*/
        DirtySet Dhat = d.multiplication(Ahat);

        Dhat.print();
    }
示例#11
0
    public void PEOneStep()
    {
        int Qsize = Q.Count;

        //Debug.Log ("Q size "+Qsize);
        // for each configuratioQ.Countn of the robot
        for (int d = 0; d < Qsize; d++)
        {
            DirtySet D = (DirtySet)Q[d];

            // try to move one robot at a time
            for (int i = 0; i < 2; i++)
            {
                int[] Xhat = new int[2] {
                    x[d][0], x[d][1]
                };
                if (debugOn)
                {
                    Debug.Log("X hat (" + Xhat[0] + ", " + Xhat[1] + ")");
                }

                // find the next node for the robot i
                for (int m = 0; m < N; m++)
                {
                    if (x[d][i] != m && isVisibleFrom(x[d][i], m))
                    {
                        Xhat[i] = m;

                        int[][] Ahat = modifyAdjacency(A, Xhat);

                        //Debug.Log("new robots: "+Xhat[0]+" "+Xhat[1]);
                        /***************/

                        /*
                         * string log = "";
                         * for (int k=0; k<N; k++) {
                         *      for (int j=0; j<N; j++) {
                         *              log += Ahat[k][j] + " ";
                         *      }
                         *      log += "\n";
                         * }
                         * Debug.Log(log);*/
                        /*******/
                        DirtySet Dhat = D.multiplication(Ahat);
                        if (debugOn)
                        {
                            Debug.Log("M: " + m + " Dhat: ");
                        }
                        if (debugOn)
                        {
                            Dhat.print();
                        }

                        int Vhat = Dhat.cardinality();

                        int Dindex = find(Dhat, Q);
                        if (Dindex == -1)
                        {
                            // new dirty set
                            x[Qsize] = Xhat;

                            path[Qsize] = new Path(path[d]);
                            path[Qsize].add(Xhat);

                            //Debug.Log("Q: "+Qsize);
                            //path[Qsize].print();
                            v[Qsize] = Vhat;

                            Q.Add(Dhat);
                            Dindex = Qsize;


                            if (Vhat == Vbest && path[Dindex].path.Count < Pbest.path.Count)
                            {
                                Pbest = new Path(path[Dindex]);
                                Vbest = Vhat;
                            }
                            else if (Vhat < Vbest)
                            {
                                Pbest = new Path(path[Dindex]);
                                Vbest = Vhat;
                            }
                        }
                        else if (Vhat < v[Dindex] || (Vhat == v[Dindex] &&
                                                      dist(Xhat, Dhat.dirtySet) < dist(x[d], Dhat.dirtySet)))
                        {
                            // update old dirty set with new info

                            //Debug.Log(dist(Xhat, Dhat.dirtySet));
                            x[Dindex] = Xhat;
                            //Debug.Log("Before: ");
                            //path[Qsize].print();
                            path[Dindex].add(Xhat);
                            //Debug.Log("Q: "+Dindex);
                            //path[Qsize].print();
                            v[Dindex] = Vhat;

                            if (Vhat == Vbest && path[Dindex].path.Count < Pbest.path.Count)
                            {
                                Pbest = new Path(path[Dindex]);
                                Vbest = Vhat;
                            }
                            else if (Vhat < Vbest)
                            {
                                Pbest = new Path(path[Dindex]);
                                Vbest = Vhat;
                            }
                        }

                        /*
                         * if ( Vhat == Vbest) {
                         *              if (path[Dindex].path.Count < Pbest.path.Count) {
                         *                      Pbest = new Path(path[Dindex]);
                         *                      Vbest = Vhat;
                         *              }
                         *      } else if ( Vhat < Vbest) {
                         *              Pbest = new Path(path[Dindex]);
                         *              Vbest = Vhat;
                         *      }*/
                    }
                }
            }
        }
    }