require('phplib/phplib.php'); require('common.php'); ini_set('memory_limit', '128M'); ini_set('max_execution_time', 300); check_personeel(); /* $toetsen = get_toetsen_from_csv( 'export1415-1.csv', array('5H.schk/TP1', '6V.wisD/TP1'), array( '6V.wisAC/TP1' => array('6V.wisA/TP1', '6V.wisC/TP1'), '5H.kualg/TP1' => array('5H.kumu/TP1', '5H.kubv/TP1')) ); //array('5H.schk/TP1', '6V.wisD/TP1'), */ $db = mdb2_open($dsn); $herinfo = get_better_herinfo($db, $_GET); //if (isset($_GET['period_id'])) { // $herinfo = get_herinfo($db, $_GET['period_id']); //} else { // $herinfo = get_herinfo($db); //} $period_id = $herinfo['period_id']; //$buiten_beschouwing = array( // '5H.netl/TP2 ST lit', // '6V.netl/TP2 ST literatuur', //); $buiten_beschouwing = get_buiten_beschouwing($db, $period_id); $samenvoegen = get_samenvoegen($db, $herinfo['period_id']); //$samenvoegen = array( // '5V.wisAC/TP2 ST1' => array('5V.wisA/TP2 ST1', '5V.wisC/TP2 ST1'), // '6V.wisAC/TP2 ST3' => array('6V.wisA/TP2 ST3', '6V.wisC/TP2 ST3'), //); if (isset($_GET['limit'])) $toetsen = get_toetsen_from_db($db, $period_id, $buiten_beschouwing, $samenvoegen, $_GET['limit']); else $toetsen = get_toetsen_from_db($db, $period_id, $buiten_beschouwing, $samenvoegen); function set_graph_id(&$toets, $id) { global $toetsen, $collections; assert(!$toets['graph']); $toets['graph'] = $id; foreach ($toets['neighbors'] as $naam => $aantal) { if ($toetsen[$naam]['graph'] === NULL) { $collections[$id]['list'][] = $naam; set_graph_id($toetsen[$naam], $id); } } } $graph_id = 0; $collections = array(); foreach ($toetsen as $naam => &$value) { if ($value['graph'] !== NULL) continue; $collections[$graph_id] = array('list' => array($naam)); set_graph_id($value, $graph_id++); } unset($value); //echo("aantal onafhankelijke sets: $graph_id\n"); function get_bad_colors(&$toetsen, &$c, $no, $max_colors, $add_undefined = false) { $bad_colors = array(); for ($i = 0; $i < $max_colors; $i++) $bad_colors[] = 0; if ($add_undefined) $bad_colors[NULL] = 0; // alle vertices met een lagere index zijn al gekleurd // en alle vertices met een hogere index zijn niet gekleurd // // visit all colored neighbors foreach ($toetsen[$c['list'][$no]]['neighbors'] as $neighbor => $count) { if ($c['indices'][$neighbor] > $no && !$add_undefined) break; $bad_colors[$toetsen[$neighbor]['color']]++; } return $bad_colors; } function beschouw(&$c, $no, $used_colors, $max_colors) { static $ret; global $toetsen; if ($no == 0) { $ret = array('colors' => array(), 'solutions' => array()); for ($i = 0; $i < count($c['list']); $i++) { $ret['colors'][$i] = array(); for ($j = 0; $j < $max_colors; $j++) $ret['colors'][$i][$j] = 0; } } if ((count($toetsen[$c['list'][$no]]['neighbors']) < $max_colors) || $no == count($c['list'])) { for ($i = $no; $i < count($c['list']); $i++) { $bad_colors = get_bad_colors($toetsen, $c, $i, $max_colors, true); if ($bad_colors[NULL]) continue; // er zijn onbekende buren, dan kunnen we niks $toetsen[$c['list'][$i]]['color'] = array(); for ($j = 0; $j < $max_colors; $j++) if (!$bad_colors[$j]) $toetsen[$c['list'][$i]]['color'][] = $j + 1; } //dot($c['list'], "out".sprintf("%04d", $counter++).".dot"); $solution = array(); for ($i = 0; $i < count($c['list'])/*$no*/; $i++) { if ($toetsen[$c['list'][$i]]['color'] === NULL) { $solution[] = '*'; } else if (!is_array($toetsen[$c['list'][$i]]['color'])) { $ret['colors'][$i][$toetsen[$c['list'][$i]]['color']]++; $solution[] = $toetsen[$c['list'][$i]]['color']; } else { if (count($toetsen[$c['list'][$i]]['color']) == 1) { $ret['colors'][$i][$toetsen[$c['list'][$i]]['color'][0] - 1]++; $solution[] = $toetsen[$c['list'][$i]]['color'][0] - 1; } else { $temp = array(); for ($j = 0; $j < count($toetsen[$c['list'][$i]]['color']); $j++) { $ret['colors'][$i][$toetsen[$c['list'][$i]]['color'][$j] - 1]++; $temp[] = $toetsen[$c['list'][$i]]['color'][$j] - 1; } $solution[] = $temp; } } } if (count($ret['solutions']) < 10) $ret['solutions'][] = $solution; for ($i = $no; $i < count($c['list']); $i++) { $toetsen[$c['list'][$i]]['color'] = NULL; } } else { $bad_colors = get_bad_colors($toetsen, $c, $no, $max_colors); foreach ($bad_colors as $color => $badness) { if ($badness > 0) continue; $toetsen[$c['list'][$no]]['color'] = $color; if ($color < $used_colors) { beschouw($c, $no + 1, $used_colors, $max_colors); } else if ($color == $used_colors) { // if a color never has been used before, mark it used beschouw($c, $no + 1, $used_colors + 1, $max_colors); } // else: do nothing } $toetsen[$c['list'][$no]]['color'] = NULL; } if ($no == 0) return $ret; } $max_max_colors = 1; foreach ($collections as &$collection) { usort($collection['list'], function ($a, $b) use (&$toetsen) { return count($toetsen[$a]['neighbors']) < count($toetsen[$b]['neighbors']); }); $collection['indices'] = array(); for ($i = 0; $i < count($collection['list']); $i++) { $collection['indices'][$collection['list'][$i]] = $i; } foreach ($collection['list'] as $toets) { uksort($toetsen[$toets]['neighbors'], function ($a, $b) use (&$collection) { return $collection['indices'][$a] > $collection['indices'][$b]; }); } $dot = dot($collection['list'], $toetsen); $collection['filename'] = $svg_filename = $datadir.md5($dot).'.svg'; if (!file_exists($svg_filename)) { $dot_filename = tempnam(sys_get_temp_dir(), 'her'); //if (file_put_contents($datadir.md5($dot).'.dot', $dot) === FALSE) error_system('unable to write .dot file'); if (file_put_contents($dot_filename, $dot) === FALSE) error_system('unable to write .dot file'); $out = system("dot -Tsvg -o$svg_filename $dot_filename", $ret); if ($ret != 0) error_system("error $ret creating .svg from .dot: $out"); unlink($dot_filename); } $max_colors = 0; do { $max_colors++; $collection['max_colors'] = $max_colors; $collection['solver'] = beschouw($collection, 0, 0, $max_colors); $no_solutions = count($collection['solver']['solutions']); } while (!$no_solutions); if ($max_colors > $max_max_colors) $max_max_colors = $max_colors; /* echo("
"); echo("required colors: $max_colors, no_solutions=$no_solutions\n"); foreach ($ret['solutions'] as $solution) { foreach ($solution as $color) echo($color); echo("\n"); } foreach ($collection['list'] as $toets) { echo($toets.' '."{$toetsen[$toets]['color']}\n"); } echo(""); */ } unset($collection); $colors = array("#fbb4ae", "#b3cde3", "#ccebc5", "#decbe4", "#fed9a6", "#ffffcc", "#e5d8bd", "#fddaec", "#f2f2f2"); $colors['*'] = "#ffffff"; $colors['?'] = "#ffffff"; $gradients = array(); foreach ($collections as &$collection) { for ($i = 0; $i < count($collection['solver']['solutions']); $i++) { $colorset = ''; foreach ($collection['indices'] as $idx) { //echo("$idx\n"); //echo($toetsen[$collection['list'][$idx]]['id'].' color: '.$collection['solver']['solutions'][0][$idx]."\n"); $color = $collection['solver']['solutions'][$i][$idx]; if (!is_array($color) && $colors[$color]) $colorset .= "\$('#n".$toetsen[$collection['list'][$idx]]['id']." > polygon').attr('fill', '".$colors[$color]."');\n"; else if (is_array($color) && count($color) > 1) { $gradname = 'grad'.implode('', $color); $colorset .= "\$('#n".$toetsen[$collection['list'][$idx]]['id']." > polygon').attr('fill', 'url(#$gradname)');\n"; if (!isset($gradients[$gradname])) { $grad = '
ST's die buiten beschouwing zijn gelaten:
print_r($buiten_beschouwing); ?>
ST's die gekoppeld zijn:
print_r($samenvoegen); ?>