Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by GutoThomas · May 18, 2012 at 02:53 PM · pathfindingbinaryheap

Hep with binary heap poll

Hey everyone. Actually, after putting my A* to work with some Array.Sort to get the Node list adapted by F score, I'm trying to reach some better performance and finded out that the way to do this would be implementing Binary Heap into my pathfinding.

When I add an element to the list everything goes fine. It's added in his proper position. The problem is when I want to remove an item, sometimes it seems to work, but sometimes not, and the lower F cost node still in some other position in the array which is not actually 0.

This is the code I did to do the poll from the binary heap:

 void binaryHeapPoll(int Index) {
         
         if(openList.Count == 0) { return; }
         
         openList[Index] = openList[openList.Count - 1];
         
         openList.RemoveAt(openList.Count - 1);
         
         int current = Index;
         
         while(current < openList.Count) {
             
             int leftChild = leftChildOf(current);
             int rightChild = rightChildOf(current);
             
                 if(rightChild < openList.Count) {
                 
                     if((openList[leftChild].F < openList[current].F) && (openList[leftChild].F < openList[rightChild].F)) {
                     
                         current = binaryHeapSwap(current, leftChild, false); // this just swap the two items and return me the highest index between current and leftChild
                     
                     } else if ((openList[rightChild].F < openList[current].F) && (openList[rightChild].F < openList[leftChild].F)) {
                     
                         current = binaryHeapSwap(current, rightChild, false);
                     
                     } else { return; }
                 
                 } else if (leftChild < openList.Count) {
                 
                     if(openList[leftChild].F < openList[current].F) {
                     
                         current = binaryHeapSwap(current, leftChild, false);
                         return;
                     
                     } else { return; }
                 
                 } else { return; }
             
             }
         
         }


The Add method and some other methods I'm using in the binary heap you can see below:

 int binaryHeapSwap(int indexOne, int indexTwo, bool returnMinimumIndex) {
         
         int iOne = indexOne;
         int iTwo = indexTwo;
         int iMinimum = Mathf.Min(indexOne, indexTwo);
         int iMaximum = Mathf.Max(indexOne, indexTwo);
         
         Node indexOneNode = openList[iOne];
         openList[iOne] = openList[iTwo];
         openList[iTwo] = indexOneNode;
         
         if(returnMinimumIndex)  {return iMinimum;} 
         
         else                     {return iMaximum;}
     
     }



 void binaryHeapOffer(Node nodeToInsert) {
         
         Node node = nodeToInsert;
         
         openList.Add(node);
         
         int currentIndex = openList.Count - 1;
         
         while(currentIndex > 0) {
             
             int parentIndex = parentOf(currentIndex);
             
             if(openList[currentIndex].F < openList[parentIndex].F) {
                 
                 currentIndex = binaryHeapSwap(currentIndex, parentIndex, true);
                 
             } else { break; }
             
         }
     
     }


 int leftChildOf(int index) {
         
         return ((index << 1) + 1);
         
     }
     
     int rightChildOf(int index) {
         
         return ((index << 1) + 2);
         
     }
     
     int parentOf(int index) {
         
         return((index - 1) >> 1);
         
     }


Really hope someone can help me with this. I spent like 3 hours last night and didn't get any different result, but just with the poll.

Any help would be appreciated, from code to just logic tips. =)

Thanks from now!

Cheers!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by GutoThomas · May 18, 2012 at 03:57 PM

The following code solve the problems!

 void binaryHeapPoll(int Index) {
             
             if(openList.Count == 0) { return; }
             
             openList[Index] = openList[openList.Count - 1];
             
             openList.RemoveAt(openList.Count - 1);
             
             int current = Index;
             
             while(current < openList.Count) {
                 
                 int leftChild = leftChildOf(current);
                 int rightChild = rightChildOf(current);
                 
                     if(rightChild < openList.Count) {
                     
                         if((openList[leftChild].F < openList[rightChild].F)) {
                         
                             if(openList[leftChild].F < openList[current].F) {
                             
                             current = binaryHeapSwap(leftChild, current, false);
                             
                         } else { return; }
                         
                         } else {
                         
                         if(openList[rightChild].F < openList[current].F) {
                             
                             current = binaryHeapSwap(rightChild, current, false);
                             
                         } else { return; }
                         
                     }
                     
                 } else if (leftChild < openList.Count) {
                     
                         if(openList[leftChild].F < openList[current].F) {
                         
                             current = binaryHeapSwap(current, leftChild, false);
                             return;
                         
                         } else { return; }
                     
                     } else { return; }
                 
                 }
             
             }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Change location of binary file created when saving 1 Answer

Where are binary heaps used? 0 Answers

Binary Heap Minimum value 1 Answer

What's the best way to keep an object from falling off of something? 1 Answer

A* Pathfinding- A moving target 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges