DiscreteBayesNet::shared_ptr createSampler(size_t i, size_t slot, vector<Scheduler>& schedulers) { Scheduler scheduler = largeExample(0); // todo: wrong nr students addStudent(scheduler, i); SETDEBUG("Scheduler::buildGraph", false); scheduler.addStudentSpecificConstraints(0, slot); DiscreteBayesNet::shared_ptr chordal = scheduler.eliminate(); // chordal->print(scheduler[i].studentKey(0).name()); // large ! schedulers.push_back(scheduler); return chordal; }
// Sample from solution found above and evaluate cost function DiscreteBayesNet::shared_ptr createSampler(size_t i, size_t slot, vector<Scheduler>& schedulers) { Scheduler scheduler = largeExample(1,false); addStudent(scheduler, i); cout << " creating sampler for " << scheduler.studentName(0) << endl; SETDEBUG("Scheduler::buildGraph", false); // scheduler.print(); scheduler.addStudentSpecificConstraints(0, slot); DiscreteBayesNet::shared_ptr chordal = scheduler.eliminate(); schedulers.push_back(scheduler); return chordal; }
// Solve a series of relaxed problems for maximum flexibility solution void solveStaged(size_t addMutex = 2) { // super-hack! just count... bool debug = false; SETDEBUG("DiscreteConditional::COUNT", true); SETDEBUG("DiscreteConditional::DiscreteConditional", debug); // progress // make a vector with slot availability, initially all 1 // Reads file to get count :-) vector<double> slotsAvailable(largeExample(0).nrTimeSlots(), 1.0); // now, find optimal value for each student, using relaxed mutex constraints for (size_t s = 0; s < NRSTUDENTS; s++) { // add all students first time, then drop last one second time, etc... Scheduler scheduler = largeExample(NRSTUDENTS - s); //scheduler.print(str(boost::format("Scheduler %d") % (NRSTUDENTS-s))); // only allow slots not yet taken scheduler.setSlotsAvailable(slotsAvailable); // BUILD THE GRAPH ! scheduler.buildGraph(addMutex); // Do EXACT INFERENCE gttic_(eliminate); DiscreteBayesNet::shared_ptr chordal = scheduler.eliminate(); gttoc_(eliminate); // find root node // chordal->back()->print("back: "); // chordal->front()->print("front: "); // exit(0); DiscreteConditional::shared_ptr root = chordal->back(); if (debug) root->print(""/*scheduler.studentName(s)*/); // solve root node only Scheduler::Values values; size_t bestSlot = root->solve(values); // get corresponding count DiscreteKey dkey = scheduler.studentKey(NRSTUDENTS - 1 - s); values[dkey.first] = bestSlot; size_t count = (*root)(values); // remove this slot from consideration slotsAvailable[bestSlot] = 0.0; cout << boost::format("%s = %d (%d), count = %d") % scheduler.studentName(NRSTUDENTS-1-s) % scheduler.slotName(bestSlot) % bestSlot % count << endl; } tictoc_print_(); }
/* ************************************************************************* */ void accomodateStudent() { // super-hack! just count... bool debug = false; // SETDEBUG("DiscreteConditional::COUNT",true); SETDEBUG("DiscreteConditional::DiscreteConditional", debug); // progress Scheduler scheduler = largeExample(0); // scheduler.addStudent("Victor E", "Autonomy", "HRI", "AI", // "Henrik Christensen"); scheduler.addStudent("Carlos N", "Perception", "AI", "Autonomy", "Henrik Christensen"); scheduler.print("scheduler"); // rule out all occupied slots vector<size_t> slots; slots += 16, 17, 11, 2, 0, 5, 9, 14; vector<double> slotsAvailable(scheduler.nrTimeSlots(), 1.0); BOOST_FOREACH(size_t s, slots) slotsAvailable[s] = 0; scheduler.setSlotsAvailable(slotsAvailable); // BUILD THE GRAPH ! scheduler.buildGraph(1); // Do EXACT INFERENCE DiscreteBayesNet::shared_ptr chordal = scheduler.eliminate(); // find root node DiscreteConditional::shared_ptr root = chordal->back(); if (debug) root->print(""/*scheduler.studentName(s)*/); // GTSAM_PRINT(*chordal); // solve root node only Scheduler::Values values; size_t bestSlot = root->solve(values); // get corresponding count DiscreteKey dkey = scheduler.studentKey(0); values[dkey.first] = bestSlot; size_t count = (*root)(values); cout << boost::format("%s = %d (%d), count = %d") % scheduler.studentName(0) % scheduler.slotName(bestSlot) % bestSlot % count << endl; // sample schedules for (size_t n = 0; n < 10; n++) { Scheduler::sharedValues sample0 = chordal->sample(); scheduler.printAssignment(sample0); } }
/* ************************************************************************* */ void runLargeExample() { Scheduler scheduler = largeExample(); scheduler.print(); // BUILD THE GRAPH ! size_t addMutex = 3; // SETDEBUG("Scheduler::buildGraph", true); scheduler.buildGraph(addMutex); // Do brute force product and output that to file if (scheduler.nrStudents() == 1) { // otherwise too slow DecisionTreeFactor product = scheduler.product(); product.dot("scheduling-large", false); } // Do exact inference // SETDEBUG("timing-verbose", true); SETDEBUG("DiscreteConditional::DiscreteConditional", true); #define SAMPLE #ifdef SAMPLE tic(2, "large"); DiscreteBayesNet::shared_ptr chordal = scheduler.eliminate(); toc(2, "large"); tictoc_finishedIteration(); tictoc_print(); for (size_t i=0;i<100;i++) { DiscreteFactor::sharedValues assignment = sample(*chordal); vector<size_t> stats(scheduler.nrFaculty()); scheduler.accumulateStats(assignment, stats); size_t max = *max_element(stats.begin(), stats.end()); size_t min = *min_element(stats.begin(), stats.end()); size_t nz = count_if(stats.begin(), stats.end(), NonZero); // cout << min << ", " << max << ", " << nz << endl; if (nz >= 13 && min >=1 && max <= 4) { cout << "======================================================\n"; scheduler.printAssignment(assignment); } } #else tic(2, "large"); DiscreteFactor::sharedValues MPE = scheduler.optimalAssignment(); toc(2, "large"); tictoc_finishedIteration(); tictoc_print(); scheduler.printAssignment(MPE); #endif }
// Solve a series of relaxed problems for maximum flexibility solution void solveStaged(size_t addMutex = 2) { // super-hack! just count... bool debug = false; SETDEBUG("DiscreteConditional::COUNT", true); SETDEBUG("DiscreteConditional::DiscreteConditional", debug); // progress // make a vector with slot availability, initially all 1 // Reads file to get count :-) vector<double> slotsAvailable(largeExample(0).nrTimeSlots(), 1.0); // now, find optimal value for each student, using relaxed mutex constraints for (size_t s = 0; s < 7; s++) { // add all students first time, then drop last one second time, etc... Scheduler scheduler = largeExample(7 - s); //scheduler.print(str(boost::format("Scheduler %d") % (7-s))); // only allow slots not yet taken scheduler.setSlotsAvailable(slotsAvailable); // BUILD THE GRAPH ! scheduler.buildGraph(addMutex); // Do EXACT INFERENCE gttic_(eliminate); DiscreteBayesNet::shared_ptr chordal = scheduler.eliminate(); gttoc_(eliminate); // find root node DiscreteConditional::shared_ptr root = chordal->back(); if (debug) root->print(""/*scheduler.studentName(s)*/); // solve root node only Scheduler::Values values; size_t bestSlot = root->solve(values); // get corresponding count DiscreteKey dkey = scheduler.studentKey(6 - s); values[dkey.first] = bestSlot; size_t count = (*root)(values); // remove this slot from consideration slotsAvailable[bestSlot] = 0.0; cout << boost::format("%s = %d (%d), count = %d") % scheduler.studentName(6-s) % scheduler.slotName(bestSlot) % bestSlot % count << endl; } tictoc_print_(); // Solution with addMutex = 2: (20 secs) // TC = Wed 2 (9), count = 96375041778 // AC = Tue 2 (5), count = 4076088090 // SJ = Mon 1 (0), count = 29596704 // TK = Mon 3 (2), count = 755370 // JH = Wed 4 (11), count = 12000 // TH = Fri 2 (17), count = 220 // MN = Fri 1 (16), count = 5 // // Mutex does make a difference !! }