Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Open WBO totalizer encoding for At Most One constraints #26

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
a5855f9
Use Open-WBOs Totalizer encoding for at most 1 constraint
sukrutrao Sep 13, 2018
c004b2f
Merge branch 'development' of github.com:GoodDeeds/Timetabler into de…
sukrutrao Sep 13, 2018
a86e126
Merge branch 'development' of github.com:GoodDeeds/Timetabler into de…
prateekkumarweb Sep 13, 2018
e5c7f57
Fix compiler error
prateekkumarweb Sep 13, 2018
f25a078
Use customized totalizer encoding based off OpenWBO
sukrutrao Sep 13, 2018
1da2314
Merge branch 'master' into totalizer
prateekkumarweb Oct 27, 2018
dea0f8b
Merge branch 'master' into totalizer
prateekkumarweb Nov 2, 2018
d2249c9
Merge branch 'master' into totalizer
prateekkumarweb Nov 2, 2018
b641830
Fix bug, minor improvements
prateekkumarweb Nov 9, 2018
0b0cbf7
Merge branch 'master' into bugfix
sukrutrao Nov 10, 2018
0a1b7b4
Use logger to indicate completed parsing
sukrutrao Nov 10, 2018
03dc6cc
Reset variables in custom_parser: constraint_expr
prateekkumarweb Nov 11, 2018
0a29355
Merge branch 'bugfix' of github.com:GoodDeeds/Timetabler into bugfix
prateekkumarweb Nov 11, 2018
c863bb7
yaml-cpp: Do not build unnecessary tools
prateekkumarweb Nov 11, 2018
402552b
snap: Fix issue with accessing home folder
prateekkumarweb Nov 11, 2018
c9c1f13
snap: Fix deprecated warning
prateekkumarweb Nov 11, 2018
d7cd72a
constraint_adder: unbundle some of the predefined constraints
prateekkumarweb Nov 13, 2018
f2d3859
Wrap custom parser grammar in namespace
prateekkumarweb Nov 13, 2018
e226f3f
doxygen: generate docs for custom constraint grammar
prateekkumarweb Nov 15, 2018
fdc5d46
doxygen: Add documentation of some of the undocumented functions
prateekkumarweb Nov 15, 2018
5737962
Predefined Constraint: Add high level vars for each course
prateekkumarweb Nov 17, 2018
c8b1d77
cmake: Add -Wpedantic flag
prateekkumarweb Nov 26, 2018
2885211
Change MinorType to enum class (also fixes isMinor bug)
prateekkumarweb Nov 29, 2018
2fec7b7
Parser: CSV Parser program Core/Elective/No
prateekkumarweb Nov 29, 2018
86478f6
Generate version number from git
prateekkumarweb Dec 12, 2018
9c552fe
Run gen_version.sh inside cmake
prateekkumarweb Dec 12, 2018
70cd9b2
Merge branch 'bugfix' into totalizer
prateekkumarweb Dec 12, 2018
95d4066
Fix compiling issue in src/totalizer.cpp
prateekkumarweb Dec 12, 2018
6440035
gen_version.sh: Disable print of version
prateekkumarweb Dec 12, 2018
045526a
snap: Fix version number in snap
prateekkumarweb Dec 12, 2018
8321fde
install_dependencies.sh: Update folder names to not contain version
prateekkumarweb Dec 13, 2018
0843e50
snap: Speed up dependency installation, fix version
prateekkumarweb Dec 13, 2018
0cf3353
Merge branch 'bugfix' into totalizer
prateekkumarweb Dec 13, 2018
b9ccec8
Fix hasEncoding in Totalizer build
prateekkumarweb Dec 13, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,16 @@ dependencies

*.swp

# Version
include/version.h
TimetablerVersion.cmake

# Snap
*.snap
snap/.snapcraft
*.xdelta*
parts
prime
stage
.idea
cmake-build-debug
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ include(TimetablerVersion.cmake)

set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-parentheses -Wno-misleading-indentation")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-parentheses -Wno-misleading-indentation -Wpedantic")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DTIMETABLERDEBUG")

set(OPEN_WBO_PATH "${CMAKE_SOURCE_DIR}/dependencies/open-wbo" CACHE PATH "Open-WBO path")
set(YAML_CPP_PATH "${CMAKE_SOURCE_DIR}/dependencies/yaml-cpp-yaml-cpp-0.6.2" CACHE PATH "YAML CPP path")
set(YAML_CPP_PATH "${CMAKE_SOURCE_DIR}/dependencies/yaml-cpp" CACHE PATH "YAML CPP path")
set(CSVPARSER_PATH "${CMAKE_SOURCE_DIR}/dependencies/CSVparser" CACHE PATH "CSVParser path")
set(PEGTL_PATH "${CMAKE_SOURCE_DIR}/dependencies/PEGTL-2.7.0" CACHE PATH "PEGTL path")
set(PEGTL_PATH "${CMAKE_SOURCE_DIR}/dependencies/PEGTL" CACHE PATH "PEGTL path")
set(ENABLE_TESTS "OFF" CACHE BOOL "Enable Google tests")
set(GTEST_PATH "${CMAKE_SOURCE_DIR}/dependencies/googletest-release-1.8.1" CACHE PATH "GTest path")

Expand Down
6 changes: 0 additions & 6 deletions TimetablerVersion.cmake

This file was deleted.

10 changes: 7 additions & 3 deletions docs/Doxyfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
PROJECT_NAME = "Timetabler"
OUTPUT_DIRECTORY = build
INPUT = .. README.md
EXCLUDE = ../tests ../src/custom_parser.cpp
INPUT = .. ../README.md
EXCLUDE = ../tests ../dependencies ../snap ../parts ../prime ../stage ../build ../docs
RECURSIVE = YES
QUIET = YES
GENERATE_HTML = YES
GENERATE_XML = NO
GENERATE_LATEX = NO
USE_MDFILE_AS_MAINPAGE = README.md
SOURCE_BROWSER = YES
REFERENCED_BY_RELATION = YES
REFERENCES_RELATION = YES
REFERENCES_LINK_SOURCE = YES
USE_MDFILE_AS_MAINPAGE = ../README.md

15 changes: 15 additions & 0 deletions gen_version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
Timetabler_VERSION=$(git describe --always --tags)
if [ -z "$Timetabler_VERSION" ]
then
# Update the below line after every release
Timetabler_VERSION="v0.3.0"
fi
VERSION_INCLUDE_STR="#ifndef __TIMETABLER_VERSION__\n#define __TIMETABLER_VERSION__ \"${Timetabler_VERSION}\"\n#endif"
if [ "$2" == "1" ]
then
echo ${Timetabler_VERSION:1}
else
echo -e $VERSION_INCLUDE_STR > $1/include/version.h
echo "set (Timetabler_VERSION \"${Timetabler_VERSION:1}\")" > $1/TimetablerVersion.cmake
fi
1 change: 1 addition & 0 deletions include/clauses.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Clauses {

public:
Clauses(const std::vector<CClause> &);
Clauses(const vec<Lit> &);
Clauses(const CClause &);
Clauses(const Lit &);
Clauses(const Var &);
Expand Down
15 changes: 8 additions & 7 deletions include/constraint_adder.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,21 @@ class ConstraintAdder {
*/
Timetabler *timetabler;
Clauses fieldSingleValueAtATime(FieldType);
Clauses exactlyOneFieldValuePerCourse(FieldType);
std::vector<Clauses> exactlyOneFieldValuePerCourse(FieldType);
Clauses instructorSingleCourseAtATime();
Clauses classroomSingleCourseAtATime();
Clauses programSingleCoreCourseAtATime();
Clauses minorInMinorTime();
Clauses coreInMorningTime();
Clauses electiveInNonMorningTime();
Clauses existingAssignmentClauses();
Clauses programAtMostOneOfCoreOrElective();
std::vector<Clauses> minorInMinorTime();
std::vector<Clauses> coreInMorningTime();
std::vector<Clauses> electiveInNonMorningTime();
// Clauses existingAssignmentClauses();
std::vector<Clauses> programAtMostOneOfCoreOrElective();

public:
ConstraintAdder(ConstraintEncoder *, Timetabler *);
void addConstraints();
void addSingleConstraint(PredefinedClauses, const Clauses &);
void addSingleConstraint(PredefinedClauses, const Clauses &,
const int course);
};

#endif
8 changes: 0 additions & 8 deletions include/custom_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,6 @@ struct Object {
Object();
};

/**
* @brief Parses custom constraints given in a file and adds them to the
* solver.
*
* @param[in] file The file containing the constraints
* @param constraintEncoder The ConstraintEncoder object
* @param timetabler The Timetabler object
*/
void parseCustomConstraints(std::string file,
ConstraintEncoder *constraintEncoder,
Timetabler *timetabler);
Expand Down
13 changes: 12 additions & 1 deletion include/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#ifndef DATA_H
#define DATA_H

#include <map>
#include <string>
#include <vector>
#include "core/Solver.h"
Expand Down Expand Up @@ -78,7 +79,13 @@ class Data {
* can make the necessary changes.
*/
std::vector<std::vector<Var>> highLevelVars;
std::vector<Var> predefinedConstraintVars;
/**
* Stores the high level variables associated with the predefined constraints.
*/
std::vector<std::vector<Var>> predefinedConstraintVars;
/**
* Stores the high level variables associated with the custom constraints.
*/
std::vector<Var> customConstraintVars;
/**
* Stores the existing assignments for every Course and
Expand Down Expand Up @@ -115,6 +122,10 @@ class Data {
* or to disable certain constraints.
*/
std::vector<int> predefinedClausesWeights;
/**
* Stores the course with the associated custom constraint.
*/
std::map<int, unsigned> customMap;
Data();
};

Expand Down
8 changes: 4 additions & 4 deletions include/fields/course.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ class Course {
* the index of the type in the list of possible isMinor
* values.
*/
int isMinor;
MinorType isMinor;

int classroom = -1;

int slot = -1;

public:
Course(std::string, unsigned, int, int, int);
Course(std::string, unsigned, int, int, int, std::vector<int>);
Course(std::string, unsigned, int, int, MinorType);
Course(std::string, unsigned, int, int, MinorType, std::vector<int>);
void setPrograms(std::vector<int>);
void addProgram(int);
void addClassroom(int);
Expand All @@ -64,7 +64,7 @@ class Course {
int getInstructor();
std::vector<int> getPrograms();
int getSegment();
int getIsMinor();
MinorType getIsMinor();
int getClassroom();
int getSlot();
unsigned getClassSize();
Expand Down
2 changes: 1 addition & 1 deletion include/fields/is_minor.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* value under this enum would be isMinorCourse, and otherwise,
* it would be isNotMinorCourse.
*/
enum MinorType {
enum class MinorType {
/**
* Value when the Course is a minor course
*/
Expand Down
6 changes: 3 additions & 3 deletions include/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ enum FieldType { instructor, segment, isMinor, program, classroom, slot };
* @brief Enum that represents all the predefined constraints.
*/
enum PredefinedClauses {
instructorSingleCourseAtATime,
classroomSingleCourseAtATime,
programSingleCoreCourseAtATime,
instructorSingleCourseAtATime, // 1
classroomSingleCourseAtATime, // 1
programSingleCoreCourseAtATime, // 1
minorInMinorTime,
exactlyOneSlotPerCourse,
exactlyOneInstructorPerCourse,
Expand Down
5 changes: 5 additions & 0 deletions include/global_vars.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
/** @file */

#ifndef GLOBAL_VARS_H
#define GLOBAL_VARS_H

#include "timetabler.h"

/**
* Timetabler variable
*/
extern Timetabler *timetabler;

#endif
6 changes: 5 additions & 1 deletion include/timetabler.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "core/SolverTypes.h"
#include "data.h"
#include "mtl/Vec.h"
#include "totalizer.h"
#include "tsolver.h"

using namespace NSPACE;
Expand Down Expand Up @@ -66,6 +67,7 @@ class Timetabler {
void addClauses(const std::vector<CClause> &, int);
void addClauses(const Clauses &, int);
bool checkAllTrue(const std::vector<Var> &);
bool checkAllTrue(const std::vector<std::vector<Var>> &);
bool isVarTrue(const Var &);
SolverStatus solve();
Var newVar();
Expand All @@ -74,13 +76,15 @@ class Timetabler {
void displayTimeTable();
void displayUnsatisfiedOutputReasons();
void addHighLevelClauses();
void addHighLevelConstraintClauses(PredefinedClauses);
void addHighLevelConstraintClauses(PredefinedClauses, const int course);
void addHighLevelCustomConstraintClauses(int, int);
void writeOutput(std::string);
void addExistingAssignments();
void addToFormula(vec<Lit> &, int);
void addToFormula(Lit, int);
void displayChangesInGivenAssignment();
Clauses generateAtMostKTotalizerEncoding(const std::vector<Var> &,
int64_t rhs = 1);
};

#endif
120 changes: 120 additions & 0 deletions include/totalizer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*!
* \author Ruben Martins - [email protected]
*
* @section LICENSE
*
* Open-WBO, Copyright (c) 2013-2017, Ruben Martins, Vasco Manquinho, Ines Lynce
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

#ifndef Totalizer_h
#define Totalizer_h

#include "MaxSATFormula.h"
#include "core/Solver.h"
#include "core/SolverTypes.h"
#include "mtl/Vec.h"

using namespace openwbo;
using namespace NSPACE;

class TTotalizer {

public:
TTotalizer(int strategy = _INCREMENTAL_NONE_) {
blocking = lit_Undef;
hasEncoding = false;
joinMode = false;
current_cardinality_rhs = -1; // -1 corresponds to an unitialized value
incremental_strategy = strategy;

n_clauses = 0;
n_variables = 0;
}
~TTotalizer() {}

void build(MaxSATFormula *formula, vec<Lit> &lits, int64_t rhs);
void join(MaxSATFormula *formula, vec<Lit> &lits, int64_t rhs);
void update(MaxSATFormula *formula, int64_t rhs, vec<Lit> &lits,
vec<Lit> &assumptions);
void update(MaxSATFormula *formula, int64_t rhs) {
vec<Lit> lits;
vec<Lit> assumptions;
update(formula, rhs, lits, assumptions);
}

bool hasCreatedEncoding() { return hasEncoding; }
void setIncremental(int incremental) { incremental_strategy = incremental; }
int getIncremental() { return incremental_strategy; }

int getNbClauses() { return n_clauses; }
int getNbVariables() { return n_variables; }
void resetCounters() {
n_clauses = 0;
n_variables = 0;
}

vec<Lit> &lits() { return ilits; }
vec<Lit> &outputs() { return cardinality_outlits; }

void addUnitClause(MaxSATFormula *formula, Lit a);
void addBinaryClause(MaxSATFormula *formula, Lit a, Lit b);
void addTernaryClause(MaxSATFormula *formula, Lit a, Lit b, Lit c);

void newSATVariable(MaxSATFormula *formula);

protected:
void encode(MaxSATFormula *formula, vec<Lit> &lits);
void adder(MaxSATFormula *formula, vec<Lit> &left, vec<Lit> &right,
vec<Lit> &output);
void incremental(MaxSATFormula *formula, int64_t rhs);
void toCNF(MaxSATFormula *formula, vec<Lit> &lits);

vec<vec<Lit>> totalizerIterative_left;
vec<vec<Lit>> totalizerIterative_right;
vec<vec<Lit>> totalizerIterative_output;
vec<int64_t> totalizerIterative_rhs;

Lit blocking; // Controls the blocking literal for the incremental blocking.
bool hasEncoding;

// TEMP
vec<Lit> ilits;
vec<Lit> olits;

vec<Lit>
cardinality_inlits; // Stores the inputs of the cardinality
// constraint encoding for the totalizer encoding
vec<Lit> cardinality_outlits; // Stores the outputs of the cardinality
// constraint encoding for incremental solving

int incremental_strategy;
int64_t current_cardinality_rhs;

vec<Lit>
disable_lits; // Contains a vector with a list of blocking literals.
bool joinMode;

int n_clauses;
int n_variables;
};

#endif
12 changes: 8 additions & 4 deletions include/tsolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ using namespace openwbo;
* terminates, it simply returns.
*/
class TSolver : public OLL {
public:
TSolver(int verb = _VERBOSITY_MINIMAL_, int enc = _CARD_TOTALIZER_);
std::vector<lbool> tSearch();
void tWeighted();
public:
TSolver(int, int);
std::vector<lbool> tSearch();
void tWeighted();
// void encodeAtMostK(vec<Lit> &lits, vec<Lit> &assumptions, int64_t rhs =
// 1);
Var newVar();
Lit newLiteral(bool sign = false);
};

#endif
Loading