Skip to content

Commit

Permalink
Merge branch 'unify_objective_constraint'
Browse files Browse the repository at this point in the history
  • Loading branch information
funkey committed Apr 7, 2023
2 parents 0e28df2 + bc546c7 commit 89b00c8
Show file tree
Hide file tree
Showing 27 changed files with 254 additions and 684 deletions.
79 changes: 21 additions & 58 deletions ilpy/decl.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ cdef extern from 'impl/solvers/Solution.h':
void setValue(double value)
double getValue()

cdef extern from 'impl/solvers/QuadraticObjective.cpp':
cdef extern from 'impl/solvers/Objective.cpp':
pass

cdef extern from 'impl/solvers/QuadraticObjective.h':
cdef cppclass QuadraticObjective:
QuadraticObjective() except +
QuadraticObjective(unsigned int) except +
cdef extern from 'impl/solvers/Objective.h':
cdef cppclass Objective:
Objective() except +
Objective(unsigned int) except +
void setConstant(double)
double getConstant()
void setCoefficient(unsigned int, double)
Expand All @@ -59,25 +59,12 @@ cdef extern from 'impl/solvers/QuadraticObjective.h':
void resize(unsigned int)
unsigned int size()

cdef extern from 'impl/solvers/LinearObjective.h':
cdef cppclass LinearObjective:
LinearObjective() except +
LinearObjective(unsigned int) except +
void setConstant(double)
double getConstant()
void setCoefficient(unsigned int, double)
const vector[double]& getCoefficients()
void setSense(Sense)
Sense getSense()
void resize(unsigned int)
unsigned int size()

cdef extern from 'impl/solvers/QuadraticConstraint.cpp':
cdef extern from 'impl/solvers/Constraint.cpp':
pass

cdef extern from 'impl/solvers/QuadraticConstraint.h':
cdef cppclass QuadraticConstraint:
QuadraticConstraint() except +
cdef extern from 'impl/solvers/Constraint.h':
cdef cppclass Constraint:
Constraint() except +
void setCoefficient(unsigned int, double)
const map[unsigned int, double]& getCoefficients()
void setQuadraticCoefficient(unsigned int, unsigned int, double)
Expand All @@ -88,46 +75,23 @@ cdef extern from 'impl/solvers/QuadraticConstraint.h':
double getValue()
bool isViolated(const Solution&)

cdef extern from 'impl/solvers/LinearConstraint.h':
cdef cppclass LinearConstraint:
LinearConstraint() except +
void setCoefficient(unsigned int, double)
const map[unsigned int, double]& getCoefficients()
void setRelation(Relation)
void setValue(double)
Relation getRelation()
double getValue()
bool isViolated(const Solution&)

cdef extern from 'impl/solvers/LinearConstraints.cpp':
cdef extern from 'impl/solvers/Constraints.cpp':
pass

cdef extern from 'impl/solvers/LinearConstraints.h':
cdef cppclass LinearConstraints:
LinearConstraints() except +
cdef extern from 'impl/solvers/Constraints.h':
cdef cppclass Constraints:
Constraints() except +
void clear()
void add(LinearConstraint&)
void addAll(LinearConstraints&);
void add(Constraint&)
void addAll(Constraints&);
unsigned int size()

cdef extern from 'impl/solvers/LinearSolverBackend.h':
cdef cppclass LinearSolverBackend:
void initialize(unsigned int, VariableType, map[unsigned int, VariableType]&) except +
void setObjective(LinearObjective&)
void setConstraints(LinearConstraints&)
void addConstraint(LinearConstraint&)
void setTimeout(double)
void setOptimalityGap(double, bool)
void setNumThreads(unsigned int)
void setVerbose(bool)
bool solve(Solution& solution, string& message)

cdef extern from 'impl/solvers/QuadraticSolverBackend.h':
cdef cppclass QuadraticSolverBackend:
cdef extern from 'impl/solvers/SolverBackend.h':
cdef cppclass SolverBackend:
void initialize(unsigned int, VariableType, map[unsigned int, VariableType]&) except +
void setObjective(QuadraticObjective&)
void setConstraints(LinearConstraints&)
void addConstraint(QuadraticConstraint&)
void setObjective(Objective&)
void setConstraints(Constraints&)
void addConstraint(Constraint&)
void setTimeout(double)
void setOptimalityGap(double, bool)
void setNumThreads(unsigned int)
Expand All @@ -145,5 +109,4 @@ cdef extern from 'impl/solvers/SolverFactory.cpp':

cdef extern from 'impl/solvers/SolverFactory.h':
cdef cppclass SolverFactory:
shared_ptr[LinearSolverBackend] createLinearSolverBackend(Preference) except +
shared_ptr[QuadraticSolverBackend] createQuadraticSolverBackend(Preference) except +
shared_ptr[SolverBackend] createSolverBackend(Preference) except +
10 changes: 5 additions & 5 deletions ilpy/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import ast
from typing import Any, Sequence, Union

from ilpy.wrapper import LinearConstraint, Relation
from ilpy.wrapper import Constraint, Relation

Number = Union[float, int]

Expand All @@ -22,7 +22,7 @@ class Expression(ast.AST):
Or, use ``print(expr)` to see the string representation of an expression.
"""

def constraint(self) -> LinearConstraint:
def constraint(self) -> Constraint:
"""Create a linear constraint from this expression."""
return _expression_to_constraint(self)

Expand Down Expand Up @@ -198,9 +198,9 @@ def _get_relation(expr: Expression) -> Relation | None:
return relation


def _expression_to_constraint(expr: Expression) -> LinearConstraint:
"""Convert an expression to a `LinearConstraint`."""
constraint = LinearConstraint()
def _expression_to_constraint(expr: Expression) -> Constraint:
"""Convert an expression to a `Constraint`."""
constraint = Constraint()
if relation := _get_relation(expr):
constraint.set_relation(relation)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "QuadraticConstraint.h"
#include "Constraint.h"

QuadraticConstraint::QuadraticConstraint() :
Constraint::Constraint() :
_relation(LessEqual) {}

void
QuadraticConstraint::setCoefficient(unsigned int varNum, double coef) {
Constraint::setCoefficient(unsigned int varNum, double coef) {

if (coef == 0) {

Expand All @@ -17,7 +17,7 @@ QuadraticConstraint::setCoefficient(unsigned int varNum, double coef) {
}

void
QuadraticConstraint::setQuadraticCoefficient(unsigned int varNum1, unsigned int varNum2, double coef) {
Constraint::setQuadraticCoefficient(unsigned int varNum1, unsigned int varNum2, double coef) {

if (coef == 0) {

Expand All @@ -30,12 +30,12 @@ QuadraticConstraint::setQuadraticCoefficient(unsigned int varNum1, unsigned int
}

void
QuadraticConstraint::setRelation(Relation relation) {
Constraint::setRelation(Relation relation) {

_relation = relation;
}

bool QuadraticConstraint::isViolated(const Solution & solution){
bool Constraint::isViolated(const Solution & solution){

double s = 0;

Expand All @@ -58,36 +58,36 @@ bool QuadraticConstraint::isViolated(const Solution & solution){


void
QuadraticConstraint::setValue(double value) {
Constraint::setValue(double value) {

_value = value;
}

const std::map<unsigned int, double>&
QuadraticConstraint::getCoefficients() const {
Constraint::getCoefficients() const {

return _coefs;
}

const std::map<std::pair<unsigned int, unsigned int>, double>&
QuadraticConstraint::getQuadraticCoefficients() const {
Constraint::getQuadraticCoefficients() const {

return _quadraticCoefs;
}

const Relation&
QuadraticConstraint::getRelation() const {
Constraint::getRelation() const {

return _relation;
}

double
QuadraticConstraint::getValue() const {
Constraint::getValue() const {

return _value;
}

std::ostream& operator<<(std::ostream& out, const QuadraticConstraint& constraint) {
std::ostream& operator<<(std::ostream& out, const Constraint& constraint) {

typedef std::map<unsigned int, double>::value_type pair_t;
for (const pair_t& pair : constraint.getCoefficients())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef INFERENCE_QUADRATIC_CONSTRAINT_H__
#define INFERENCE_QUADRATIC_CONSTRAINT_H__
#ifndef INFERENCE_CONSTRAINT_H__
#define INFERENCE_CONSTRAINT_H__

#include <map>
#include <ostream>
Expand All @@ -9,11 +9,11 @@
/**
* A sparse quadratic constraint.
*/
class QuadraticConstraint {
class Constraint {

public:

QuadraticConstraint();
Constraint();

void setCoefficient(unsigned int varNum, double coef);

Expand Down Expand Up @@ -44,7 +44,7 @@ class QuadraticConstraint {
double _value;
};

std::ostream& operator<<(std::ostream& out, const QuadraticConstraint& constraint);
std::ostream& operator<<(std::ostream& out, const Constraint& constraint);

#endif // INFERENCE_QUADRATIC_CONSTRAINT_H__
#endif // INFERENCE_CONSTRAINT_H__

40 changes: 40 additions & 0 deletions ilpy/impl/solvers/Constraints.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "Constraints.h"

Constraints::Constraints(size_t size) {

_constraints.resize(size);
}

void
Constraints::add(const Constraint& constraint) {

_constraints.push_back(constraint);
}

void
Constraints::addAll(const Constraints& constraints) {

_constraints.insert(_constraints.end(), constraints.begin(), constraints.end());
}

std::vector<unsigned int>
Constraints::getConstraints(const std::vector<unsigned int>& variableIds) {

std::vector<unsigned int> indices;

for (unsigned int i = 0; i < size(); i++) {

Constraint& constraint = _constraints[i];

for (unsigned int v : variableIds) {

if (constraint.getCoefficients().count(v) != 0) {

indices.push_back(i);
break;
}
}
}

return indices;
}
73 changes: 73 additions & 0 deletions ilpy/impl/solvers/Constraints.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#ifndef INFERENCE_CONSTRAINTS_H__
#define INFERENCE_CONSTRAINTS_H__

#include "Constraint.h"

class Constraints {

typedef std::vector<Constraint> constraints_type;

public:

typedef constraints_type::iterator iterator;

typedef constraints_type::const_iterator const_iterator;

/**
* Create a new set of constraints and allocate enough memory to hold
* 'size' constraints. More or less constraints can be added, but
* memory might be wasted (if more allocated then necessary) or unnecessary
* reallocations might occur (if more added than allocated).
*
* @param size The number of constraints to reserve memory for.
*/
Constraints(size_t size = 0);

/**
* Remove all constraints from this set of constraints.
*/
void clear() { _constraints.clear(); }

/**
* Add a constraint.
*
* @param constraint The constraint to add.
*/
void add(const Constraint& constraint);

/**
* Add a set of constraints.
*
* @param constraints The set of constraints to add.
*/
void addAll(const Constraints& constraints);

/**
* @return The number of constraints in this set.
*/
unsigned int size() const { return _constraints.size(); }

const const_iterator begin() const { return _constraints.begin(); }

iterator begin() { return _constraints.begin(); }

const const_iterator end() const { return _constraints.end(); }

iterator end() { return _constraints.end(); }

const Constraint& operator[](size_t i) const { return _constraints[i]; }

Constraint& operator[](size_t i) { return _constraints[i]; }

/**
* Get a linst of indices of constraints that use the given variables.
*/
std::vector<unsigned int> getConstraints(const std::vector<unsigned int>& variableIds);

private:

constraints_type _constraints;
};

#endif // INFERENCE_CONSTRAINTS_H__

Loading

0 comments on commit 89b00c8

Please sign in to comment.