# Placement algorithm assistance

• August 6th, 2012, 06:20 AM
ipodman
Placement algorithm assistance
First of all thank you to everyone who has posted in threads that I have read throughout my time. 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 algorithm focuses on placement based on user requirements - 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.

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
• August 6th, 2012, 09:06 AM
Norm
Re: Placement algorithm assistance
Also posted at Placement algorithm assistance - Dev Shed

Can you post code showing what you have tried so far?
• August 6th, 2012, 09:13 AM
ipodman
Re: Placement algorithm assistance
Quote:

Originally Posted by Norm
Also posted at Placement algorithm assistance - Dev Shed

Can you post code showing what you have tried so far?

Posted in an attempt to get wider readership - I'll bring together a smaller example to demonstrate and reply back. As stated previously, its part of a larger project, however I do acknowledge I do need to provide an example, for which I will do soon.

Regards

Dan
• August 7th, 2012, 05:33 AM
ipodman
Re: Placement algorithm assistance
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 :

```  public class placement {     /** 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;   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); Collection c = userData.keySet(); 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);   Collection c = houses.values();   Iterator itr = c.iterator();   while (itr.hasNext()) System.out.println("houses " + itr.next()); }   }```

Thanks,

Dan