public static List <int[]> GetPermutation(int[] list)
    {
        List <int[]> permutations = new List <int[]>();

        foreach (int[] permutation in PairPermutation.AllFor(list))
        {
            //string pair = string.Join(" ", permutation);
            //Debug.Log(pair);
            permutations.Add(permutation);
        }
        return(permutations);
    }
示例#2
0
    // Update is called once per frame
    void FixedUpdate()
    {
        if (timeCounter - lastPrint > 1)
        {
            lastPrint = timeCounter;
        }

        //Debug.LogFormat("{0} {1}", eventTimer, EVENT_INTERVAL);
        // Check if to generate new random event
        if (eventTimer > EVENT_INTERVAL)
        {
            eventTimer = 0;
            //Debug.Log("New Event Attempt");

            if (waitingEventsId.Count + ongoingEventsId.Count < shelves.Length - 1)
            {
                //  Debug.Log("New Event Created");

                int newIdx = GenRandEvent();
                waitingEventsId.Add(newIdx);
                totalEventCounter++;
            }
        }

        // try to gaurentee there are 6 crashes per minute
        if (sixtysecsCounter < 60)
        {
            sixtysecsCounter += Time.fixedDeltaTime;
        }
        else
        {
            currMinuteCrashCounter = 0;
            sixtysecsCounter       = 0;
        }

        if (availableDronesId.Count > 0 && waitingEventsId.Count > 0)
        {
            if (currMinuteCrashCounter >= 6)
            {
                Debug.Log("???????????????????????");
                PairPermutation formPermute  = new PairPermutation();
                int[]           droneIDArray = availableDronesId.getList().ToArray();
                int[]           eventIDArray = waitingEventsId.getList().ToArray();
                Debug.Log(string.Join(" ", droneIDArray));
                List <int[]> dronePermutations = PairPermutation.GetPermutation(droneIDArray);

                int  counter     = 0;
                bool hasSolution = false;
                foreach (int[] permute in dronePermutations)
                {
                    int         numOptions       = permute.Length;
                    int         size             = numOptions * numOptions;
                    List <bool> pairIntersection = new List <bool>();
                    bool        collision        = false;
                    for (int i = 0; i < numOptions - 1; i++)
                    {
                        for (int j = 0; j < numOptions - 1; j++)
                        {
                            Vector3 p1, p2, p3, p4;
                            p1        = eventsDict[eventIDArray[i]].pos;
                            p2        = dronesDict[droneIDArray[i]].hoverPos;
                            p3        = eventsDict[eventIDArray[j]].pos;
                            p4        = dronesDict[droneIDArray[j]].hoverPos;
                            collision = IsWithinCollisionBound(p1, p2, p3, p4);
                            Debug.LogFormat("Drone {0} Event {1} | Drone {2} Event{3} could crash!!!!!!!", droneIDArray[i], eventIDArray[i], droneIDArray[j], eventIDArray[j]);
                            if (collision)
                            {
                                break;
                            }
                        }
                        if (collision)
                        {
                            break;
                        }
                    }
                    if (!collision)
                    {
                        hasSolution = true;
                        break;
                    }
                    else
                    {
                        collision = false;
                        counter++;
                    }
                }
                if (hasSolution)
                {
                    int[] solution = dronePermutations[counter];
                    Utility.IS_RND_TAKEOFF = false;
                    OrderedSet <int> newAvailableDronesID = new OrderedSet <int>();
                    foreach (int item in solution)
                    {
                        newAvailableDronesID.Add(item);
                    }
                    availableDronesId = newAvailableDronesID;
                }
            }
            else
            {
                Utility.IS_RND_TAKEOFF = true;
            }
            int   e         = waitingEventsId.Next();
            int   d         = Utility.IS_RND_TAKEOFF ? availableDronesId.NextRnd() : availableDronesId.Next();
            Drone nextDrone = dronesDict[d];
            Event nextEvent = eventsDict[e];
            nextDrone.AddEvent(nextEvent);
            availableDronesId.Remove(d);
            workingDronesId.Add(d);
            waitingEventsId.Remove(e);
            ongoingEventsId.Add(e);
        }
        //if (availableDronesId.Count > 0 && waitingEventsId.Count > 0)
        //{
        //    //Debug.Log("Assigning event to drone");
        //    int e = waitingEventsId.NextRnd();
        //    //int e = waitingEventsId.Next();
        //    int d = Utility.IS_RND_TAKEOFF ? availableDronesId.NextRnd() : availableDronesId.Next();


        //    Drone avalibleDrone = dronesDict[d];

        //    avalibleDrone.AddEvent(eventsDict[e]);
        //    availableDronesId.Remove(d);
        //    workingDronesId.Add(d);
        //    waitingEventsId.Remove(e);
        //    ongoingEventsId.Add(e);
        //}

        // apply force meanwhile check collision
        foreach (int i in workingDronesId)
        {
            //Debug.Log("Drone Collision Loop 1");
            foreach (int j in workingDronesId)
            {
                if (i == j)
                {
                    continue;
                }
                //Debug.Log("Drone Collision Loop 2");
                Vector3 delta = dronesDict[i].gameObjectPointer.transform.Find("pCube2").gameObject.transform.position - dronesDict[j].gameObjectPointer.transform.Find("pCube2").gameObject.transform.position;
                float   dis   = delta.magnitude;

                //if (Utility.IsLessThan(delta, Utility.CUTOFF_INTERACT))
                if (dis < Utility.INTERACT_DIM)
                {
                    //if (Utility.IsLessThan(delta, Utility.COLLISION_BOUND))
                    if (dis < Utility.BOUND_DIM)
                    {
                        //Debug.Log("2. DroneID " + dronesDict[i].droneId + " Drone Collision");
                        if (!dronesDict[i].isCollided && !dronesDict[j].isCollided)
                        {
                            userError++;
                            currMinuteCrashCounter++;
                            Debug.LogFormat("===== Drone {0}, Drone {1} | COLLISION  =====", i, j);
                        }

                        dronesDict[i].isCollided = true;
                        dronesDict[j].isCollided = true;
                    }
                    else
                    {
                        systemError++;
                    }
                }
            }
            dronesDict[i].direction = Vector3.Normalize(dronesDict[i].dstPos - dronesDict[i].curPos);
        }


        // check status
        // move every working drone
        for (int i = 0; i < numDrones; i++)
        {
            Drone             currDrone = dronesDict[i];
            Drone.DroneStatus status    = currDrone.status;

            if (status == Drone.DroneStatus.PARKED)
            {
                continue;
            }

            Drone.MoveStatus moveStatus = currDrone.Move();


            if (moveStatus == Drone.MoveStatus.END_TO_SHELF)  // drone status 2 --> 3
            {
                ongoingEventsId.Remove(currDrone.eventId);
                if (!currDrone.isCollided)
                {
                    successEventCounter++;
                    Debug.LogFormat("Drone {0} | event {1} | COMPLETE", i, currDrone.eventId);
                }
                else
                {
                    Debug.LogFormat("Drone {0} event {1} | CRASH", i, currDrone.eventId);
                }
            }

            if (currDrone.status == Drone.DroneStatus.PARKED)
            {
                currDrone.isCollided = false;
                workingDronesId.Remove(i);
                availableDronesId.Add(i);
                ongoingEventsId.Remove(currDrone.eventId);
            }
        }
        // update counter
        timeCounter += Time.fixedDeltaTime;
        eventTimer  += Time.fixedDeltaTime;
        cleanCounter++;


#if IS_USER_STUDY
        if (SEED <= MAX_SEED)
        {
            if (timeCounter >= EXIT_TIME)
            {
                Debug.Log("====================End of a 3 minute user study=============================");
                //Debug.Log(SEED);
                ResetSim();

                ReloadScene();


                //QuitGame();
            }
        }
        else
        {
            Debug.Log("???????????????????????????");
            QuitGame();
        }
#endif
    }