Polyeder.cpp 2.88 KB
Newer Older
1
2
3
4
#include "Polyeder.h"

Polyeder::Polyeder(){}

5
6
7
8
Polyeder::Polyeder(const Polyeder& p){
  *this = p;
}

9
10
Polyeder::Polyeder(std::vector<Constraint> constraints, boost::bimap<Variable*, size_t> variables) : _constraints(constraints), _variables(variables) {
    // TODO: check that variables used in constraints are registered in variables?
11
12
}

13
boost::bimap<Variable*, size_t> Polyeder::variables(){
14
15
16
  return this->_variables;
}

17
18
19
20
size_t Polyeder::vs_dim(){
  return this->_variables.size();
}

Jonas Seidel's avatar
Jonas Seidel committed
21
Variable& Polyeder::variable(size_t index){
22
23
24
25
26
27
28
29
30
  auto search_result = this->_variables.right.find(index);
  if(search_result == this->_variables.right.end()) throw std::range_error("no such variable!");
  return *search_result->second;
}

size_t Polyeder::variable(Variable& var){
  auto search_result = this->_variables.left.find(&var);
  if(search_result == this->_variables.left.end()) throw std::range_error("no such variable!");
  return search_result->second;
31
32
}

33
34
35
36
37
std::pair<Variable*, size_t> Polyeder::add_variable(Variable var){
  Variable* new_var = new Variable(var);
  size_t position = this->_variables.size();
  this->_variables.insert({new_var, position});
  return {new_var, position};
38
39
}

Jonas Seidel's avatar
Jonas Seidel committed
40
41
42
std::string Polyeder::variable_identifier(size_t index){
  std::stringstream name;
  if(Variable::VERBOSE_IDENT){
43
    name << this->variable(index).description();
Jonas Seidel's avatar
Jonas Seidel committed
44
45
46
47
48
49
  }else{
    name << "x" << index;
  }
  return name.str();
}

50
51
52
53
54
55
56
57
58
59
60
61
std::vector<Constraint> Polyeder::constraints(){
  return this->_constraints;
}

size_t Polyeder::number_of_constraints(){
  return this->_constraints.size();
}

Constraint& Polyeder::constraint(size_t index){
  return this->_constraints[index];
}

62
void Polyeder::add_constraint(Constraint constraint){
63
  // TODO: check that variables used in constraint are registered in polyeder?
64
  this->_constraints.push_back(constraint);
65
66
}

Jonas Seidel's avatar
Jonas Seidel committed
67
68
69
70
71
72
Polyeder::~Polyeder(){
  // for(auto var : this->_variables){
  //   delete var.left;
  // }
}

73
74
75
76
77
78
79
80
81
82
void Polyeder::operator=(const Polyeder& p){
  this->_constraints = p._constraints;
  this->_variables = p._variables;
}

void Polyeder::operator=(Polyeder&& p){
  this->_constraints = std::move(p._constraints);
  this->_variables = std::move(p._variables);
}

83
84
std::ostream& operator<<(std::ostream& os, Polyeder& polyeder){
  os << "Subject to\n";
85
86
  for(auto constraint : polyeder.constraints()) {
    os << constraint << "\n";
87
88
  }
  os << "Bounds\n";
Jonas Seidel's avatar
Jonas Seidel committed
89
90
91
92
  for(auto var : polyeder._variables){
    if(var.left->lower_bound().first || var.left->upper_bound().first){
      if(var.left->lower_bound().first){
        os << var.left->lower_bound().second << " <= ";
Jonas Seidel's avatar
Jonas Seidel committed
93
      }
Jonas Seidel's avatar
Jonas Seidel committed
94
95
96
      os << var.left->description();
      if(var.left->upper_bound().first){
        os << " <= " << var.left->upper_bound().second;
Jonas Seidel's avatar
Jonas Seidel committed
97
98
99
100
      }
      os << "\n";
    }
  }
101
  os << "General\n";
jonasseidel's avatar
jonasseidel committed
102
103
104
  for(auto var : polyeder._variables){
    if(var.left->is_integral() == Integral){
      os << var.left->description() << " ";
105
106
107
108
    }
  }
  return os;
}