2.2.11.3. flow/LinExp.hpp

Linear expressions and constraints can be implicitly created from variable specifiers and operators, meaning one rarely need to instantiate them explicitly. However creating the zero-expression might sometimes be useful (e.g., in a sum expression) and can be done simply with the expression flow::LinExp().

A variable specifier is a representation of either a single variable or a sum of variables. The addition, multiplication and division operators are overloaded for variable specifiers and linear expressions such that expressions can be written in the natural manner.

The operators <=, == and >= are overloaded on linear expressions to make the creation of linear constraints easier. However, one argument to the operator must be a linear expression while the other argument must be a number.

2.2.11.3.1. Variable Specifiers

The main variables in a flow model are associated with either vertices or hyperedges in the underlying derivation graph. A variable specifier represents either a set of variables or a single variable, Each specifier has an associated ID that must be registered by a flow model module at the time when the specifier is added to the model. See also Hyperflow Model for an overview of the specifiers.

2.2.11.3.2. Class flow::VarSumVertex

class flow::VarSumVertex

Represents the sum of all variables indicated by the given id. Can be indexed to retrieve a specifier for a single variable in the set.

2.2.11.3.2.1. Synopsis

struct MOD_DECL VarSumVertex {
    explicit VarSumVertex(std::string id);
    MOD_DECL friend std::ostream &operator<<(std::ostream &s, const VarSumVertex &v);
    MOD_DECL friend bool operator==(const VarSumVertex &a, const VarSumVertex &b) {
        return a.id == b.id;
    }
    VarVertex operator[](dg::DG::Vertex v) const;
    VarVertexGraph operator[](std::shared_ptr<graph::Graph> g) const;
public:
    std::string id;
};

2.2.11.3.2.2. Details

explicit VarSumVertex(std::string id)

Construct a variable specifier for a sum of variables associated with vertices in the underlying derivation graph.

friend std::ostream &operator<<(std::ostream &s, const VarSumVertex &v)
friend bool operator==(const VarSumVertex &a, const VarSumVertex &b)
VarVertex operator[](dg::DG::Vertex v) const

Select the variable associated with the given derivation graph vertex. When the resulting specifier is given to a flow model the vertex must be from the underlying derivation graph.

Returns

a variable specifier for a single variable in the set represented by this object.

Throws

LogicError if !v.

VarVertexGraph operator[](std::shared_ptr<graph::Graph> g) const

Select the variable associated with the derivation graph vertex which represents the given graph. When the resulting specifier is given to a flow model such a vertex must then exist in the underlying derivation graph.

Returns

a variable specifier for a single variable in the set represented by this object.

std::string id

The ID for this specifier.

2.2.11.3.3. Class flow::VarVertex

class flow::VarVertex

Represents a variable associated with a single vertex in a derivation graph.

2.2.11.3.3.1. Synopsis

struct MOD_DECL VarVertex {
    explicit VarVertex(std::string id, dg::DG::Vertex v);
    MOD_DECL friend std::ostream &operator<<(std::ostream &s, const VarVertex &v);
    MOD_DECL friend bool operator==(const VarVertex &a, const VarVertex &b) {
        return a.id == b.id && a.v == b.v;
    }
public:
    std::string id;
    dg::DG::Vertex v;
};

2.2.11.3.3.2. Details

explicit VarVertex(std::string id, dg::DG::Vertex v)
Throws

LogicError if !v.

friend std::ostream &operator<<(std::ostream &s, const VarVertex &v)
friend bool operator==(const VarVertex &a, const VarVertex &b)
std::string id
dg::DG::Vertex v

2.2.11.3.4. Class flow::VarVertexGraph

class flow::VarVertexGraph

Represents a variable associated with a single vertex in a derivation graph. This is done implicitly by a graph, and when this specifier is later given to a flow model a vertex with the graph associated must exist in the underlying derivation graph.

2.2.11.3.4.1. Synopsis

struct MOD_DECL VarVertexGraph {
    explicit VarVertexGraph(std::string id, std::shared_ptr<graph::Graph> g);
    MOD_DECL friend std::ostream &operator<<(std::ostream &s, const VarVertexGraph &v);
    MOD_DECL friend bool operator==(const VarVertexGraph &a, const VarVertexGraph &b) {
        return a.id == b.id && a.g == b.g;
    }
public:
    std::string id;
    std::shared_ptr<graph::Graph> g;
};

2.2.11.3.4.2. Details

explicit VarVertexGraph(std::string id, std::shared_ptr<graph::Graph> g)
friend std::ostream &operator<<(std::ostream &s, const VarVertexGraph &v)
friend bool operator==(const VarVertexGraph &a, const VarVertexGraph &b)
std::string id
std::shared_ptr<graph::Graph> g

2.2.11.3.5. Class flow::VarSumEdge

class flow::VarSumEdge

Represents the sum of all variables indicated by the given id. Can be indexed to retrieve a specifier for a single variable in the set.

2.2.11.3.5.1. Synopsis

struct MOD_DECL VarSumEdge {
    explicit VarSumEdge(std::string id);
public:
    MOD_DECL friend std::ostream &operator<<(std::ostream &s, const VarSumEdge &v);
    MOD_DECL friend bool operator==(const VarSumEdge &a, const VarSumEdge &b) {
        return a.id == b.id;
    }
    VarEdge operator[](dg::DG::HyperEdge e) const;
public:
    std::string id;
};

2.2.11.3.5.2. Details

explicit VarSumEdge(std::string id)

Construct a variable specifier for a sum of variables associated with hyperedges in the underlying derivation graph.

friend std::ostream &operator<<(std::ostream &s, const VarSumEdge &v)
friend bool operator==(const VarSumEdge &a, const VarSumEdge &b)
VarEdge operator[](dg::DG::HyperEdge e) const

Select the variable associated with the given derivation graph hyperedge. When the resulting specifier is given to a flow model its underlying derivation graph must be the same as the derivation graph the given hyperedge belongs to.

Returns

a variable specifier for a single variable in the set represented by this object.

Throws

LogicError if !e.

std::string id

2.2.11.3.6. Class flow::VarEdge

class flow::VarEdge

Represents a variable associated with a single hyperedge in a derivation graph. When this specifier is later given to a flow model its underlying derivation graph must be the same as the derivation graph the hyperedge in this object belongs to.

2.2.11.3.6.1. Synopsis

struct MOD_DECL VarEdge {
    explicit VarEdge(std::string id, dg::DG::HyperEdge e);
    MOD_DECL friend std::ostream &operator<<(std::ostream &s, const VarEdge &v);
    MOD_DECL friend bool operator==(const VarEdge &a, const VarEdge &b) {
        return a.id == b.id && a.edge == b.edge;
    }
public:
    std::string id;
    dg::DG::HyperEdge edge;
};

2.2.11.3.6.2. Details

explicit VarEdge(std::string id, dg::DG::HyperEdge e)
Throws

LogicError if !e.

friend std::ostream &operator<<(std::ostream &s, const VarEdge &v)
friend bool operator==(const VarEdge &a, const VarEdge &b)
std::string id
dg::DG::HyperEdge edge

2.2.11.3.7. Class flow::VarSumCustom

class flow::VarSumCustom

Represents the sum of all variables indicated by the given id. Can be indexed to retrieve a specifier for a single variable in the set.

2.2.11.3.7.1. Synopsis

struct MOD_DECL VarSumCustom {
    explicit VarSumCustom(std::string id);
    MOD_DECL friend std::ostream &operator<<(std::ostream &s, const VarSumCustom &v);
    MOD_DECL friend bool operator==(const VarSumCustom &a, const VarSumCustom &b) {
        return a.id == b.id;
    }
    VarCustom operator[](std::string name) const;
public:
    std::string id;
};

2.2.11.3.7.2. Details

explicit VarSumCustom(std::string id)
friend std::ostream &operator<<(std::ostream &s, const VarSumCustom &v)
friend bool operator==(const VarSumCustom &a, const VarSumCustom &b)
VarCustom operator[](std::string name) const

Select the variable associated with the given name. When the resulting specifier is given to a flow model that variable must have been created in the specification in some way.

Returns

a variable specifier for a single variable in the set represented by this object.

std::string id

2.2.11.3.8. Class flow::VarCustom

class flow::VarCustom

Represents a custom variable.

2.2.11.3.8.1. Synopsis

struct MOD_DECL VarCustom {
    explicit VarCustom(std::string id, std::string name);
    MOD_DECL friend std::ostream &operator<<(std::ostream &s, const VarCustom &v);
    MOD_DECL friend bool operator==(const VarCustom &a, const VarCustom &b) {
        return a.id == b.id && a.name == b.name;
    }
public:
    std::string id;
    std::string name;
};

2.2.11.3.8.2. Details

explicit VarCustom(std::string id, std::string name)
friend std::ostream &operator<<(std::ostream &s, const VarCustom &v)
friend bool operator==(const VarCustom &a, const VarCustom &b)
std::string id
std::string name

2.2.11.3.9. Class flow::Var

class flow::Var

Represents any of the variable specifier types, and can be implicitly converted to from any of them.

2.2.11.3.9.1. Synopsis

struct MOD_DECL Var {
    using VarImpl = boost::variant<VarSumVertex, VarVertex, VarVertexGraph, VarSumEdge, VarEdge, VarSumCustom, VarCustom>;
public:
    template<typename T, typename = typename std::enable_if<std::is_convertible<T, VarImpl>::value>::type>
    Var(T v) : v(std::move(v)) {}
    template<typename Visitor>
    typename Visitor::result_type applyVisitor(Visitor visitor) {
        return boost::apply_visitor(visitor, v);
    }
    template<typename Visitor>
    typename Visitor::result_type applyVisitor(Visitor visitor) const {
        return boost::apply_visitor(visitor, v);
    }
    template<typename T>
    const T *get() const {
        return get<T>(&v);
    }
    MOD_DECL friend std::ostream &operator<<(std::ostream &s, const Var &v);
    MOD_DECL friend bool operator==(const Var &a, const Var &b) {
        return a.v == b.v;
    }
private:
    VarImpl v;
};

2.2.11.3.9.2. Details

using VarImpl = boost::variant<VarSumVertex, VarVertex, VarVertexGraph, VarSumEdge, VarEdge, VarSumCustom, VarCustom>
template<typename T>
Var(T v)

Requires T being a variable specifier type.

template<typename Visitor>
typename Visitor::result_type applyVisitor(Visitor visitor)
template<typename Visitor>
typename Visitor::result_type applyVisitor(Visitor visitor) const
Returns

boost::apply_visitor(visitor, v)

template<typename T>
const T *get() const
Returns

get<T>(&v)

friend std::ostream &operator<<(std::ostream &s, const Var &v)
friend bool operator==(const Var &a, const Var &b)
private VarImpl v

2.2.11.3.10. Class flow::LinExp

class flow::LinExp

Represents a linear expression over variable specifiers.

2.2.11.3.10.1. Synopsis

struct MOD_DECL LinExp {
    using IntElement = std::pair<long int, Var>;
    using FloatElement = std::pair<double, Var>;
public:
    LinExp() = default;
    LinExp(Var v);
    template<typename T, typename = typename std::enable_if<std::is_convertible<T, Var>::value>::type>
    LinExp(T v) : LinExp(Var(std::move(v))) {}
    MOD_DECL friend std::ostream &operator<<(std::ostream &s, const LinExp &exp);
    const std::vector<IntElement> &getIntElements() const;
    const std::vector<FloatElement> &getFloatElements() const;
    LinExp &operator+=(const LinExp &e);
    LinExp &operator-=(const LinExp &e);
    LinExp &operator*=(int c);
    LinExp &operator*=(double c);
private:
    std::vector<IntElement> intElements;
    std::vector<FloatElement> floatElements;
};

2.2.11.3.10.2. Details

using IntElement = std::pair<long int, Var>
using FloatElement = std::pair<double, Var>
LinExp() = default
template<typename T>
LinExp(T v)

Requires T convertible to Var.

friend std::ostream &operator<<(std::ostream &s, const LinExp &exp)
const std::vector<IntElement> &getIntElements() const
Returns

all elements of the linear expression that have integer coefficients.

const std::vector<FloatElement> &getFloatElements() const
Returns

all elements of the linear expression that have floating point coefficients.

2.2.11.3.11. Class flow::LinConstraint

class flow::LinConstraint

Represents a linear constraint on the form exp relation bound,

2.2.11.3.11.1. Synopsis

struct MOD_DECL LinConstraint {
    enum class Relation {
        Leq, Eq, Geq
    };
public:
    explicit LinConstraint(LinExp exp, Relation relation, double bound);
    MOD_DECL friend std::ostream &operator<<(std::ostream &s, const LinConstraint &c);
public:
    LinExp exp;
    Relation relation;
    double bound;
};

2.2.11.3.11.2. Details

enum class Relation
explicit LinConstraint(LinExp exp, Relation relation, double bound)
friend std::ostream &operator<<(std::ostream &s, const LinConstraint &c)
LinExp exp
Relation relation
double bound