Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: PriorityQueue Enquiries

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PriorityQueue Enquiries

    Hi All,

    I try to google around for how can i do the following thing in priorityqueue but there is no answer for this.

    It is actually quite straight forward, lets said i have a set of PriorityQueue with few items inside and I want to get the object n in the pq and do a comparison, if the path is higher cost, replace with a new object n. The Pseudo code is something like this.

    If Child.STATE is in PriorityQueue with Higer PATH-COST then
    replace that PriorityQueue with the Child.STATE

    Anyone able to guide me on this? Coz i believe PriorityQueue does not have the GET function.

    Cheers.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: PriorityQueue Enquiries

    Would contains and remove do what you want?

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: PriorityQueue Enquiries

    Quote Originally Posted by Norm View Post
    Would contains and remove do what you want?
    I would like to get the Object N from the priority Queue and do a comparison with current object .. if current object contains lesser cost-path, it will replace the object N..

    2 Object would have same attribute different path cost ...

    Is it more like a travel using UCS ..

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: PriorityQueue Enquiries

    Can you write a small simple program that compiles and executes to show what you want to do?

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: PriorityQueue Enquiries

    Quote Originally Posted by Norm View Post
    Can you write a small simple program that compiles and executes to show what you want to do?
    My program is something like this.

    I have an input files as follow

    5 5
    1 1
    5 5

    1 1 1 1 1
    1 1 1 1 1
    1 1 1 1 1
    1 1 1 1 1

    5 5 = indicates Grid Size
    1 1 = indicates Start Location
    5 5 = indicates Goal Location
    The travel path will be replaced by *

    The real answer should be

    * 1 1 1 1
    * 1 1 2 1
    * 1 3 1 1
    * 1 1 1 1
    * * * * *

    It would use priority queue to compare if terrain level is higher or same level using the following formula.

    If Node(c,d) - Node(a,b) > 0 then
    Path Cost = 1 + Node(c,d)[Terrain Level] - Node(a,b)[Terrain Level] + Parent Path Cost

    The start goal will have 0 path cost.

    It should travel by 4 direction in sequence, Up - Down - Left - Right

    My output files as follow:-

    * * * * *
    1 1 1 2 *
    1 1 3 1 *
    1 1 1 1 *
    1 1 1 1 *

    So, what i suspect my program wrong is because i do not compare the path cost which contain the node.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: PriorityQueue Enquiries

    sorry I meant a java program that compiles and executes to show what you want to do. You know, with classes and methods that execute.

  7. #7
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: PriorityQueue Enquiries

    Quote Originally Posted by keat84 View Post
    I would like to get the Object N from the priority Queue
    A queue is a queue - get(int) works on a list. If you want to get the nth item based on a 'cost' you'll have to use Collections.sort on a list (or Arrays.sort on an array) and pass it a Comparator that uses your cost calculation (if it's not already your object's natural order).

Similar Threads

  1. unable to add my array(Words[]) to PriorityQueue
    By Harini Rao in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 10th, 2012, 07:08 AM