#!/usr/bin/php $info) { $conflict_ids = explode(',', $info['conflict']); $kind = array(); foreach ($conflict_ids as $cid) { $duur = $momentinfos['data'][$cid]['duur']; echo("duur=$duur"); } exit; } $toetstranslate = get_toetsnames_index($db, $period_id); foreach ($toetsen as $toets => $info) { $toetsen[$toets]['pta_id'] = $toetstranslate[$toets]; } function create_identity_matrix($n) { $matrix = array(); for ($row = 0; $row < $n; $row++) { $newrow = array(); for ($col = 0; $col < $n; $col++) { if ($row == $col) $newrow[] = 1; else $newrow[] = 0; } $matrix[] = $newrow; } return $matrix; } function create_swap_matrix($n, $i, $j) { $matrix = array(); for ($row = 0; $row < $n; $row++) { $newrow = array(); for ($col = 0; $col < $n; $col++) { if ($row == $i && $col == $j) $newrow[] = 1; else if ($row == $j && $col == $i) $newrow[] = 1; else if ($row == $col && $row != $i && $row != $j) $newrow[] = 1; else $newrow[] = 0; } $matrix[] = $newrow; } return $matrix; } function matrix_mul($a, $b) { $out = array(); if (count($a[0]) != count($b)) error_system("incompatible matrices in matrix multiplication"); foreach($a as $row) { $nr = array(); for ($j = 0; $j < count($b[0]); $j++) { //$acc = 0; $nr[$j] = 0; foreach ($row as $i => $elt) /*$acc*/ $nr[$j] += $elt*$b[$i][$j]; //$nr[] = $acc; } $out[] = $nr; } return $out; } function matrix_check($a) { if (!is_array($a)) return false; $row_idx = 0; foreach ($a as $i => $row) { if ($row_idx++ !== $i) return false; if (!is_array($row)) return false; $no_cols= -1; $col_idx = 0; foreach ($row as $j => $elt) { if ($col_idx++ !== $j) return false; if (!is_int($elt)) return false; } if ($no_cols == -1) $no_cols = $col_idx; else if ($no_cols != $col_idx) return false; } if ($row_idx == 0 || $no_cols == 0) return false; return true; } function get_matrix_no_rows($a) { return count($a); } function get_matrix_no_cols($a) { return count($a[0]); } function show_matrix($a) { echo("---\n"); foreach ($a as $row) { echo("["); foreach($row as $elt) { echo(" $elt"); } echo(" ]\n"); } } $collections = array(); make_collections($collections, $toetsen); // // [ 1 2 3 ] [ a b c ] // A_ij = [ 4 5 6 ] B_jk = [ d e f ] // [ 7 8 9 ] [ g h i ] // // C_ik = A_ij * B_jk // $a = array( array( 1, 2, 3 ), array( 4, 5, 6 ), array( 7, 8, 9 ) ); if (!matrix_check($a)) error_system("matrix_check failed"); $b = array( array( -5, -4 ), array( -3, -2 ), array( -1, 0 ) ); $v = array( array( 0 ), array( 1 ), array( 2 ), array( 3 ), array( 4 ), array( 5 ) ); function create_conflict_matrix($conflicts) { $out = array(); $n = count($conflicts); foreach ($conflicts as $i => $c) { $row = array(); for ($j = 0; $j < $n; $j++) { if ($i != $j && isset($c[$j])) $row[] = 1; else $row[] = 0; } $out[] = $row; } return $out; } $cm = create_conflict_matrix($momentinfos['conflicts']); show_matrix($cm); $no_cols = get_matrix_no_cols($cm); echo("cols=$no_cols\n"); $no_rows = get_matrix_no_rows($cm); echo("rows=$no_rows\n"); if ($no_rows == $no_cols) $n = $no_rows; else fatal("impossible"); //$swap01 = create_swap_matrix($n, 0, 1); //$swap12 = create_swap_matrix($n, 1, 2); //show_matrix($swap01); //show_matrix($swap12); //$c = matrix_mul($a, $b); foreach ($momentinfos['from_color'] as $idx => $moment_id) { $m = $momentinfos['data'][$moment_id]; $conflict_with = implode(',', array_filter(array_keys($momentinfos['conflicts'][$idx]), function ($k) use ($idx) { return $idx != $k; })); if ($conflict_with == '') $conflict_with = '-'; $idx_string = str_pad("$idx", 2, ' ', STR_PAD_LEFT); $duur_string = str_pad("{$m['duur']}", 3, ' ', STR_PAD_LEFT); echo("$idx_string {$m['moment_date']} {$m['moment_time_start']}-{$m['moment_time_end']} (conflict {$m['moment_time_conflict_start']}-{$m['moment_time_conflict_end']}) $duur_string $conflict_with\n"); if (!isset($duur[$m['duur']])) $duur[$m['duur']] = array('indices' => array()); //$duren[$m['duur']]['indices'][] = $idx; } //print_r($duren); //$swap0112 = matrix_mul($swap01, $swap12); //$swap1201 = matrix_mul($swap12, $swap01); function create_permutations($arr, $next, $rest, &$out) { for ($i = 0; $i <= $next; $i++) { array_splice($arr, $i, 0, array($next)); if ($rest > 0) create_permutations($arr, $next + 1, $rest - 1, $out); else $out[] = $arr; array_splice($arr, $i, 1); } } $duren = array(); foreach ($momentinfos['max_colors_perduur'] as $duur => $count) { $duren[$duur] = array(); create_permutations(array(), 0, $count - 1, $duren[$duur]); } //print_r($duren); function show_perm($perm) { echo("---\n"); foreach ($perm as $src => $dst) { echo("$src -> $dst\n"); } } function perm_check($perm) { if (!is_array($perm)) return 0; $n = count($perm); //echo("n=$n\n"); $check_src = array_fill(0, $n, 0); $check_dst = array_fill(0, $n, 0); foreach ($perm as $src => $dst) { if (!array_key_exists($src, $check_src)) return 0; if (!array_key_exists($dst, $check_dst)) return 0; $check_src[$src]++; $check_dst[$dst]++; } //print_r($check_src); //print_r($check_dst); for ($i = 0; $i < $n; $i++) { if ($check_src[$i] != 1) return 0; if ($check_dst[$i] != 1) return 0; } return 1; } function perm_invert($perm) { $inverse = array(); foreach($perm as $src => $dst) { $inverse[$dst] = $src; } return $inverse; } function perm_transpositions($perm) { $transpositions = array(); $inverse = perm_invert($perm); //show_perm($inverse); $elts = array(); foreach ($inverse as $src => $dst) { $elts[$src] = array( 'src' => $src, 'dst' => $dst); } //$count = 0; //echo("all data:\n"); //print_r($elts); while (count($elts)) { //echo("restart outer while loop\n"); $elt = end($elts); unset($elts[$elt['src']]); //echo("examining:\n"); //print_r($elt); $src = $elt['src']; //echo("ultimate source is $src\n"); while ($elt['dst'] != $src && count($elts) != 0) { //echo("dst ({$elt['dst']}) is unequal to ultimate source ($src)\n"); //$count++; $transpositions[] = array('a' => $elt['src'], 'b' => $elt['dst']); //print_r($transpositions); $elt = $elts[$elt['dst']]; unset($elts[$elt['src']]); //echo("examining next:\n"); //print_r($elt); //if ($count > 3) exit; } } return $transpositions; } $perm = array(3, 2, 0, 1, 5, 4); // range(0, 5); shuffle($perm); $ts = perm_transpositions($perm); print_r($ts); //$inverse = perm_invert($perm); //show_perm($inverse); if (!perm_check($perm)) fatal("error"); $out = create_identity_matrix(6); show_matrix($out); foreach ($ts as $swap) { $out = matrix_mul($out, create_swap_matrix(6, $swap['a'], $swap['b'])); //show_matrix($out); } show_matrix(matrix_mul($out, $v)); show_perm($perm); //if (!perm_check($inverse)) fatal("error"); /* for ($i = 0; $i < $n; $i++) { for ($j = $i + 1; $j < $n; $j++) { echo("consider $i $j\n"); if ($duur[$i] == $duur[$j]) $swaps[] = create_swap_matrix($n, $i, $j); } } foreach ($swaps as $swap) { show_matrix($swap); } */ //print_r($swaps); //show_matrix($a); //show_matrix($swap01); //show_matrix($swap12); //show_matrix($swap0112); //show_matrix($swap1201); //$swapped = matrix_mul(matrix_mul($swap01, $a), $swap01); //show_matrix($swapped); //show_matrix($b); //show_matrix($c); //$exch = create_swap_matrix(4, 0, 1); //show_matrix($exch); // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // a b c d e f g h i j k l m n o p q r // // 0 a // // 1 b // // 2 c // // 3 d // // 4 e // // 5 f // // 6 g // // 7 h // // 8 i // // 9 j // // 10 k // // 11 l // // 12 m // // 13 n // // 14 o // // 15 p // // 16 q // // 17 r // //print_r($collections); //print_r($toetstranslate); //$ret = check_consistent_and(false, $toetsen, $collections, $momentinfos); //echo("ret=$ret\n"); //foreach ($collections[0]/*['subgraphs'][0]*/['list'] as $toets) { // $count = count($toetsen[$toets]['neighbors']); // echo("$toets alwaysfits={$toetsen[$toets]['alwaysfits']}, subgraph_id={$toetsen[$toets]['subgraph_id']} subgraph_order={$toetsen[$toets]['subgraph_order']} $count\n"); // print_r($toetsen[$toets]); //} //print_r($collections[0]['subgraphs']); /* calc_optimal_order_of_subgraph($collections[0]['subgraphs'][0]['list'], $toetsen); */ /* foreach ($collections[0]['subgraphs'][0]['list'] as $toets) { $count = count($toetsen[$toets]['neighbors']); echo("$toets alwaysfits={$toetsen[$toets]['alwaysfits']}, subgraph_id={$toetsen[$toets]['subgraph_id']} subgraph_order={$toetsen[$toets]['subgraph_order']} $count\n"); } */ /* $list = $collections[0]['subgraphs'][0]['list']; $tmplist = array(); foreach ($list as $elt) { $tmplist[$elt] = $toetsen[$elt]['neighbors']; } $wildfire = array(); $wildfire[] = $list[0]; $neighbors = $tmplist[$list[0]]; unset($tmplist[$list[0]]); foreach ($neighbors as $neighbor => $aantal) { if (!isset($tmplist[$neighbor])) { unset($neighbors[$neighbor]); continue; } } //print_r($tmplist); expand($wildfire, $tmplist, $neighbors); echo("selected order\n"); //print_r($wildfire); foreach ($wildfire as $toets) { $count = count($toetsen[$toets]['neighbors']); echo("$toets alwaysfits={$toetsen[$toets]['alwaysfits']}, subgraph_id={$toetsen[$toets]['subgraph_id']} $count\n"); } */ //print_r($momentinfos); //print_r($toetsen); ?>