Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Only do certain optimization when applicable; add more general tests

  • Loading branch information...
commit 5c057528b7a8bcd948795ca4a5e50acb913715dd 1 parent 6d56d5f
@tylo42 authored
Showing with 86 additions and 83 deletions.
  1. +6 −10 Scheduler.cpp
  2. +5 −5 Scheduler.hpp
  3. +4 −0 test_expected.txt
  4. +71 −68 test_main.cpp
View
16 Scheduler.cpp
@@ -41,7 +41,7 @@ Scheduler::Scheduler(size_t people, size_t groups, size_t rounds, size_t min, si
#endif
}
-size_t Scheduler::solve(eCount count) {
+size_t Scheduler::solve(eCount count) const {
m_solutions.clear();
if(m_rounds == 0) return 0;
@@ -70,11 +70,6 @@ size_t Scheduler::solve(eCount count) {
}
ASSERT(group->size() == m_people);
round.push_back(group);
-
- // Remove from group_comb, it is no longer valid, ASSUMING ONLY ONE ONE PAIRING
- //group_set::const_iterator it = group_comb.find(group);
- //ASSERT(it != group_comb.end());
- //group_comb.erase(it);
}
ASSERT(round.size() == m_groups);
@@ -82,19 +77,20 @@ size_t Scheduler::solve(eCount count) {
return solve_next_round(count, schedule, group_comb, group_comb.begin());
}
-size_t Scheduler::solve_next_round(eCount count, Schedule & schedule, const group_set & group_comb, const group_set::const_iterator & first_group_it) {
+size_t Scheduler::solve_next_round(eCount count, Schedule & schedule, const group_set & group_comb, const group_set::const_iterator & first_group_it) const {
if(m_rounds == schedule.round_size() && schedule.check(m_min, m_max)) {
// we have a solution
m_solutions.push_back(schedule);
return 1;
}
- group_set group_comb_copy(first_group_it, group_comb.end(), group_comb.value_comp());
+ const group_set::const_iterator first = ((m_max > 1) ? group_comb.begin() : first_group_it); // optimization when players can't be in the same group more than once
+ group_set group_comb_copy(first, group_comb.end(), group_comb.value_comp());
remove_invalid_groups(schedule, group_comb_copy);
return solve_first_group_next_round(count, schedule, group_comb_copy);
}
-size_t Scheduler::solve_first_group_next_round(eCount count, Schedule & schedule, const group_set & group_comb) {
+size_t Scheduler::solve_first_group_next_round(eCount count, Schedule & schedule, const group_set & group_comb) const {
size_t solutions = 0;
round_type round;
group_set::const_iterator it = group_comb.begin();
@@ -108,7 +104,7 @@ size_t Scheduler::solve_first_group_next_round(eCount count, Schedule & schedule
return solutions;
}
-size_t Scheduler::solve_next_group(eCount count, Schedule & schedule, round_type & round, const group_set & group_comb, group_set::const_iterator cur, const group_set::const_iterator & first_group_it) {
+size_t Scheduler::solve_next_group(eCount count, Schedule & schedule, round_type & round, const group_set & group_comb, group_set::const_iterator cur, const group_set::const_iterator & first_group_it) const {
size_t solutions = 0;
#ifdef UNKNOWN_OPTIMIZATION
if(std::distance(it, group_comb.end()) < static_cast<int>(m_groups)) {
View
10 Scheduler.hpp
@@ -28,7 +28,7 @@ class Scheduler {
Scheduler(size_t people, size_t groups, size_t rounds, size_t min, size_t max);
// Solve for a schedule with given constraints, returns number of solutions found
- size_t solve(eCount count = ALL);
+ size_t solve(eCount count = ALL) const;
solution_set const & solutions() const { return m_solutions; }
@@ -44,7 +44,7 @@ class Scheduler {
* @param[in] first_group_it a pointer to where the next first group should start
* @return number of solutions with schedule
*/
- size_t solve_next_round(eCount count, Schedule & schedule, const group_set & group_comb, const group_set::const_iterator & first_group_it);
+ size_t solve_next_round(eCount count, Schedule & schedule, const group_set & group_comb, const group_set::const_iterator & first_group_it) const;
/**
* Try the first group of the next round
@@ -54,7 +54,7 @@ class Scheduler {
* @param[in] group_comb set of groups still valid in schedule
* @return number of solutions with schedule
*/
- size_t solve_first_group_next_round(eCount count, Schedule & schedule, const group_set & group_comb);
+ size_t solve_first_group_next_round(eCount count, Schedule & schedule, const group_set & group_comb) const;
/**
* Find the next group in round
@@ -67,7 +67,7 @@ class Scheduler {
* @param[in] first_group_it a pointer to where the next first group should start
* @return number of solutions with schedule plus round
*/
- size_t solve_next_group(eCount count, Schedule & schedule, round_type & round, const group_set & group_comb, group_set::const_iterator cur, const group_set::const_iterator & first_group_it);
+ size_t solve_next_group(eCount count, Schedule & schedule, round_type & round, const group_set & group_comb, group_set::const_iterator cur, const group_set::const_iterator & first_group_it) const;
bool valid_group(const group_type & group, const round_type & round) const;
@@ -82,7 +82,7 @@ class Scheduler {
size_t m_rounds;
size_t m_min;
size_t m_max;
- solution_set m_solutions;
+ mutable solution_set m_solutions;
};
#endif // SCHEDULER_HPP
View
4 test_expected.txt
@@ -221,5 +221,9 @@
{0, 9, 13, 17, 21} {1, 5, 14, 18, 22} {2, 6, 10, 19, 23} {3, 7, 11, 15, 24} {4, 8, 12, 16, 20}
{0, 1} {2, 3}
+{0, 1} {2, 3}
+{0, 1} {2, 3}
+
+{0, 1} {2, 3}
{0, 2} {1, 3}
{0, 3} {1, 2}
View
139 test_main.cpp
@@ -10,35 +10,8 @@
static void test_scheduler(std::ostream & os);
static void test_schedule(std::ostream & os);
+static bool files_match(const std::string & expected, const std::string & actual);
-static bool files_match(const std::string & expected, const std::string & actual) {
- std::ifstream expected_file(expected.c_str());
- std::ifstream actual_file(actual.c_str());
- BOOST_CHECK(expected_file.is_open());
- BOOST_CHECK(actual_file.is_open());
-
- bool match = true;
- while(!expected_file.eof() && !actual_file.eof()) {
- std::string expected_line;
- std::string actual_line;
- expected_file >> expected_line;
- actual_file >> actual_line;
-
- if(expected_line != actual_line) {
- match = false;
- break;
- }
- }
-
- if(!expected_file.eof() || !actual_file.eof()) {
- match = false;
- }
-
- actual_file.close();
- expected_file.close();
-
- return match;
-}
int test_main(int, char *[]) {
clock_t start = clock();
@@ -62,8 +35,7 @@ static void print_schedules(const Scheduler::solution_set & schedules, std::ostr
}
}
-static void test_all_scheduler(size_t people, size_t groups, size_t num_solutions, std::ostream & os) {
- Scheduler s(people, groups);
+static void test_all_scheduler(const Scheduler & s, size_t num_solutions, std::ostream & os) {
BOOST_CHECK(num_solutions == s.solve());
BOOST_CHECK(num_solutions == s.solutions().size());
@@ -80,8 +52,7 @@ static void test_all_scheduler(size_t people, size_t groups, size_t num_solution
print_schedules(s.solutions(), os);
}
-static void test_one_scheduler(size_t people, size_t groups, std::ostream & os) {
- Scheduler s(people, groups);
+static void test_one_scheduler(const Scheduler & s, std::ostream & os) {
BOOST_CHECK(1 == s.solve(Scheduler::ONE));
BOOST_CHECK(1 == s.solutions().size());
@@ -89,44 +60,47 @@ static void test_one_scheduler(size_t people, size_t groups, std::ostream & os)
}
static void test_scheduler(std::ostream & os) {
- test_all_scheduler(0, 0, 0, os);
- test_all_scheduler(1, 0, 0, os);
- test_all_scheduler(0, 1, 0, os);
-
- test_all_scheduler(1, 1, 1, os);
- test_all_scheduler(1, 2, 0, os);
- test_all_scheduler(1, 3, 0, os);
- test_all_scheduler(1, 4, 0, os);
- test_all_scheduler(1, 5, 0, os);
-
- test_all_scheduler(2, 1, 1, os);
- test_all_scheduler(2, 2, 1, os);
- test_all_scheduler(2, 3, 2, os);
- test_one_scheduler(2, 4, os);
- test_one_scheduler(2, 5, os);
- test_one_scheduler(2, 6, os);
- test_one_scheduler(2, 7, os);
- test_one_scheduler(2, 8, os);
- test_one_scheduler(2, 9, os);
- test_one_scheduler(2, 10, os);
-
- test_all_scheduler(3, 1, 1, os);
- test_all_scheduler(3, 2, 0, os);
- test_all_scheduler(3, 3, 12, os);
- test_all_scheduler(3, 4, 0, os);
+ test_all_scheduler(Scheduler(0, 0), 0, os);
+ test_all_scheduler(Scheduler(1, 0), 0, os);
+ test_all_scheduler(Scheduler(0, 1), 0, os);
+
+ test_all_scheduler(Scheduler(1, 1), 1, os);
+ test_all_scheduler(Scheduler(1, 2), 0, os);
+ test_all_scheduler(Scheduler(1, 3), 0, os);
+ test_all_scheduler(Scheduler(1, 4), 0, os);
+ test_all_scheduler(Scheduler(1, 5), 0, os);
+
+ test_all_scheduler(Scheduler(2, 1), 1, os);
+ test_all_scheduler(Scheduler(2, 2), 1, os);
+ test_all_scheduler(Scheduler(2, 3), 2, os);
+ test_one_scheduler(Scheduler(2, 4), os);
+ test_one_scheduler(Scheduler(2, 5), os);
+ test_one_scheduler(Scheduler(2, 6), os);
+ test_one_scheduler(Scheduler(2, 7), os);
+ test_one_scheduler(Scheduler(2, 8), os);
+ test_one_scheduler(Scheduler(2, 9), os);
+ test_one_scheduler(Scheduler(2, 10), os);
+
+ test_all_scheduler(Scheduler(3, 1), 1, os);
+ test_all_scheduler(Scheduler(3, 2), 0, os);
+ test_all_scheduler(Scheduler(3, 3), 12, os);
+ test_all_scheduler(Scheduler(3, 4), 0, os);
//test_one_scheduler(3, 5, os); // for now this one takes too long to run after each compile
- test_all_scheduler(4, 1, 1, os);
- test_all_scheduler(4, 2, 0, os);
- test_all_scheduler(4, 3, 0, os);
- test_one_scheduler(4, 4, os);
- test_all_scheduler(4, 5, 0, os);
-
- test_all_scheduler(5, 1, 1, os);
- test_all_scheduler(5, 2, 0, os);
- test_all_scheduler(5, 3, 0, os);
- test_all_scheduler(5, 4, 0, os);
- test_one_scheduler(5, 5, os);
+ test_all_scheduler(Scheduler(4, 1), 1, os);
+ test_all_scheduler(Scheduler(4, 2), 0, os);
+ test_all_scheduler(Scheduler(4, 3), 0, os);
+ test_one_scheduler(Scheduler(4, 4), os);
+ test_all_scheduler(Scheduler(4, 5), 0, os);
+
+ test_all_scheduler(Scheduler(5, 1), 1, os);
+ test_all_scheduler(Scheduler(5, 2), 0, os);
+ test_all_scheduler(Scheduler(5, 3), 0, os);
+ test_all_scheduler(Scheduler(5, 4), 0, os);
+ test_one_scheduler(Scheduler(5, 5), os);
+
+ // Test more general cases
+ test_one_scheduler(Scheduler(2, 2, 3, 0, 3), os);
}
static Schedule::round_type make_round(const Schedule::group_type & g1, const Schedule::group_type & g2) {
@@ -164,3 +138,32 @@ static void test_schedule(std::ostream & os) {
BOOST_CHECK(0 == s.round_size());
}
}
+
+static bool files_match(const std::string & expected, const std::string & actual) {
+ std::ifstream expected_file(expected.c_str());
+ std::ifstream actual_file(actual.c_str());
+ BOOST_CHECK(expected_file.is_open());
+ BOOST_CHECK(actual_file.is_open());
+
+ bool match = true;
+ while(!expected_file.eof() && !actual_file.eof()) {
+ std::string expected_line;
+ std::string actual_line;
+ expected_file >> expected_line;
+ actual_file >> actual_line;
+
+ if(expected_line != actual_line) {
+ match = false;
+ break;
+ }
+ }
+
+ if(!expected_file.eof() || !actual_file.eof()) {
+ match = false;
+ }
+
+ actual_file.close();
+ expected_file.close();
+
+ return match;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.