Copyright (C) 2005 James Grant This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ ?> "; echo "
"; echo i18n("When complete, a green bar will appear at the BOTTOM of this page saying that everything has completed successfully. When complete, you can use the following links to manage the Judging Teams and the Judges assigned to them (clicking on these links now will stop the scheduler)."); echo "
"; echo "
"; echo "".i18n("Manage Judge Teams").""; echo "
"; echo "".i18n("Manage Judge Members").""; echo "
"; echo "
"; echo i18n("If you get an error like: \"Fatal error: Maximum execution time of 30 seconds exceeded...\" you will need to talk to your system admin and have them adjust the \"max_execution_time\" variable in the \"php.ini\" file from 30(seconds) to something larger, like 900 (15 minutes). And then have them restart the webserver for the change to take effect"); echo "
"; echo "
"; */ //function TRACE() { } //function TRACE_R() { } function TRACE($str) { print($str); } function TRACE_R($array) { print_r($array); } TRACE("
");


function set_status($txt)
{
	TRACE("Status: $txt\n");
	mysql_query("UPDATE config SET val='$txt' WHERE 
			var='judge_scheduler_activity' AND year=0");
}

$set_percent_last_percent = -1;
function set_percent($n)
{
	global $set_percent_last_percent;
	$p = floor($n);
	if($p == $set_percent_last_percent) return;
	TRACE("Progress: $p\%\n");
	mysql_query("UPDATE config SET val='$p' WHERE 
			var='judge_scheduler_percent' AND year=0");
	$set_percent_last_percent = $p;
}

set_status("Initializing...");
set_percent(0);

/* The cost function is:
	+ 50 * each judge below the min for each team
	+ 10 * each judge above the max for each team
	+  2 * each level of preference away from the 
		max level for each judge
	+ 40 if the team doesn't have a chair.
	+ 25 for each memember on the team that can't speak the language
	     of the judging team

	( ex: if a judge has selected LS->2, PS->0, CS->-1 
	 	then matching that judge with a:
		LS = -4,
		PS = 0,
		CS = -2,
		else = 0
	)
*/

/* Compute the cost of adding a judge to a team */

function judges_cost_function($annealer, $bucket_id, $ids)
{
	global $config;
	global $jteam;
	global $judges;
	/* Bucket ID is the team number */
	/* ids are the judge ids currently in the bucket */

//	TRACE("Bucket id=$bucket_id, ids=");
//	TRACE_R($ids);

	$cost = 0;
	$have_chair = 0;

	if($bucket_id == 0) {
		/* This is the placeholder */
		$cost = count($ids) * 5;
//		TRACE("Extra judge team cost=$cost\n");
		return $cost;
	}
	
	
	$t =& $jteam[$bucket_id];
	
	/* Compute the over max / under min costs */
	$c = count($ids);
	$min = ($c < $t['min_judges']) ? $t['min_judges'] - $c : 0;
	$max = ($c > $t['max_judges']) ? $c - $t['max_judges'] : 0;
	$cost += $min * 50;
	$cost += $max * 10;

//	TRACE("Under min=$min, over max=$max\n");

	/* For each judge on the team, score their preferences */
	for($x=0; $x1 */
	if(!$have_chair && $config['min_judges_per_team']>1) 
		$cost += 40;
 
//	TRACE("Team $bucket_id, cost is $cost\n");

	return $cost;
}


$current_jdiv = array();


function jdiv_compute_cost($annealer, $bucket_id, $ids)
{
	/* IDS is a list of project ids for a judging team */
	global $current_jdiv;
	
	$cost = 0;
	$t_div = array();
	$t_cat = array();
	$t_lang = array();

	/* Foreach project this jteam is judging, record the 
	 * div/cat/lang */
	for($x=0; $xitems_per_bucket)) * 100;
	/* Score 100 pts for multiple languages */
	$cost += (count($t_lang) - 1) * 75;
	/* Score 25pts for multiple divs/cats */
	$cost += (count($t_div) - 1) * 25;
	$cost += (count($t_cat) - 1) * 25;

	/* Score +200 pts for each duplicate project this team is judging, we
	 * really don't want a jteam judging the same project twice */
	for($x=0; $xid]=$r->division_shortform;
	$div[$r->id]=$r->division;
	TRACE("   {$r->id} - {$div[$r->id]}\n");
}

TRACE("Loading Project Age Categories...\n");
$cat = array();
$q=mysql_query("SELECT * FROM projectcategories WHERE year='".$config['FAIRYEAR']."' ORDER BY id");
while($r=mysql_fetch_object($q)) {
	$cat[$r->id]=$r->category;
	TRACE("   {$r->id} - {$r->category}\n");
}

TRACE("Loading Languages...\n");
$langr = array();
$q=mysql_query("SELECT * FROM languages WHERE active='Y'");
while($r=mysql_fetch_object($q)) {
	$langr[$r->lang] = $r->langname;
	TRACE("   {$r->lang} - {$r->langname}\n");
}

$jdiv = array();
TRACE("Loading Judging Division Configuration and Projects...\n");
$q=mysql_query("SELECT * FROM judges_jdiv");
while($r=mysql_fetch_object($q)) {
	/* Ignore jdiv 0 (all unassigned div/cats) */
	if($r->jdiv_id == 0) continue;

	$jdiv[$r->jdiv_id]['config'][] = array('div' => $r->projectdivisions_id,
					'cat' => $r->projectcategories_id,
					'lang' => $r->lang);
}

$keys = array_keys($jdiv);
for($k=0; $k 0) TRACE("\t- ");
		TRACE($cat[$d['cat']]." ".$div[$d['div']]." - ".$langr[$d['lang']]);
		$qp = mysql_query("SELECT projects.* FROM projects, registrations WHERE ".
					" projects.year='".$config['FAIRYEAR']."' AND ".
					" projectdivisions_id='{$d['div']}' AND ".
					" projectcategories_id='{$d['cat']}' AND ".
					" language='{$d['lang']}' AND " .
					" registrations.id = projects.registrations_id " .
					getJudgingEligibilityCode()
				);
		$count = 0;
		while($rp = mysql_fetch_object($qp)) {
			$jdiv[$jdiv_id]['projects'][$rp->id] = array( 
					'div' => $d['div'],
					'cat' => $d['cat'],
					'lang' => $d['lang']);
			$count++;
		}
		TRACE(" ($count projects)\n");
	}
	if(count($jdiv[$jdiv_id]['projects']) == 0) {
		TRACE("\t- This div has no projects, removing.\n");
		unset($jdiv[$jdiv_id]);
	}
}

set_status("Computing required judging teams");
TRACE("   Each judging team may judge {$config['max_projects_per_team']} projects\n");
TRACE("   Each project must be judged {$config['times_judged']} times\n");

$keys = array_keys($jdiv);
for($k=0; $kanneal();

	$jdiv[$jdiv_id]['jteams'] = array();
	for($x=0;$x<$a->num_buckets; $x++) {
		$bkt = $a->bucket[$x];
		TRACE("   SubTeam $x:\n");
		$jdiv[$jdiv_id]['jteams'][] = $jteam_id;
		
		$jteam[$jteam_id]['id'] = $jteam_id;
		$jteam[$jteam_id]['projects'] = $a->bucket[$x];
		$jteam[$jteam_id]['sub'] = $x;
		$jteam[$jteam_id]['jdiv_id'] = $jdiv_id;
		$jteam[$jteam_id]['divs'] = array();
		$jteam[$jteam_id]['cats'] = array();
		$jteam[$jteam_id]['langs'] = array();
		$jteam[$jteam_id]['min_judges'] = $config['min_judges_per_team'];
		$jteam[$jteam_id]['max_judges'] = $config['max_judges_per_team'];
		
		for($y=0;$yid;
	print(" $jteam_id");
	/* Clean out the judges_teams_link */
	mysql_query("DELETE FROM judges_teams_link WHERE judges_teams_id='$jteam_id' AND year={$config['FAIRYEAR']}");
	print mysql_error();
	/* Awards */
	mysql_query("DELETE FROM judges_teams_awards_link WHERE judges_teams_id='$jteam_id' AND year={$config['FAIRYEAR']}");
	print mysql_error();
	/* Timeslots */
	mysql_query("DELETE FROM judges_teams_timeslots_link WHERE judges_teams_id='$jteam_id' AND year={$config['FAIRYEAR']}");
	print mysql_error();
	/* Timeslots projects */
	mysql_query("DELETE FROM judges_teams_timeslots_projects_link WHERE judges_teams_id='$jteam_id' AND year={$config['FAIRYEAR']}");
	print mysql_error();
}
/* Finally, delete all the autocreated judges teams */
mysql_query("DELETE FROM judges_teams WHERE autocreate_type_id=1 AND year={$config['FAIRYEAR']}");
print mysql_error();

/* Also delete any judges_teams_link that link to teams that dont exist */
$q=mysql_query("SELECT judges_teams_link.id, judges_teams.id AS judges_teams_id FROM judges_teams_link LEFT JOIN judges_teams ON judges_teams_link.judges_teams_id=judges_teams.id WHERE judges_teams_link.year={$config['FAIRYEAR']}");
$n=0;
while($r=mysql_fetch_object($q)) {
	if(!$r->judges_teams_id) {
		mysql_query("DELETE FROM judges_teams_link WHERE id='$r->id'");
		$n++;
	}
}
print("Deleted $n orphaned team linkings\n");
TRACE(" Done.\n");

set_status("Loading Judges");

$q=mysql_query("SELECT judges.* FROM judges,judges_years WHERE ".
		"complete='yes' AND deleted='no' ".
		" AND judges_years.year='{$config['FAIRYEAR']}' ".
		" AND judges_years.judges_id=judges.id" 
		);

$judges=array();
$sa_judges = array();

while($r=mysql_fetch_object($q))
{
	unset($divprefs);
	unset($catprefs);
	unset($langprefs);

	/* Try to fetch a team link ID for this judge, if we can, we don't want to use this judge
	 * in the divisional awards, they are already assigned to soemthing */
	$q2 = mysql_query("SELECT judges_id FROM judges_teams_link WHERE ".
				"judges_id='$r->id' ".
				" AND year='".$config['FAIRYEAR']."'");
	if(mysql_num_rows($q2) != 0) {
		TRACE("   {$r->firstname} {$r->lastname} is already on a judging team, skipping.\n");
		continue;
	}

	//get category preferences
	$q2=mysql_query("SELECT * FROM judges_catpref WHERE judges_id='$r->id' AND year='".$config['FAIRYEAR']."' ORDER BY projectcategories_id");
	$catprefs=array();
	while($r2=mysql_fetch_object($q2))
		$catprefs[$r2->projectcategories_id]=$r2->rank;

	//get division preferences
	$q2=mysql_query("SELECT * FROM judges_expertise WHERE judges_id='$r->id' AND year='".$config['FAIRYEAR']."' AND projectsubdivisions_id IS NULL ORDER BY projectdivisions_id");
	//the areas of expertise are ranked from 1 to 5, and we need them as -2,-1,0,1,2 so we simply subtract 3
	$divprefs=array();
	while($r2=mysql_fetch_object($q2))
		$divprefs[$r2->projectdivisions_id]=$r2->val-3;

	$langprefs = array();
	$q3=mysql_query("SELECT * from judges_languages WHERE judges_id='$r->id'");
	while($r3=mysql_fetch_object($q3))
		$langprefs[]=$r3->languages_lang;

	$q2 = mysql_query("SELECT answer FROM question_answers WHERE ".
				" registrations_id='{$r->id}' AND ".
				" questions_id='$willing_chair_question_id' AND ".
				" year='{$config['FAIRYEAR']}' ");
 	mysql_error();
	$willing_chair = 'no';
	if(mysql_num_rows($q2) == 1) {
		$r2 = mysql_fetch_object($q2);
		if($r2->answer == 'yes') $willing_chair = 'yes';
	}

	$sa_only = 'no';
	if($r->typepref == 'speconly') $sa_only = 'yes';
	$sa_sel = array();


	if($sa_only == 'yes') {
		TRACE("Judge [{$r->firstname} {$r->lastname}] is a special awards only.\n");
		/* Find their special award id */
		$qq = mysql_query("SELECT award_awards.id,award_awards.name FROM 
				judges_specialaward_sel,award_awards 
				WHERE
				award_awards.id=judges_specialaward_sel.award_awards_id
				AND judges_specialaward_sel.judges_id='{$r->id}'
				AND judges_specialaward_sel.year='{$config['FAIRYEAR']}'
				AND award_awards.year='{$config['FAIRYEAR']}'");
		echo mysql_error();
		if(mysql_num_rows($qq) == 0) {
			TRACE("   - NO special award selected! (removing special award only request)\n");
			$sa_only = 'no';
		} else if(mysql_num_rows($qq) > 1) {
			TRACE("   - More than ONE special award selected (removing special award only request):\n");
			$sa_only = 'no';
		}
		while($rr = mysql_fetch_object($qq)) {
			TRACE("      ".$rr->name."\n");
			$sa_sel[] = $rr->id;
		}
	}

	$j=array(
	"judges_id"=>"$r->id",
	"name"=>"$r->firstname $r->lastname",
	"years_school"=>$r->years_school,
	"years_regional"=>$r->years_regional,
	"years_national"=>$r->years_national,
	"willing_chair"=>$willing_chair,
	"divprefs"=>$divprefs,
	"catprefs"=>$catprefs,
	"languages"=>$langprefs,
	"sa_only"=>$sa_only,
	"sa_sel"=>$sa_sel,
	);

	/* If it's a special award only judge, keep them
	 * out of the judges list for the divisional annealer */
	if($sa_only == 'yes') {
		$sa_judges[$r->id] = $j;
	} else {
		$judges[$r->id] = $j;
	}


}
TRACE("Loaded ".count($judges)." judges.\n");
$jteam[0]['max_judges'] = count($judges);


function judges_to_teams_update($progress, $total)
{
	set_percent(($progress * 50) / $total);
}
set_status("Assigning Judges to Teams");

$judge_ids = array_keys($judges);
$e = $config['effort'];
$a = new annealer(count($jteam), 25, $e, 0.98, judges_cost_function, $judge_ids);
$a->set_update_callback(judges_to_teams_update);
$a->anneal();



function pr_judge(&$jt, $jid)
{
	global $judges;
	$j =& $judges[$jid];
	print("   - {$j['name']} (");
	for($x=0; $xmax;

TRACE("Max Judging Team Number is currently $max_jteam_num\n");

for($x=1;$xbucket_cost[$x]} ");
	$lang_array = $t['langs'];
	asort($lang_array);
	$langstr = implode(' ', $lang_array);

	print("langs=($langstr)");
	print("cats=(");
	$catstr="";
	for($y=0; $ybucket[$x];
	for($y=0; $yid','$team_id','{$config['FAIRYEAR']}')");
		}
	}

}

print("Unused Judges:\n");
$ids = $a->bucket[0];
for($y=0; $y $t['max_judges']) ? $c - $t['max_judges'] : 0;
	$cost += $min * 50;
	$cost += $max * 10;

	/* For each judge on the team, score their preferences */
	for($x=0; $xpick_move();

		/* See if $b1,$i1 is movable */
		$id1 = $a->bucket[$b1][$i1];
		$j1 =& $sa_judges[$id1];
//		print("J1:");
//		print_r($j1);
		if($j1['sa_only'] == 'yes') continue;

		if($i2 != -1) {
			$id2 = $a->bucket[$b2][$i2];
			$j2 =& $sa_judges[$id2];
//		print("J2:");
//		print_r($j2);
			if($j2['sa_only'] == 'yes') continue;
		}

		return array($b1, $i1, $b2, $i2);
	}
}


if($config['scheduler_enable_sa_scheduling'] == 'yes') {
	set_status("Creating Special Award Judging Teams (one team per award)");
	$q = "SELECT award_awards.name,award_awards.id FROM award_awards,award_types
		WHERE 
			award_awards.year='{$config['FAIRYEAR']}'
			AND award_types.id=award_awards.award_types_id
			AND award_awards.schedule_judges='yes'
			AND award_types.year='{$config['FAIRYEAR']}'
			AND award_types.type='Special' 
		";
	$r = mysql_query($q);
	print(mysql_error());
	/* sa_jteam for leftover judges, if any */
	$sa_jteam = array();
	$sa_jteam[0]['id'] = 0;
	$sa_jteam[0]['projects'] = array();
	$sa_jteam[0]['langs'] = array();
	$sa_jteam[0]['min_judges'] = 0;
	$sa_jteam[0]['max_judges'] = 0;
	$sa_jteam[0]['award_ids'] = array();

	$x=1;
	while($i = mysql_fetch_object($r)) {
		$projects = getProjectsNominatedForSpecialAward($i->id);

		$max_jteam_num++;  /* Pre-increment before using */

		$pids = array_keys($projects);
		$sa_jteam[$x]['id'] = $jteam_id;
		$sa_jteam[$x]['projects'] = $pids;
		$sa_jteam[$x]['sub'] = 0;
		$sa_jteam[$x]['langs'] = array();
		$min = floor(count($pids) / $config['projects_per_special_award_judge']) + 1;
		$sa_jteam[$x]['min_judges'] = $min;
		$sa_jteam[$x]['max_judges'] = $min;
		$sa_jteam[$x]['award_ids'] = array($i->id);

		$tn = "{$i->name}";
		/* Write this team to the DB */
		mysql_query("INSERT INTO judges_teams (num,name,autocreate_type_id,year)
				VALUES ('$max_jteam_num','".mysql_escape_string($tn)."','1','{$config['FAIRYEAR']}')");
		$sa_jteam[$x]['id'] = mysql_insert_id();

			/* Link the award to this team */
		mysql_query("INSERT INTO judges_teams_awards_link (award_awards_id,judges_teams_id,year) 
				VALUES ('{$i->id}','{$sa_jteam[$x]['id']}','{$config['FAIRYEAR']}')");

		TRACE("Created Team: $tn {$sa_jteam[$x]['id']}\n");
		$jteam_id++;
		$x++;
	}



	/* ====================================================================*/
	set_status("Assigning Judges to Special Award Teams\n");


	$judge_ids = array_keys($sa_judges);
	$e = $config['effort'];
	$a = new annealer(count($sa_jteam), 25, $e, 0.98, judges_sa_cost_function, $judge_ids);
	//$a->set_update_callback(judges_to_teams_update);
	//$a->set_pick_move(judges_sa_pick_move);
	$a->anneal();


	$x=0;
	foreach($sa_jteam as $tid => $t) {
		if($tid == 0) {
			$x++;
			continue;
		}
		print("Judging Team {$t['id']}: cost={$a->bucket_cost[$x]} #=({$t['min_judges']},{$t['max_judges']}) ");
	//	print("langs=(");
	/*	$langstr="";
		for($y=0; $ybucket[$x];
		for($y=0; $y$r->id,
					"date"=>$r->date,
					"starttime"=>substr($r->starttime,0,-3),
					"endtime"=>substr($r->endtime,0,-3));
	print("   ".$available_timeslots[$x]['starttime']." -> ".
			$available_timeslots[$x]['endtime']."\n");
	$x++;
}

$n_timeslots = count($available_timeslots);


	/* First, check to see if the project is being judged 3 or
	 * more times in a row, OR, if it has large gaps that aren't
	 at the end of the judging */
/* I'm going to leave this here, for now, we shoudl do something like
 * this at some point in evaluating projects, but right now 
 * the randomness is pretty good. */
/*	for($x=0; $x 2) $cost += $z_count;
				$r_count++;
				$z_count=0;
				if($r_count > 2) $cost += $r_count;
			}
		}
	}
	*/

function timeslot_pick_move($a)
{
	/* Use the existing pick move, but we want the item numbers
	 * in each bucket to always be the same */
	list($b1, $i1, $b2, $i2) = $a->pick_move();
	$i2 = $i1;
	return array($b1, $i1, $b2, $i2);
}


function timeslot_cost_function($annealer, $bucket_id, $ids)
{	
	$cost = 0;

	/* Check to make sure a judge isn't judging two projects
	 * at the same time */
	$n_pids = count($ids);
	for($x=0; $x<$n_pids-1; $x++) {
		$jteam_id1 = $ids[$x];
		if($jteam_id1 == 0) continue;
		for($y=$x+1; $y<$n_pids; $y++) {
			$jteam_id2 = $ids[$y];
			if($jteam_id1 == $jteam_id2)
				$cost += 50;
		}
	}
	return $cost;
}




$keys = array_keys($jdiv);
$keys_count = count($keys);
for($k=0; $k<$keys_count; $k++) {
	$jdiv_id = $keys[$k];

	$pids = array_keys($jdiv[$jdiv_id]['projects']);
	$n_projects = count($pids);

	if($n_projects == 0) continue;

	unset($project_rlookup);
	$project_rlookup = array();

	for($x=0; $xset_pick_move(timeslot_pick_move);
	$a->anneal();

	for($x=0; $xbucket[$y][$x];
			TRACE(($y+1).":$jteam_id  ");

			if($jteam_id == 0) continue;

			/* if jteam_id isn't 0, instert it into the db */
			mysql_query("INSERT INTO judges_teams_timeslots_link ".
				" (judges_teams_id,judges_timeslots_id,year)".
				" VALUES ('{$jteam[$jteam_id]['team_id']}', ".
				" '{$available_timeslots[$y]['id']}', ".
				" '{$config['FAIRYEAR']}')");
														 
			mysql_query("INSERT INTO judges_teams_timeslots_projects_link ".
				" (judges_teams_id,judges_timeslots_id,projects_id,year) ".
				" VALUES ('{$jteam[$jteam_id]['team_id']}', ".
				" '{$available_timeslots[$y]['id']}', ".
				" '$pid', '{$config['FAIRYEAR']}')");

		}
		TRACE("\n");
	}

}

TRACE("All Done.\n");
echo "
"; set_percent(-1); set_status("Done"); //echo happy("Scheduler completed successfully"); //send_footer(); ?>