Commit d7cf3969 authored by Jonas Seidel's avatar Jonas Seidel

Linear Programming Upgrade

parent 28185801
#include "Constraint.h"
Constraint::Constraint(std::string description, relation relation_type, std::vector<double> lhs, double rhs, size_t starting_variable)
: _description(description), _relation(relation_type), _starting_variable(starting_variable), _lhs(lhs), _rhs(rhs){}
Constraint::Constraint(relation relation_type, std::vector<double> lhs, double rhs, size_t starting_variable)
: _relation(relation_type), _starting_variable(starting_variable), _lhs(lhs), _rhs(rhs){}
std::string Constraint::description(){
return this->_description;
}
relation Constraint::is_equality(){
return this->_relation;
}
......@@ -11,7 +18,7 @@ size_t Constraint::lhs_first_variable(){
return this->_starting_variable;
}
size_t Constraint::lhs_last_variable(){
size_t Constraint::lhs_end(){
return this->_starting_variable + this->_lhs.size();
}
......@@ -20,7 +27,7 @@ std::vector<double> Constraint::lhs(){
}
double Constraint::lhs_coefficient(size_t index){
if(index > this->lhs_last_variable() || index < this->lhs_first_variable()) return 0;
if(index >= this->lhs_end() || index < this->lhs_first_variable()) return 0;
return this->_lhs[index - this->lhs_first_variable()];
}
......@@ -29,14 +36,28 @@ double Constraint::rhs(){
}
std::ostream& operator<<(std::ostream& os, Constraint& constraint){
for(size_t index = constraint.lhs_first_variable(); index < constraint.lhs_last_variable()-1; index++){
os << constraint.lhs_coefficient(index) << "*x" << index << "+";
os << constraint.description() << ": ";
bool empty = true;
if(constraint.lhs_end() > 0){
bool add_plus = false;
for(size_t index = constraint.lhs_first_variable(); index < constraint.lhs_end(); index++){
if(std::abs(constraint.lhs_coefficient(index)) > 1e-100){
empty = false;
if(add_plus){
os << " + ";
}
add_plus = true;
os << "(" << constraint.lhs_coefficient(index) << ")" << " x" << index;
}
}
}
if(empty){
os << "0";
}
os << constraint.lhs_coefficient(constraint.lhs_last_variable()-1) << "*x" << constraint.lhs_last_variable()-1;
if(constraint.is_equality()){
os << "=";
os << " = ";
}else{
os << "<=";
os << " <= ";
}
os << constraint.rhs();
return os;
......
......@@ -2,22 +2,27 @@
#define CONSTRAINT_H
#include <cstddef>
#include <string>
#include <vector>
#include <iostream>
#include <cmath>
enum relation : bool {Inequality = false, Equality = true};
class Constraint{
std::string _description;
relation _relation;
size_t _starting_variable;
std::vector<double> _lhs;
double _rhs;
public:
Constraint(std::string description, relation relation_type, std::vector<double> lhs, double rhs, size_t starting_variable = 0);
Constraint(relation relation_type, std::vector<double> lhs, double rhs, size_t starting_variable = 0);
std::string description();
relation is_equality();
size_t lhs_first_variable();
size_t lhs_last_variable();
size_t lhs_end();
std::vector<double> lhs();
double lhs_coefficient(size_t index);
double rhs();
......
......@@ -18,14 +18,23 @@ bool Linear_Program::is_maximum(){
}
double Linear_Program::direction_coefficient(size_t index){
return this->_direction[index];
if(index < this->direction_start() || index > this->direction_end()) return 0;
return this->_direction[index - this->direction_start()];
}
Polyeder& Linear_Program::polyeder(){
return this->_polyeder;
}
std::vector<double> Linear_Program::direction(){
size_t& Linear_Program::direction_start(){
return this->_direction_start;
}
size_t Linear_Program::direction_end(){
return this->direction_start() + this->_direction.size();
}
std::vector<double>& Linear_Program::direction(){
return this->_direction;
}
......@@ -50,9 +59,26 @@ std::ostream& operator<<(std::ostream& os, Linear_Program& linear_program){
os << "Minimize\n";
}
os << "\tobj: ";
for(size_t index = 0; index < linear_program.direction().size()-1; index++){
os << linear_program.direction_coefficient(index) << "*x" << index << "+";
bool empty = true;
if(linear_program.direction().size() > 0){
bool add_plus = false;
for(size_t index = linear_program.direction_start(); index < linear_program.direction_end(); index++){
if(std::abs(linear_program.direction_coefficient(index)) > 1e-100){
empty = false;
if(add_plus){
os << " + ";
}
add_plus = true;
os << "(" << linear_program.direction_coefficient(index) << ")" << " x" << index;
}
}
}
if(empty){
os << "0";
}
os << linear_program.direction_coefficient(linear_program.direction().size()-1) << "*x" << linear_program.direction().size()-1;
os << "\n";
os << linear_program.polyeder() << "\n";
os << "End";
return os;
}
......@@ -3,11 +3,13 @@
#include <cstddef>
#include <vector>
#include <cmath>
#include "Polyeder.h"
class Linear_Program{
bool _maximum;
size_t _direction_start;
std::vector<double> _direction;
Polyeder _polyeder;
public:
......@@ -19,7 +21,10 @@ public:
bool is_maximum();
double direction_coefficient(size_t index);
Polyeder& polyeder();
std::vector<double> direction();
size_t& direction_start();
size_t direction_end();
std::vector<double>& direction();
void operator=(Linear_Program& lp);
void operator=(Linear_Program&& lp);
};
......
......@@ -20,9 +20,9 @@ size_t Polyeder::vs_dim(){
return this->_variables.size();
}
Variable& Polyeder::variable(size_t var){
assert(var < this->vs_dim());
return this->_variables[var];
Variable& Polyeder::variable(size_t index){
assert(index < this->vs_dim());
return this->_variables[index];
}
size_t Polyeder::add_variable(Variable var){
......@@ -30,6 +30,16 @@ size_t Polyeder::add_variable(Variable var){
return this->vs_dim()-1;
}
std::string Polyeder::variable_identifier(size_t index){
std::stringstream name;
if(Variable::VERBOSE_IDENT){
name << this->variable(index).description() << "_x" << index;
}else{
name << "x" << index;
}
return name.str();
}
std::vector<Constraint> Polyeder::constraints(){
return this->_constraints;
}
......@@ -43,7 +53,7 @@ Constraint& Polyeder::constraint(size_t index){
}
void Polyeder::add_constraint(Constraint constraint){
if(this->vs_dim() < constraint.lhs_last_variable()) throw std::range_error("undeclared variables used!");
if(this->vs_dim() < constraint.lhs_end()) throw std::range_error("undeclared variables used!");
this->_constraints.push_back(constraint);
}
......@@ -59,12 +69,24 @@ void Polyeder::operator=(Polyeder&& p){
std::ostream& operator<<(std::ostream& os, Polyeder& polyeder){
os << "Subject to\n";
for(size_t index = 0; index < polyeder.number_of_constraints(); index++) {
os << polyeder.constraint(index) << std::endl;
for(size_t index = 0; index < polyeder.number_of_constraints(); ++index) {
os << polyeder.constraint(index) << "\n";
}
os << "Bounds\n";
for(size_t index = 0; index < polyeder.variables().size(); ++index){
if(polyeder.variable(index).lower_bound().first || polyeder.variable(index).upper_bound().first){
if(polyeder.variable(index).lower_bound().first){
os << polyeder.variable(index).lower_bound().second << " <= ";
}
os << polyeder.variable_identifier(index);
if(polyeder.variable(index).upper_bound().first){
os << " <= " << polyeder.variable(index).upper_bound().second;
}
os << "\n";
}
}
os << "General\n";
for(size_t index = 0; index < polyeder.vs_dim(); index++){
for(size_t index = 0; index < polyeder.vs_dim(); ++index){
if(polyeder.variable(index).is_integral() == Integral){
os << "x" << index << " ";
}
......
......@@ -3,6 +3,7 @@
#include <cstddef>
#include <vector>
#include <sstream>
#include <cassert>
#include "Variable.h"
......@@ -19,13 +20,14 @@ public:
std::vector<Variable> variables();
size_t vs_dim();
Variable& variable(size_t var);
Variable& variable(size_t index);
size_t add_variable(Variable var);
std::string variable_identifier(size_t index);
std::vector<Constraint> constraints();
size_t number_of_constraints();
Constraint& constraint(size_t index);
void add_constraint(Constraint inequality);
void add_constraint(Constraint constraint);
void operator=(const Polyeder& p);
void operator=(Polyeder&& p);
......
#include "Variable.h"
Variable::Variable(integrality integrality, double value) : _integrality(integrality), _value(value) {};
bool Variable::VERBOSE_IDENT = false;
Variable::Variable(integrality integrality, std::pair<bool, double> lower_bound, std::pair<bool, double> upper_bound, double value) : _integrality(integrality), _lower_bound(lower_bound), _upper_bound(upper_bound), _value(value) {}
Variable::Variable(std::string description, integrality integrality, std::pair<bool, double> lower_bound, std::pair<bool, double> upper_bound, double value) : _description(description), _integrality(integrality), _lower_bound(lower_bound), _upper_bound(upper_bound), _value(value) {}
std::string Variable::description(){
return this->_description;
}
integrality Variable::is_integral(){
return this->_integrality;
}
std::pair<bool, double> Variable::lower_bound(){
return this->_lower_bound;
}
std::pair<bool, double> Variable::upper_bound(){
return this->_upper_bound;
}
double& Variable::value(){
return this->_value;
}
......@@ -7,13 +7,22 @@
class Variable{
std::string _description;
integrality _integrality;
std::pair<bool, double> _lower_bound;
std::pair<bool, double> _upper_bound;
double _value;
public:
Variable(integrality integrality, double value = 0);
Variable(integrality integrality, std::pair<bool, double> lower_bound = {false, 0}, std::pair<bool, double> upper_bound = {false, 0}, double value = 0);
Variable(std::string description, integrality integrality, std::pair<bool, double> lower_bound = {false, 0}, std::pair<bool, double> upper_bound = {false, 0}, double value = 0);
std::string description();
integrality is_integral();
std::pair<bool, double> lower_bound();
std::pair<bool, double> upper_bound();
double& value();
static bool VERBOSE_IDENT;
};
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment