#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2005
    Location
    Manchester, Uk
    Posts
    6
    Rep Power
    0

    Placement algorithm assistance


    I'm a big lurker on these forums and generally surf over and read threads. Until today, I have a little problem that I was hoping someone could provide a little guidance.

    The code focuses on placement based on user requirement - I'll try to explain below.

    Users: 10 users - Simple user id (userid) and user allowed (userallowed = 1 or 0)

    Houses: 5 houses that are stored in a Map - all the same and nothing unique about each one (unique ids).

    The aim is to place the 10 users into each of the 5 houses, however when a user is not allowed (userallowed = 0) they are placed in a house thats "secured" and only other disallowed users (userallowed = 0 ) are able to placed. All other users are shared amongst the other 4 houses (for example).

    My approach so far has been to use a for loop which an if statement, however this has been unsuccessful and I'm looking for some guidance as to how best to proceed.

    Regards,

    Dan
  2. #2
  3. No Profile Picture
    rebel with a cause
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2004
    Location
    The Batsh!t Crazy State.
    Posts
    5,817
    Rep Power
    3462
    Can you share the code you tried and an example of what you were expecting?
    Dear God. What is it like in your funny little brains? It must be so boring.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2005
    Location
    Manchester, Uk
    Posts
    6
    Rep Power
    0
    The code is part of a very much larger project, I've tried to break is down to its basic elements (as stated above).

    The expected output would be: if you have 10 users, but only one is not allowed (disallowed = 0) then one house would have this user in, and the other 4 houses would have the other 9 in.

    As stated previously, the code is part of a larger project but I was looking at exploring something such as (not working example for concept):

    for(int i = 0; i < houses.size(); i++){ // cycle through the 5 houses

    //This is where I lose the ability, I was looking at having some sort of IF statement here to check if the house first of all had a disallowed user, if not then based on the user (if they are allowed or disallowed) place them. However, if another house has one disallowed user and the user requesting to be placed is disallowed to they need to be placed together.

    }


    crownjewel82 thank you for your initial reply.

    Dan
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    A small program that compiles, executes and shows the problem would be helpful.
  8. #5
  9. No Profile Picture
    rebel with a cause
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2004
    Location
    The Batsh!t Crazy State.
    Posts
    5,817
    Rep Power
    3462
    Your algorithm still isn't completely clear.

    If I have users 1 - 10 and houses A - E, and all 10 are allowed how would the users be distributed? 2 in each? Would 1 and 2 be in the same house or would 1 and 5 be in the same house?

    If user 1 and 2 are disallowed are houses A and B secured or is only house A secured?

    Can it ever be the case that all 5 houses are secured and the other users cannot be placed?

    You're right about the method of using a loop and an if statement you just need to clarify the rules to build the conditions.
    Dear God. What is it like in your funny little brains? It must be so boring.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2005
    Location
    Manchester, Uk
    Posts
    6
    Rep Power
    0
    Thanks for the reply crownjewel82 - I'll bring together a basic example to better make clear what I am attempting to do.

    In the mean time, users 1- 10 and houses A - E. The users would simple be allocated to the next free house and if there is no possibility of a place (for example due to all 5 houses being used by allowed users) then they would not be placed (rejected). A house can have multiple users therefore if all of the users require to be secured they can all be allocated to one house.

    I hope that aids in clarity, I'll update when I have a working example, thanks for taking the time to read and reply.

    Dan
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2005
    Location
    Manchester, Uk
    Posts
    6
    Rep Power
    0
    I've put together a very basic and raw example. Below are a few scenarios:

    Example 1: If user 1/8 is not allowed, the first house in the foor loop to allocated will be house 1, hereafter only disallowed users (1/8) would be allowed to be placed in this house. All other users would be distributed to other houses.

    Example 2: if user 5/6 are disallowed users, all houses have already been allocated to allowed users then they will simply be rejected as no secure house can be offered.

    The code is very basic and provides a starting point, thank you all who have so far taken time to answer my questions.

    Code:
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    
    public class placement {
    
    	private List<Integer> hostTble;
    
    	/** Linking the user to a house **/
    	private static Map<Integer, Integer> housesUsed;
    
    	/** Hold user data **/
    	private static Map<Integer, Integer> userData;
    
    	/** Hold houses **/
    	private static Map<Integer, Integer> houses;
    
    	public static void main(String[] args) {
    		setUsers();
    		setHouses();
    		housesUsed = new HashMap<Integer, Integer>();
    
    		int housePosition = 0;
    		System.out.println("Placing user with houses");
    		
    		for (int a = 0; a < userData.size(); a++) {
    
    			for (int i = 0; i < houses.size(); i++) {
    
    				if (!housesUsed.containsKey(a)) {
    					housePosition = i;
    					housesUsed.put(housePosition, a);
    
    				}
    
    			}
    			System.out.println("House id: " + housePosition + " User id: " + a);
    
    		}
    
    	}
    
    	public static void setUsers() {
    		userData = new HashMap<Integer, Integer>();
    		// User id followed by allowed (o not / 1 allowed)
    		userData.put(0, 0);
    		userData.put(1, 1);
    		userData.put(2, 1);
    		userData.put(3, 1);
    		userData.put(4, 0);
    		userData.put(5, 1);
    		userData.put(6, 1);
    		userData.put(7, 0);
    		userData.put(8, 1);
    		userData.put(9, 1);
    		System.out.println("Users created with following validation: ");
    		Collection c = userData.values();
    
    		Iterator itr = c.iterator();
    
    		while (itr.hasNext())
    			System.out.println("user id " + itr.next());
    	}
    
    	public static void setHouses() {
    		houses = new HashMap<Integer, Integer>();
    		houses.put(0, 0);
    		houses.put(1, 1);
    		houses.put(2, 2);
    		houses.put(3, 3);
    		houses.put(4, 4);
    System.out.println("Houses created");
    		Collection c = houses.values();
    
    		Iterator itr = c.iterator();
    
    		while (itr.hasNext())
    			System.out.println("houses " + itr.next());
    	}
    
    }
    Output currently:
    Code:
    Users created with following validation: 
    user id 0
    user id 1
    user id 1
    user id 1
    user id 0
    user id 1
    user id 1
    user id 0
    user id 1
    user id 1
    Houses created
    houses 0
    houses 1
    houses 2
    houses 3
    houses 4
    Placing user with houses
    House id: 0 User id: 0
    House id: 1 User id: 1
    House id: 2 User id: 2
    House id: 3 User id: 3
    House id: 4 User id: 4
    House id: 4 User id: 5
    House id: 4 User id: 6
    House id: 4 User id: 7
    House id: 4 User id: 8
    House id: 4 User id: 9
    The main aim is for it to either co-locate users (who are not valid, represented by the 0), of if that's not possible reject their placement. However, it should also place the others users to houses (as shown below) The problem is how do I go about positioning the users based on their requirements. Based on the files it should be like the following:

    House 0: user 0 (0); user 4 (0); user 7 (0)
    House 1: user 1 (1); user 6 (1)
    House 2: user 2 (1); user 8 (1)
    House 3: user 3 (1); user 9 (1)
    House 4: user 5 (1)



    Thanks,

    Dan
    Last edited by ipodman; August 7th, 2012 at 08:24 AM. Reason: Requested update from user
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Can you fix the posted code so it compiles, executes and shows the problem?
    The posted code gives 10 compiler errors. One problem is missing import statements.

    Also what does the execution of this code show?
    Can you post its output and explain what is wrong with it and post what the output should look like.
    Last edited by NormR; August 7th, 2012 at 07:36 AM.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2005
    Location
    Manchester, Uk
    Posts
    6
    Rep Power
    0
    First of all thank you for taking the time to reply, I've updated the previous post with the information you've asked for.

    Thanks again,

    Dan
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    The formatting of the program's output:
    Placing user with houses
    House id: 0 User id: 0
    House id: 1 User id: 1
    House id: 2 User id: 2
    House id: 3 User id: 3
    House id: 4 User id: 4
    House id: 4 User id: 5
    House id: 4 User id: 6
    House id: 4 User id: 7
    House id: 4 User id: 8
    House id: 4 User id: 9
    Is different from the desired output:
    House 0: user 0 (0); user 4 (0); user 7 (0)
    House 1: user 1 (1); user 6 (1)
    House 2: user 2 (1); user 8 (1)
    House 3: user 3 (1); user 9 (1)
    House 4: user 5 (1)
    For example the numbers in()s? How do you go from one to the other?

    EDIT: Can you explain what the loop in the main() method is supposed to do?
    Last edited by NormR; August 7th, 2012 at 10:00 AM.
  20. #11
  21. No Profile Picture
    rebel with a cause
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2004
    Location
    The Batsh!t Crazy State.
    Posts
    5,817
    Rep Power
    3462
    Start by making sure that the valid (1) users are distributed correctly. That should be a straight forward loop through the users and a FIFO queue for the houses. Just push the house back to the end of the queue after it's been used. (You don't actually have to use the queue implementation to make this work.)

    Then you need to check to see if a user is invalid (0) if they are then you need to put them in the next empty house and remove that house from the queue and store it elsewhere. Then, every time you hit an invalid user it goes to that house instead of to the next valid house.

    Just take your time and implement one rule at a time.
    Dear God. What is it like in your funny little brains? It must be so boring.

IMN logo majestic logo threadwatch logo seochat tools logo