![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
|
I'm doing a project which involves reading in a list of people from a file then matching them up in the most efficiant way possible. For anything that they both like, the 'dating agency' get £10 but for everything one of them likes and the other dislikes, the agency loses £5.
The file is a txt file that has the format: Gender "m" or "f" Surname Forename Likes (stored as a string but each letter represents one of 6 things, e.g fot would be f = football, o = opera and t = theatre) Dislikes (stored in the same way above) I've read from the file and put the men in one ArrayList and the women in another ArrayList An attempt to optomise profit is all thats needed (as there will be an unknown number of customers and so covering all possibilities would be virtually impossible) so my idea was to look at each man and find his optimum woman. Obviously, this may not be optimum so I will repeat it a few times starting with a different man or woman but only after I sort this code... I get an IndexOutOfBoundsException at line 126 of the code show below. I understand that this means I'm looking for a position in the ArrayList that doesn't exist yet but I'm sure it does. This tells me I've made a logical error somewhere but I have no idea where ![]() Also, I know this code currently allows polygamy but I haven't completely finished yet ![]() This code is the method thats giving me hell... 78 public void matchMaker(){
79 ArrayList bestMatch = new ArrayList();
80
... Iterator i = manArray.iterator();
int count = 0;
int countW = 0;
int highestSoFar = 0;
int position = 0;
while (i.hasNext()){
int profit = 0;
Customer thisCustomer = (Customer)i.next();
for (int likePos = 0; likePos < thisCustomer.getLikes().length(); likePos++){
char heLikes = thisCustomer.getLikes().charAt(likePos);
Iterator j = womanArray.iterator();
countW = 0;
while (j.hasNext()){
profit = 0;
Customer thatCustomer = (Customer)j.next();
for (int likePosW = 0; likePosW < thatCustomer.getLikes().length(); likePosW++){
char sheLikes = thatCustomer.getLikes().charAt(likePosW);
if (heLikes == sheLikes){
profit = profit+10;
}
}
for (int dislikePosW = 0; dislikePosW < thatCustomer.getDislikes().length(); dislikePosW++){
char sheDislikes = thatCustomer.getDislikes().charAt(dislikePosW);
if (heLikes == sheDislikes){
profit = profit-5;
}
}
if (profit > highestSoFar){
highestSoFar = profit;
position = countW;
}
countW++;
}
}
Customer thatCustomer = (Customer)womanArray.get(position);
if (bestMatch.isEmpty()){
Matched isMatched = new Matched
(thisCustomer.getGender(), thisCustomer.getSurname(), thisCustomer.getForename(), thisCustomer.getLikes(), thisCustomer.getDislikes(),
thatCustomer.getGender(), thatCustomer.getSurname(), thatCustomer.getForename(), thatCustomer.getLikes(), thatCustomer.getDislikes(), profit);
bestMatch.add(isMatched);
}
else{
126 Matched thisMatch = (Matched)bestMatch.get(count);
int arrayProfit = thisMatch.getProfit();
if (profit > arrayProfit){
Matched isMatched = new Matched
(thisCustomer.getGender(), thisCustomer.getSurname(), thisCustomer.getForename(), thisCustomer.getLikes(), thisCustomer.getDislikes(),
thatCustomer.getGender(), thatCustomer.getSurname(), thatCustomer.getForename(), thatCustomer.getLikes(), thatCustomer.getDislikes(), profit);
bestMatch.add(count, isMatched);
bestMatch.remove(count);
}
... }
136 count++;
137 }
138 }I'm not gonna post ALL my code but if anyone wants it, I'll post it later. Sorry this is such a long post. Phew! |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
I don't see a condition to limit "count" to bestMatch's length so it can easily go out of bound. Try some debugging, print "count" for every "while" iteration and see what number you're erroring at.
|
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
Make the orignal array size bigger, maybe?
__________________
Hoes telling me to calm down but I'm like fuck that shit!
|
|
|
|
|
|
#4 |
|
Newbie
|
Thanks so much for your promt replies!
I did have a look at what you mentioned regarding the 'count' and spotted a different potential problem. I ended up re-writing the method ![]() The array size shouldn't have affected it because I was using an ArrayList which told me the array wasn't being filled and the 'count' variable was going too high and therefore throwing the runtime error. I didn't limit the count, as you suggested because the idea was that the loop can only go so far and count would then be reset when it checks a new man against all the women. It now compiles fine (like before) but it now runs without any errors!! That is, until I tested with my 'printMatched' method... (I think its fairly obvious what that method does :p Our lecturer always nags about 'meaningful names' (one thing I'm generally not very good at!)) Anyway, I'm still working on it but now its bed time :p I just wanted to thank you for your promt response earlier and I'm sure I'll be back for more help later, heh heh. Last edited by RandomRetard; Dec 16th, 2005 at 6:50 PM. |
|
|
|
|
|
#5 |
|
Newbie
|
So I'm back from my Xmas break and it's straight back to coding! The only problem... I don't know where to begin to fix my errors
![]() As much as I dislike BlueJ, I've been trying to use its debugger because its generally quite good. Unfortunatly, this doesn't seem to be working either! None of my variables are being recognised in the debugger. For example, as the code goes past 'int count = 0;' nothing appears in the debugger. It doesn't even come up as null! It's like the line doesn't exist and this continues throughout the code. So I can't see whats happening and therefore I can't see whats going wrong in my code. So now it's time to turn to my other debugger... The internet:p Here is the re-written code. As far as I can tell, the problem is that nothing is being added to the matched couples ArrayList but I suppose the error could be anywhere. public void matchMaker(){
ArrayList bestMatch = new ArrayList();
Iterator i = manArray.iterator();
int count = 0;
int countW = 0;
int highestSoFar = -100;
int position = 0;
int profit = 0;
while (i.hasNext()){
Customer thisCustomer = (Customer)i.next();
Iterator j = womanArray.iterator();
countW = 0;
while (j.hasNext()){
profit = 0;
Customer thatCustomer = (Customer)i.next();
for (int likePos = 0; likePos < thisCustomer.getLikes().length(); likePos++){
char heLikes = thisCustomer.getLikes().charAt(likePos);
for (int likePosW = 0; likePosW < thatCustomer.getLikes().length(); likePos++){
char sheLikes = thatCustomer.getLikes().charAt(likePosW);
if (heLikes == sheLikes){
profit = profit + 10;
}
}
for (int dislikePosW = 0; dislikePosW < thatCustomer.getDislikes().length(); dislikePosW++){
char sheDislikes = thatCustomer.getDislikes().charAt(dislikePosW);
if (heLikes == sheDislikes){
profit = profit - 5;
}
}
}
for (int dislikePos = 0; dislikePos < thisCustomer. getDislikes().length(); dislikePos++){
char heDislikes = thisCustomer.getDislikes().charAt(dislikePos);
for (int likePosW = 0; likePosW < thatCustomer.getLikes().length(); likePosW++){
char sheLikes = thatCustomer.getLikes().charAt(likePosW);
if (heDislikes == sheLikes){
profit = profit - 5;
}
}
}
if (profit > highestSoFar){
highestSoFar = profit;
position = countW;
}
countW++;
}
Customer thatCustomer = (Customer)womanArray.get(position);
if (bestMatch.isEmpty()){
Matched isMatched = new Matched
(thisCustomer.getGender(), thisCustomer.getSurname(), thisCustomer.getForename(), thisCustomer.getLikes(), thisCustomer.getDislikes(),
thatCustomer.getGender(), thatCustomer.getSurname(), thatCustomer.getForename(), thatCustomer.getLikes(), thatCustomer.getDislikes(), profit);
bestMatch.add(isMatched);
}
else{
Matched thisMatch = (Matched)bestMatch.get(count - 1);
int arrayProfit = thisMatch.getProfit();
if (profit > arrayProfit){
Matched isMatched = new Matched
(thisCustomer.getGender(), thisCustomer.getSurname(), thisCustomer.getForename(), thisCustomer.getLikes(), thisCustomer.getDislikes(),
thatCustomer.getGender(), thatCustomer.getSurname(), thatCustomer.getForename(), thatCustomer.getLikes(), thatCustomer.getDislikes(), profit);
bestMatch.remove(count-1);
bestMatch.add(count - 1, isMatched);
}
}
count++;
}
}
public void printMatched(){
Iterator x = bestMatch.iterator();
while (x.hasNext()){
Matched thisMatch = (Matched)x.next();
System.out.println(thisMatch.getGenderM()+" + "+thisMatch.getGenderW());
System.out.println(thisMatch.getSurnameM()+" + "+thisMatch.getSurnameW());
System.out.println(thisMatch.getForenameM()+" + "+thisMatch.getForenameW());
System.out.println(thisMatch.getLikesM()+" + "+thisMatch.getLikesW());
System.out.println(thisMatch.getDislikesM()+" + "+thisMatch.getDislikesW());
System.out.println("");
}
}Thanks in advance. This has been driving me crazy! |
|
|
|
|
|
#6 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
Did you set a breakpoint on the line?
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#7 |
|
Newbie
|
Thanks for your reply Dameon but I'm not quite sure what you mean. Could you clarify that a bit please?
|
|
|
|
|
|
#8 |
|
Newbie
|
|
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Dec 2005
Posts: 118
Rep Power: 0
![]() |
One thing to note - Hungarian matching is a reasonably fast algorithm and finds ideal matchings, although it may be a bit complicated for this situation...
|
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Dec 2005
Posts: 118
Rep Power: 0
![]() |
It's a bit difficult to test without a data file :p
Also, I think you may still have a bit to learn about object-oriented programming. For example, why not have member functions in the Customer class to (1) read the customer's details from a file (2) write them to screen (3) compare this person to another customer and calculate the profit of matching them together? It'll make your code a lot neater and reduce some of your majorly nested loops to a few function calls. Then for your matched class, why are you transferring across all the details? Why not just have two Customer references for the people you've matched? I think you're making some things harder on yourself than necessary. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|