Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Programming Exercise #4: Super fun edition (https://forums.graalonline.com/forums/showthread.php?t=80068)

Tolnaftate2004 06-13-2008 09:46 PM

Programming Exercise #4: Super fun edition
 
Today's challenge will include 2 problems:
  1. hard Suppose that you are organizing housing accommodations for a group of four hundred university students. Space is limited and only one hundred of the students will receive places in the dormitory. To complicate matters, the Dean has provided you with a list of pairs of incompatible students, and requested that no pair from this list appear in your final choice.
    The point of this exercise is to GENERATE DISCUSSION. We are not asking you to WRITE the code, merely speculate as to how you might do it. BUT IF YOU FEEL COMPELLED, write the code to accomodate arbitrary lists from the Dean.
  2. easy Solve this problem.

Note From Hell Raven: the 100 students are rooming in pairs of 2. So that is 50 pairs (I do not believe this was clear...)

DrakilorP2P 06-14-2008 12:47 AM

Only primes are incompatible to other numbers? There's only 61 primes below 400. Isn't this easily solvable by including everyone who isn't a prime until you run out of space?

cbk1994 06-14-2008 01:10 AM

Quote:

Originally Posted by DrakilorP2P (Post 1396934)
Only primes are incompatible to other numbers? There's only 61 primes below 400. Isn't this easily solvable by including everyone who isn't a prime until you run out of space?

I guess the problem is figuring out who is a prime and who isn't ... but if I recall that's quite easy.

DrakilorP2P 06-14-2008 03:01 AM

Quote:

Originally Posted by cbk1994 (Post 1396943)
I guess the problem is figuring out who is a prime and who isn't ... but if I recall that's quite easy.

It's basic level programming.

Tolnaftate2004 06-14-2008 03:15 AM

It doesn't matter who is included. Make it include only 100 people, I don't care... Finding primes is trivial. I'm sure I could go online and find a list of them, this is not the point of the exercise.

I have done this for you:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397

cbk1994 06-14-2008 04:25 AM

Quote:

Originally Posted by Tolnaftate2004 (Post 1396955)
It doesn't matter who is included. Make it include only 100 people, I don't care... Finding primes is trivial. I'm sure I could go online and find a list of them, this is not the point of the exercise.

I have done this for you:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397

Easiest way has GOT to be to loop through every possible combination of two numbers that could possibly be a prime. Most efficient, too! ;)

Tolnaftate2004 06-14-2008 04:39 AM

Quote:

Originally Posted by cbk1994 (Post 1396964)
Easiest way has GOT to be to loop through every possible combination of two numbers that could possibly be a prime. Most efficient, too! ;)

I updated the exercise. So, primes are no longer involved.

cbk1994 06-14-2008 04:53 AM

PHP Code:

function onCreated()
{
  
temp.start = timevar2;
  
  
temp.incompatible = {
                        { 
1, 2 },
                        { 
3, 4 },
                        { 
5, 6 },
                        { 
6, 7 }
                      };
  
  
temp.chosen = null;
  
temp.sets = null;
  
  for ( 
temp.i = 0; i < 100; i ++ )
  {
    
temp.moveOn = false;
    
    while ( ! 
moveOn )
    {
      
temp.person1 = int( random( 0, 400 ) );
      
temp.person2 = int( random( 0, 400 ) );
      
      if ( 
chosen.index( @ person1 ) != -1 || chosen.index( @ person2 ) != -1 || person1 == person2 )
      {
        continue;
      }
      
      for ( 
temp.a : incompatible )
      {
        if ( ( 
a[0] == person1 && a[1] == person2 ) || ( a[0] == person2 || a[1] == person1 ) )
        {
          
// Incompatible
          
continue;
        }
      }
      
      
chosen.add( person1 );
      
chosen.add( person2 );
      
      
sets.add( { person1, person2 } );
      
      
moveOn = true;
    }
  }
  
  for ( 
temp.set : sets )
  {
    echo( 
"Set:" SPC set[0] SPC "and" SPC set[1] );
  }
  
  echo( 
"Time taken:" SPC timevar2 - start SPC "seconds." );
} 

I made it as inefficient as possible. It's rather fun to make inefficient code ^^.
I don't know why.

PHP Code:

Set: 59 and 119
Set
: 166 and 136
Set
: 279 and 156
Set
: 131 and 142
Set
: 195 and 397
Set
: 364 and 340
Set
: 314 and 382
Set
: 326 and 216
Set
: 352 and 140
Set
: 366 and 139
Set
: 12 and 32
Set
: 104 and 21
Set
: 92 and 75
Set
: 307 and 68
Set
: 271 and 367
Set
: 187 and 38
Set
: 103 and 66
Set
: 194 and 235
Set
: 209 and 390
Set
: 232 and 174
Set
: 331 and 147
Set
: 363 and 109
Set
: 398 and 329
Set
: 249 and 11
Set
: 362 and 354
Set
: 29 and 312
Set
: 170 and 337
Set
: 380 and 42
Set
: 304 and 167
Set
: 80 and 8
Set
: 234 and 275
Set
: 244 and 44
Set
: 265 and 76
Set
: 218 and 197
Set
: 224 and 375
Set
: 55 and 188
Set
: 84 and 54
Set
: 118 and 334
Set
: 288 and 98
Set
: 135 and 318
Set
: 10 and 305
Set
: 347 and 160
Set
: 158 and 28
Set
: 169 and 393
Set
: 303 and 13
Set
: 90 and 255
Set
: 230 and 22
Set
: 302 and 138
Set
: 240 and 37
Set
: 57 and 251
Set
: 343 and 313
Set
: 242 and 291
Set
: 73 and 0
Set
: 319 and 243
Set
: 394 and 223
Set
: 256 and 31
Set
: 392 and 346
Set
: 287 and 359
Set
: 261 and 117
Set
: 33 and 58
Set
: 186 and 283
Set
: 201 and 88
Set
: 126 and 78
Set
: 293 and 69
Set
: 360 and 65
Set
: 309 and 130
Set
: 162 and 95
Set
: 112 and 49
Set
: 339 and 200
Set
: 214 and 106
Set
: 184 and 231
Set
: 47 and 105
Set
: 15 and 237
Set
: 133 and 282
Set
: 205 and 72
Set
: 207 and 61
Set
: 146 and 125
Set
: 381 and 257
Set
: 64 and 149
Set
: 128 and 268
Set
: 324 and 322
Set
: 2 and 122
Set
: 226 and 46
Set
: 306 and 51
Set
: 178 and 35
Set
: 248 and 185
Set
: 369 and 155
Set
: 43 and 145
Set
: 183 and 311
Set
: 91 and 353
Set
: 14 and 115
Set
: 157 and 358
Set
: 213 and 171
Set
: 289 and 350
Set
: 290 and 286
Set
: 87 and 388
Set
: 217 and 159
Set
: 294 and 357
Set
: 148 and 24
Set
: 374 and 1
Time taken
: 0.008700847 seconds. 


Tolnaftate2004 06-14-2008 05:11 AM

I tried your code and it failed to perform the task. Leaving this to chance is probably a bad idea, seeing as it could lead to an infinite loop.

cbk1994 06-14-2008 05:15 AM

Quote:

Originally Posted by Tolnaftate2004 (Post 1396977)
I tried your code and it failed to perform the task. Leaving this to chance is probably a bad idea, seeing as it could lead to an infinite loop.

Worked fine for me ;o

The whole goal was inefficiency :)

Tolnaftate2004 06-14-2008 05:57 AM

Quote:

Originally Posted by cbk1994 (Post 1396978)
Worked fine for me ;o

The whole goal was inefficiency :)

Well I changed the incompatibilities so that any person would only match up with one other person.

cbk1994 06-14-2008 06:12 AM

Quote:

Originally Posted by Tolnaftate2004 (Post 1396982)
Well I changed the incompatibilities so that any person would only match up with one other person.

o

Probably hit a max loop limit.

Programmer 06-14-2008 07:26 AM

I suppose what you would have to do is this:

Starting with 400 students, loop through and select 100 students at random.
Then go through those 100 students and group then until you have 50 groups.
If you have 2 students who are together on the incompatibility list, flip them with another set at random, as described below:

1 & 2
v
^
3 & 4

3 & 2
1 & 4

If any of those 2 are on the incompatibility list, then repeat the process. Once everything is OK, then you've got your list.

Here's the script I made to accompany this concept:

PHP Code:

function onCreated()
{
  
this.start = timevar2;
  
  
this.incompatability = {
    {
102, 284},
    {
392, 126},
    {
1, 2},
    {
3, 4},
    {
5, 6},
    {
7, 8}
  };
  
  
  
this.studentsSelected.clear();
  
this.studentGroups.clear();
  
  
numStudentsSelected = 0;
  
// Select students at random
  
for (i = 0; i < 100; i++)
  {
    
f = int(random(0, 400));
    
    if (
this.studentsSelected.index(f) == -1)
    {
      
this.studentsSelected.add(f);
    } else
    {
      
i--; continue;
    }
  }
  
  
// Group them
  
for (i = 0; i < this.studentsSelected.size(); i += 2)
  {
    
this.studentGroups.add({this.studentsSelected[i], this.studentsSelected[i+1]});
  }
  
  
// Remove incompatabilities
  
for (i = 0; i < 50; i++)
  {
    
sGroup = this.studentGroups[i];
    
    for (
a = 0; a < this.incompatability.size(); a++)
    {
      if ((
sGroup[0] == this.incompatability[a][0] || sGroup[0] == this.incompatability[a][1]) && (sGroup[1] == this.incompatability[a][0] || sGroup[1] == this.incompatability[a][1]))
      {
        echo(
"INCOMPATABILITY: " @ sGroup);
        
        
// we've found an incompatability, so lets flip this student with the next group
        
sGroup2 = this.studentGroups[i+1];
        
        
sGroup[0] = sGroup2[0];
      }
    }
  }
  
  
this.end = timevar2;
  
  echo(
this.end - this.start);
  
  for (
i = 0; i < 50; i++)
  {
    echo(
"Group " @ (i+1) @ ": {" @ this.studentGroups[i][0] @ ", " @ this.studentGroups[i][1] @ "}");
  }
} 

Time taken: 0.002021789

Example Output:
PHP Code:

0.002021789
Group 1
: {137, 114}
Group 2: {282, 284}
Group 3: {47, 288}
Group 4: {267, 53}
Group 5: {19, 329}
Group 6: {345, 299}
Group 7: {141, 228}
Group 8: {93, 50}
Group 9: {347, 390}
Group 10: {166, 207}
Group 11: {139, 41}
Group 12: {130, 336}
Group 13: {140, 376}
Group 14: {262, 238}
Group 15: {196, 0}
Group 16: {352, 79}
Group 17: {368, 151}
Group 18: {54, 387}
Group 19: {81, 108}
Group 20: {332, 380}
Group 21: {249, 160}
Group 22: {73, 300}
Group 23: {64, 66}
Group 24: {316, 204}
Group 25: {248, 24}
Group 26: {8, 111}
Group 27: {215, 396}
Group 28: {252, 148}
Group 29: {270, 240}
Group 30: {229, 378}
Group 31: {172, 209}
Group 32: {227, 333}
Group 33: {283, 127}
Group 34: {42, 194}
Group 35: {359, 302}
Group 36: {6, 292}
Group 37: {30, 293}
Group 38: {105, 374}
Group 39: {371, 324}
Group 40: {242, 119}
Group 41: {83, 348}
Group 42: {256, 158}
Group 43: {189, 128}
Group 44: {232, 389}
Group 45: {322, 191}
Group 46: {225, 197}
Group 47: {33, 334}
Group 48: {239, 121}
Group 49: {39, 213}
Group 50: {230, 29} 

Probably not very efficient, but it works. I couldn't find any incompatibilities! XD

Tolnaftate2004 06-14-2008 07:36 AM

Quote:

Originally Posted by Programmer (Post 1396997)
Probably not very efficient, but it works. I couldn't find any incompatibilities! XD

This does not test if switching 2 people forms compatibility...

Programmer 06-14-2008 07:44 AM

Quote:

Originally Posted by Tolnaftate2004 (Post 1396998)
This does not test if switching 2 people forms compatibility...

Picky, Picky.

PHP Code:

function onCreated()
{
  
this.start = timevar2;
  
  
this.incompatability = {
    {
102, 284},
    {
392, 126},
    {
1, 2},
    {
3, 4},
    {
5, 6},
    {
7, 8}
  };
  
  
  
this.studentsSelected.clear();
  
this.studentGroups.clear();
  
  
numStudentsSelected = 0;
  
// Select students at random
  
for (i = 0; i < 100; i++)
  {
    
f = int(random(0, 400));
    
    if (
this.studentsSelected.index(f) == -1)
    {
      
this.studentsSelected.add(f);
    } else
    {
      
i--; continue;
    }
  }
  
  
// Group them
  
for (i = 0; i < this.studentsSelected.size(); i += 2)
  {
    
this.studentGroups.add({this.studentsSelected[i], this.studentsSelected[i+1]});
  }
  
  
// Remove incompatabilities
  
for (i = 0; i < 50; i++)
  {
    
sGroup = this.studentGroups[i];
    
    for (
a = 0; a < this.incompatability.size(); a++)
    {
      if ((
sGroup[0] == this.incompatability[a][0] || sGroup[0] == this.incompatability[a][1]) && (sGroup[1] == this.incompatability[a][0] || sGroup[1] == this.incompatability[a][1]))
      {
        echo(
"INCOMPATABILITY: " @ sGroup);
        
        
// we've found an incompatability, so lets flip this student with the next group
        
sGroup2 = this.studentGroups[i+1];
        
        
sGroup[0] = sGroup2[0];
        
        
this.studentGroups[i] = sGroup;
        
this.studentGroups[i+1] = sGroup2;
        
        
i--;
        continue;
      }
    }
  }
  
  
this.end = timevar2;
  
  echo(
this.end - this.start);
  
  for (
i = 0; i < 50; i++)
  {
    echo(
"Group " @ (i+1) @ ": {" @ this.studentGroups[i][0] @ ", " @ this.studentGroups[i][1] @ "}");
  }
} 

Time: 0.001927852

cbk1994 06-14-2008 08:46 AM

Quote:

Originally Posted by Programmer (Post 1397001)
Time: 0.001927852

Aww, mine was 0.008700847 seconds.

Of course, I was trying to make mine inefficient, for some reason ...

Programmer 06-14-2008 03:14 PM

Quote:

Originally Posted by cbk1994 (Post 1397004)
Aww, mine was 0.008700847 seconds.

Of course, I was trying to make mine inefficient, for some reason ...

Perhaps too many nested loops?

Tolnaftate2004 06-14-2008 07:50 PM

Quote:

Originally Posted by Programmer (Post 1397001)
Picky, Picky.

PHP Code:

function onCreated()
{
  
this.start = timevar2;
  
  
this.incompatability = {
    {
102, 284},
    {
392, 126},
    {
1, 2},
    {
3, 4},
    {
5, 6},
    {
7, 8}
  };
  
  
  
this.studentsSelected.clear();
  
this.studentGroups.clear();
  
  
numStudentsSelected = 0;
  
// Select students at random
  
for (i = 0; i < 100; i++)
  {
    
f = int(random(0, 400));
    
    if (
this.studentsSelected.index(f) == -1)
    {
      
this.studentsSelected.add(f);
    } else
    {
      
i--; continue;
    }
  }
  
  
// Group them
  
for (i = 0; i < this.studentsSelected.size(); i += 2)
  {
    
this.studentGroups.add({this.studentsSelected[i], this.studentsSelected[i+1]});
  }
  
  
// Remove incompatabilities
  
for (i = 0; i < 50; i++)
  {
    
sGroup = this.studentGroups[i];
    
    for (
a = 0; a < this.incompatability.size(); a++)
    {
      if ((
sGroup[0] == this.incompatability[a][0] || sGroup[0] == this.incompatability[a][1]) && (sGroup[1] == this.incompatability[a][0] || sGroup[1] == this.incompatability[a][1]))
      {
        echo(
"INCOMPATABILITY: " @ sGroup);
        
        
// we've found an incompatability, so lets flip this student with the next group
        
sGroup2 = this.studentGroups[i+1];
        
        
sGroup[0] = sGroup2[0];
        
        
this.studentGroups[i] = sGroup;
        
this.studentGroups[i+1] = sGroup2;
        
        
i--;
        continue;
      }
    }
  }
  
  
this.end = timevar2;
  
  echo(
this.end - this.start);
  
  for (
i = 0; i < 50; i++)
  {
    echo(
"Group " @ (i+1) @ ": {" @ this.studentGroups[i][0] @ ", " @ this.studentGroups[i][1] @ "}");
  }
} 

Time: 0.001927852

Once again, this failed to perform the task.
Again, let's discuss what we ought to be doing to solve this problem. This is a very difficult problem to solve (HRs idea, don't blame me).

Dan 06-14-2008 08:03 PM

Quote:

Originally Posted by Tolnaftate2004 (Post 1396887)
Today's challenge will include 2 problems:
  1. hard Suppose that you are organizing housing accommodations for a group of four hundred university students. Space is limited and only one hundred of the students will receive places in the dormitory. To complicate matters, the Dean has provided you with a list of pairs of incompatible students, and requested that no pair from this list appear in your final choice.
    The point of this exercise is to GENERATE DISCUSSION. We are not asking you to WRITE the code, merely speculate as to how you might do it. BUT IF YOU FEEL COMPELLED, write the code to accomodate arbitrary lists from the Dean.
  2. easy Solve this problem.

Is it possible for you to write in a little more simple English for me? :) I don't understand the task quite well, though I'd like to give it a go ;)

cbk1994 06-14-2008 08:20 PM

Quote:

Originally Posted by Dan (Post 1397049)
Is it possible for you to write in a little more simple English for me? :) I don't understand the task quite well, though I'd like to give it a go ;)

1. hard You are putting 400 people in houses. Only 100 people will be able to have a house. You have a list of people that can not be in the same house. 2 people must be in each house. Talk about it, don't script it unless you want to.

Is that a bit better?

Dan 06-15-2008 04:49 PM

Not sure if I entirely understood the task, but I suppose this should work?

PHP Code:

function onCreated() {
  
temp.students_incompatible = {{1,2},{3,49}};
  
temp.students_total = new[400];
  
temp.house_list = {};
  
  while (
temp.house_list.size() < 100) {
    
temp.i = 0;
    while (
temp.students_total[temp.i] == 1)
      
temp.i ++;
    
temp.students_total[temp.i] = 1;
    
temp.j = temp.i;
    
    
temp.go = false;
    while (
temp.students_total[temp.j] == 1 || !temp.go) {
      if ((@ {
temp.i + 1,temp.j + 1}) in temp.students_incompatible) {
        
temp.go = false;
        
temp.j ++;
      }
      else if ((@ {
temp.j + 1,temp.i + 1}) in temp.students_incompatible) {
        
temp.go = false;
        
temp.j ++;
      }
      else if (
temp.students_total[temp.j] == 1)
        
temp.j ++;
      else
        
temp.go = true;
    }
    
temp.students_total[temp.j] = 1;
    
temp.house_list.add({temp.i + 1,temp.j + 1});
  }
  echo(
temp.house_list);
} 

Time: 0.008579015

Chompy 06-15-2008 05:39 PM

Uhm, Dan, suppose to be discussion :o

Dan 06-15-2008 06:33 PM

Quote:

Originally Posted by Chompy (Post 1397216)
Uhm, Dan, suppose to be discussion :o

Well then this is how I'd make the groups, lol.

Chompy 06-15-2008 06:35 PM

Quote:

Originally Posted by Dan (Post 1397233)
Well then this is how I'd make the groups, lol.

Try explain it with words instead of code :p

Dan 06-15-2008 09:17 PM

Quote:

Originally Posted by Chompy (Post 1397237)
Try explain it with words instead of code :p

I want to know if it does what it should do tho (if I did what was needed).

Kristi 06-16-2008 07:36 PM

You are using very small lists :) Your algorithms are based on the fact that the list is small. If you took your basic ideas and applied them to more complex or impossible lists, they would run much longer then any of our lifetimes.

By impossible, suppose you were given a list of incompatible students that makes it impossible to come up with 100 students.

Your work is cut out for you ;)

cbk1994 06-16-2008 10:41 PM

Quote:

Originally Posted by Kristi (Post 1397490)
If you took your basic ideas and applied them to more complex or impossible lists, they would run much longer then any of our lifetimes.

No, that's why we have a max loop limit ^^

Kristi 06-16-2008 10:48 PM

Quote:

Originally Posted by cbk1994 (Post 1397552)
No, that's why we have a max loop limit ^^

You are supposed to assume a limit doesn't exist (in graal this can be achieved with a scheduleevent).

Dan 06-16-2008 11:03 PM

Quote:

Originally Posted by Kristi (Post 1397560)
You are supposed to assume a limit doesn't exist (in graal this can be achieved with a scheduleevent).

OK. Good luck to anyone trying, Dan's not going to invest any more time in this Programming Exercise because there's more work to do >_<.

Stryke 06-18-2008 10:46 PM

Would this work?

PHP Code:

function onCreated()
{
  
this.start = timevar2;
  
this.incompatability = {
                         {
23,46},
                         {
29,44},
                         {
45,96},
                         {
11,77},
                         {
2,32},
                         {
1,12},
                         {
13,69},
                         };
  
this.houselist = NULL;
  for(
temp.i=0; temp.i<100; temp.i++)
  {
    
this.intA = 0;
    
this.intB = 0;
    while(!
this.intA && !this.intB)
    {
      
this.intA = int(random(0,400));
      
this.intB = int(random(0,400));
      for(
temp.j=0; temp.j<100; temp.j++)
      {
        if(
this.intA == this.houselist[temp.j][0])
          
this.int = 0;
        else if(
this.intA == this.houselist[temp.j][1])
          
this.int = 0;
        else if(
this.intB == this.houselist[temp.j][0])
          
this.int = 0;
        else if(
this.intB == this.houselist[temp.j][1])
          
this.int = 0;
        if(
this.intA == this.incompatibility[temp.j])
          if(
this.intB == this.incompatibility[temp.j])
            
this.int = 0;
        else if(
this.intB == this.incompatibility[temp.j])
          if(
this.intA == this.incompatibility[temp.j])
            
this.int = 0;
      }
      if(
this.intA == this.intB)
        
this.int = 0;
    }
    
this.houselist.add({this.intA,this.intB});
  }
  
this.end = timevar2;
}
savelog2("houseList.txt",this.houselist);
echo(
"Time: " @ this.end - this.start); 

Time: 0.026829004

Damn that's slow compared to others lol

cbk1994 06-18-2008 11:00 PM

Quote:

Originally Posted by Stryke (Post 1397932)
Would this work?

PHP Code:

function onCreated()
{
  
this.start = timevar2;
  
this.incompatability = {
                         {
23,46},
                         {
29,44},
                         {
45,96},
                         {
11,77},
                         {
2,32},
                         {
1,12},
                         {
13,69},
                         };
  
this.houselist = NULL;
  for(
temp.i=0; temp.i<100; temp.i++)
  {
    
this.intA = 0;
    
this.intB = 0;
    while(!
this.intA && !this.intB)
    {
      
this.intA = int(random(0,400));
      
this.intB = int(random(0,400));
      for(
temp.j=0; temp.j<100; temp.j++)
      {
        if(
this.intA == this.houselist[temp.j][0])
          
this.int = 0;
        else if(
this.intA == this.houselist[temp.j][1])
          
this.int = 0;
        else if(
this.intB == this.houselist[temp.j][0])
          
this.int = 0;
        else if(
this.intB == this.houselist[temp.j][1])
          
this.int = 0;
        if(
this.intA == this.incompatibility[temp.j])
          if(
this.intB == this.incompatibility[temp.j])
            
this.int = 0;
        else if(
this.intB == this.incompatibility[temp.j])
          if(
this.intA == this.incompatibility[temp.j])
            
this.int = 0;
      }
      if(
this.intA == this.intB)
        
this.int = 0;
    }
    
this.houselist.add({this.intA,this.intB});
  }
  
this.end = timevar2;
}
savelog2("houseList.txt",this.houselist);
echo(
"Time: " @ this.end - this.start); 

Time: 0.026829004

Damn that's slow compared to others lol

I didn't try it, but, like said, anything using random will not work well with larger numbers.

Dan 06-18-2008 11:00 PM

I think it's slow because of the logging you're doing. But yeah, quite some loops also.

Stryke 06-21-2008 06:36 AM

Oh it's only 50? :P

Time remaining: 0.007151126

Still crappy. :P I took out the log too.

Tolnaftate2004 06-21-2008 06:52 PM

We can assume that the algorithm would eventually need to be used to find an appropriate room assignment for a group of students for which there is only one solution (i.e. worst case scenario). Because of this, the algorithm would need to "know" that student A needs to pair with student B rather than student C. Hence why random is a horrible idea.

WhiteDragon 07-19-2008 09:26 PM

PHP Code:

//#CLIENTSIDE
function onWeaponFired() {
  
// Bad match array
  
temp.bad = {
             {
91,50},
             {
60,230},
             {
132,399},
             {
52,293},
             {
22,25},
             {
83,293},
             {
83,82}
             };
  
// Generate students array
  
temp.students = new[400];
  for (
temp.i = 0; temp.i < 400; temp.i++)
    
temp.students[temp.i] = temp.i;



  
// START of exercise
  
temp.start = timevar2;

  
// Shuffle students array
  
temp.n = temp.students.size();
  for (
temp.i = 0; temp.i < temp.n - 1; temp.i++) {
    
temp.j = temp.i + random(0,32767) / (32767 / (temp.n - temp.i) + 1);
    
temp.t = temp.students[temp.j];
    
temp.students[temp.j] = temp.students[temp.i];
    
temp.students[temp.i] = temp.t;
  }
  
  
// Heapsort the bad match array
  
temp.bad.sortascending();
  
  
temp.pairs = new[50];
  
temp.i = 0;
  while (
temp.i < 50) {
    
temp.pair = {temp.students[temp.x], temp.students[temp.x+1]};

    
// Binary search for the current pair in the bad pairs
    
if (binarysearcharray(temp.bad, temp.pair) == -1) {
      
temp.pairs[temp.i] = temp.pair;
      
temp.x++;
    }
    
temp.x++;
    
temp.i++;
  }
  
  
  
// END of exercise
  
temp.time = timevar2 - temp.start;
    
  
player.chat = temp.pairs;
  echo (
temp.time);
}

// Modified binary search
function binarysearcharray(temp.a, temp.v) {
  
temp.low = 0;
  
temp.high = temp.a.size()-1;
  while (
temp.low <= temp.high) {
    
temp.mid = (temp.low + temp.high) /2;
    if (
temp.a[temp.mid][0] > temp.v[0] || temp.a[temp.mid][0] == temp.v[0] && temp.a[temp.mid][1] > temp.v[1]) 
      
temp.high = temp.mid - 1;
    else if (
temp.a[temp.mid][0] < temp.v[0] || temp.a[temp.mid][0] == temp.v[0] && temp.a[temp.mid][1] < temp.v[1])
      
temp.low = temp.mid +1;
    else
      return 
temp.mid;
  }
  return -
1;
} 

Average running time of 0.00326736083, but it's a rather small "bad pair" set.

Should run in O(n log n log m) where n is the number of pairs and m is the number of bad pairs.

WhiteDragon 07-21-2008 07:47 AM

I realized I made an error, but it wouldn't let me edit it... so here is another giant block of code:

PHP Code:

//#CLIENTSIDE
function onWeaponFired() {
  
// Bad match array
  
temp.bad = {
             {
91,50},
             {
60,230},
             {
132,399},
             {
52,293},
             {
22,25},
             {
83,293},
             {
83,82}
             };
  
// Generate students array
  
temp.students = new[400];
  for (
temp.i = 0; temp.i < 400; temp.i++)
    
temp.students[temp.i] = temp.i;



  
// START of exercise
  
temp.start = timevar2;

  
// Shuffle students array
  
temp.n = temp.students.size();
  for (
temp.i = 0; temp.i < temp.n - 1; temp.i++) {
    
temp.j = temp.i + random(0,32767) / (32767 / (temp.n - temp.i) + 1);
    
temp.t = temp.students[temp.j];
    
temp.students[temp.j] = temp.students[temp.i];
    
temp.students[temp.i] = temp.t;
  }
  
  
// Heapsort the bad match array
  
temp.bad.sortascending();
  
  
temp.pairs = new[50];
  
temp.i = 0;
  while (
temp.i < 50) {
    
temp.pair = {temp.students[temp.x], temp.students[temp.x+1]};

    
// Binary search for the current pair in the bad pairs
    
if (binarysearcharray(temp.bad, temp.pair) == -1) {
      
// Add it
      
temp.pairs[temp.i] = temp.pair;
      
temp.students.remove(temp.students[temp.x]);
      
temp.students.remove(temp.students[temp.y]);
      
temp.i++;
      
temp.x = 0;
      
temp.y = 0;
    } else {
      
// Look more
      
if (temp.y == temp.students.size()) {
        
temp.x++;
        
temp.y = 0;
      } else {
        
temp.y++;
      }
    }
  }
  
  
  
// END of exercise
  
temp.time = timevar2 - temp.start;
    
  
player.chat = temp.pairs;
  echo (
temp.time);
}

// Modified binary search
function binarysearcharray(temp.a, temp.v) {
  
temp.low = 0;
  
temp.high = temp.a.size()-1;
  while (
temp.low <= temp.high) {
    
temp.mid = (temp.low + temp.high) /2;
    if (
temp.a[temp.mid][0] > temp.v[0] || temp.a[temp.mid][0] == temp.v[0] && temp.a[temp.mid][1] > temp.v[1]) 
      
temp.high = temp.mid - 1;
    else if (
temp.a[temp.mid][0] < temp.v[0] || temp.a[temp.mid][0] == temp.v[0] && temp.a[temp.mid][1] < temp.v[1])
      
temp.low = temp.mid +1;
    else
      return 
temp.mid;
  }
  return -
1;
} 

Worst case is actually O(n**2 log n log m), but it uses Randomization and Restriction, so it's considerably fast than the worst case.

_Z3phyr_ 08-02-2008 11:24 AM

Quote:

Originally Posted by Tolnaftate2004 (Post 1396887)
Today's challenge will include 2 problems:
  1. hard Suppose that you are organizing housing accommodations for a group of four hundred university students. Space is limited and only one hundred of the students will receive places in the dormitory. To complicate matters, the Dean has provided you with a list of pairs of incompatible students, and requested that no pair from this list appear in your final choice.
    The point of this exercise is to GENERATE DISCUSSION. We are not asking you to WRITE the code, merely speculate as to how you might do it. BUT IF YOU FEEL COMPELLED, write the code to accomodate arbitrary lists from the Dean.

Note From Hell Raven: the 100 students are rooming in pairs of 2. So that is 50 pairs (I do not believe this was clear...)


answer:

http://img376.imageshack.us/img376/5436/86884629uj9.png
http://img376.imageshack.us/img376/5...101aefdfeb.jpg


proof:

N and B are disjoint sets. (see above picture)

---

this is an abstract problem. idk what you guys have been doing with all of that prime number crap.

WhiteDragon 08-02-2008 10:35 PM

This problem is NP-complete, so it's impossible to do it in polynomial time.
The runtime is actually O(n**m) if you check all conditions. The best way to attack the problem would most likely be dynamic programming.


All times are GMT +2. The time now is 10:31 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.