Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.12.0)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 11)
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They just released the preview for C++ 23 at the end of last year. C++ 14 is already quite old, why do we need to change it to C++11?

set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

Expand Down
Empty file added lib/core/types.cpp
Empty file.
207 changes: 134 additions & 73 deletions lib/utils/reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,79 +7,101 @@ namespace utils{

Reader::Reader(){}

//this function is probably now useless but still good practise!
void Reader::readFirstProblem(const std::string problems_filepath){
void Reader::readNextProblem(std::fstream &problems_filestream){
//clear anything still sored in class members from previous read
problem_matrix_.clear();
lower_bounds_.clear();
upper_bounds_.clear();

//initialise filestream and open file
std::fstream newfile;
newfile.open(problems_filepath, std::ios::in);
//check file is open
if(!newfile.is_open()){
if(!problems_filestream.is_open()){
std::cout << "ERROR: Unable to open file" << std::endl;
}

//initialise strings for storing elements read from file
std::string temp_string;
std::string num_variables_string;
std::string num_inequality_rows_string;
std::string num_equality_rows_string;

// get number of variables and number of inequalities we are reading in
std::getline(newfile, num_variables_string);
const core::int_t num_variables = static_cast<core::int_t>(atoi(num_variables_string.c_str())) + 1;
// get number of variables we are reading in, taking into
// account whether the line is a separation line or not
// (it is only not a separation line in the first problem.)
std::getline(problems_filestream, temp_string);
if (temp_string.find('~') != std::string::npos) {
std::getline(problems_filestream, num_variables_string);
} else {
num_variables_string = temp_string;
}
const int num_variables = static_cast<int>(atoi(num_variables_string.c_str()));
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

atoi returns int anyways, no need to typecast


std::getline(newfile, num_inequality_rows_string);
const core::int_t num_inequality_rows = static_cast<core::int_t>(atoi(num_inequality_rows_string.c_str()));
// Get number of inequalities.
std::getline(problems_filestream, num_inequality_rows_string);
const int num_inequality_rows = static_cast<int>(atoi(num_inequality_rows_string.c_str()));
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typecasting


//initialise vector for using as temporary holding for matrix row
std::vector<core::int_t> matrix_row;
// Define vectors to read in constraints.
std::vector<int> problem_row; // A line in the input file
int constant_term;
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this do? naming isn't clear? Also a bit misleading since constant implies unchanging but this variable is reassigned a few times?

std::vector<int> problem_matrix_row; // Formatted constraint without the
// constant term.

//read inequality in rows, add vectors to problem matrix
for(size_t i = 0; i < num_inequality_rows; ++i){
std::getline(newfile, temp_string);
matrix_row = convertStringToVector(temp_string);
std::getline(problems_filestream, temp_string);
problem_row = getProblemRowAsIntVector(temp_string);

constant_term = problem_row.at(0)*(-1);
problem_matrix_row = spliceVector(problem_row, 1, num_variables);

//check vector size
if(matrix_row.size() != num_variables){
std::cout << "ERROR: length of matrix row is: " << matrix_row.size() << " but length of " << num_inequality_rows << " was expected" << std::endl;
if(problem_matrix_row.size() != num_variables){
std::cout << "ERROR: length of matrix row is: " << problem_row.size() << " but length of " << num_variables << " was expected" << std::endl;
}
problem_matrix_.push_back(matrix_row);
matrix_row.clear();

problem_matrix_.push_back(problem_matrix_row);
upper_bounds_.push_back(kMaxInt);
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switching to core::kIntInfinity. Works better algebraically and theoretically

lower_bounds_.push_back(constant_term);

problem_row.clear();
problem_matrix_row.clear();
}

//read in equality rows and add to problem matrix
std::string num_equality_rows_string;
std::getline(newfile, num_equality_rows_string);

core::int_t num_equality_rows = static_cast<core::int_t>(atoi(num_equality_rows_string.c_str()));
std::getline(problems_filestream, num_equality_rows_string);
int num_equality_rows = static_cast<int>(atoi(num_equality_rows_string.c_str()));
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typecasting


for(size_t i = 0; i < num_equality_rows; ++i){
//get and typecast row vector
std::getline(newfile, temp_string);
matrix_row = convertStringToVector(temp_string);
std::getline(problems_filestream, temp_string);
problem_row = getProblemRowAsIntVector(temp_string);

constant_term = problem_row.at(0)*(-1);
problem_matrix_row = spliceVector(problem_row, 1, num_variables);

//check vector size
if(matrix_row.size() != num_variables){
std::cout << "ERROR: Expexted row of length " << num_variables << " but row has length " << matrix_row.size() << std::endl;
if(problem_matrix_row.size() != num_variables){
std::cout << "ERROR: length of matrix row is: " << problem_row.size() << " but length of " << num_variables << " was expected" << std::endl;
}
problem_matrix_.push_back(matrix_row);
matrix_row.clear();
}
newfile.close();

//make bound vectors
for(size_t i = 0; i < num_inequality_rows; ++i){
upper_bounds_.push_back(kMaxInt);
lower_bounds_.push_back(0);
}
problem_matrix_.push_back(problem_matrix_row);
upper_bounds_.push_back(constant_term);
lower_bounds_.push_back(constant_term);

for(size_t i = num_inequality_rows; i < num_equality_rows+num_inequality_rows; ++i){
upper_bounds_.push_back(0);
lower_bounds_.push_back(0);
problem_row.clear();
problem_matrix_row.clear();
}

// Read the tilde line so that next problem starts
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

multiline comments can be started with /* and ended a few lines later with */

// at the number of variables line. If the last problem
// ends with a tilde line would work and we would not need the
// tilde check at the start:
// std::getline(problems_filestream, temp_string);
}

void Reader::readProblem(const std::string problems_filepath, const core::int_t problem_number){
void Reader::readProblem(const std::string problems_filepath, const int problem_number){
//clear anything still sored in class members from previous read
problem_matrix_.clear();
lower_bounds_.clear();
upper_bounds_.clear();

//initialise filestream and open file
std::fstream newfile;
Expand All @@ -89,7 +111,7 @@ void Reader::readProblem(const std::string problems_filepath, const core::int_t
std::cout << "ERROR: Unable to open file" << std::endl;
}
//find location in file we want to look at (nth problem)
core::int_t current_problem_location = 0;
int current_problem_location = 0;
std::string tempstring;
std::string tilde = "~";
while(current_problem_location != problem_number-1){
Expand All @@ -106,62 +128,67 @@ void Reader::readProblem(const std::string problems_filepath, const core::int_t

// get number of variables and number of inequalities we are reading in
std::getline(newfile, num_variables_string);
const core::int_t num_variables = static_cast<core::int_t>(atoi(num_variables_string.c_str())) + 1;
const int num_variables = static_cast<int>(atoi(num_variables_string.c_str()));
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typecasting


std::getline(newfile, num_inequality_rows_string);
const core::int_t num_inequality_rows = static_cast<core::int_t>(atoi(num_inequality_rows_string.c_str()));
const int num_inequality_rows = static_cast<int>(atoi(num_inequality_rows_string.c_str()));
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typecasting


//initialise vector for using as temporary holding for matrix row
std::vector<core::int_t> matrix_row;
std::vector<int> problem_row;
int constant_term;
std::vector<int> problem_matrix_row;

//read inequality in rows, add vectors to problem matrix
for(size_t i = 0; i < num_inequality_rows; ++i){
std::getline(newfile, temp_string);
matrix_row = convertStringToVector(temp_string);
problem_row = getProblemRowAsIntVector(temp_string);

constant_term = problem_row.at(0)*(-1);
problem_matrix_row = spliceVector(problem_row, 1, num_variables);

//check vector size
if(matrix_row.size() != num_variables){
std::cout << "ERROR: length of matrix row is: " << matrix_row.size() << " but length of " << num_inequality_rows << " was expected" << std::endl;
if(problem_matrix_row.size() != num_variables){
std::cout << "ERROR: length of matrix row is: " << problem_row.size() << " but length of " << num_variables << " was expected" << std::endl;
}
problem_matrix_.push_back(matrix_row);
matrix_row.clear();

problem_matrix_.push_back(problem_matrix_row);
upper_bounds_.push_back(kMaxInt);
lower_bounds_.push_back(constant_term);

problem_row.clear();
problem_matrix_row.clear();
}

//read in equality rows and add to problem matrix
std::string num_equality_rows_string;
std::getline(newfile, num_equality_rows_string);

core::int_t num_equality_rows = static_cast<core::int_t>(atoi(num_equality_rows_string.c_str()));
int num_equality_rows = static_cast<int>(atoi(num_equality_rows_string.c_str()));
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typecasting


for(size_t i = 0; i < num_equality_rows; ++i){
//get and typecast row vector
std::getline(newfile, temp_string);
matrix_row = convertStringToVector(temp_string);
problem_row = getProblemRowAsIntVector(temp_string);

constant_term = problem_row.at(0)*(-1);
problem_matrix_row = spliceVector(problem_row, 1, num_variables);

//check vector size
if(matrix_row.size() != num_variables){
std::cout << "ERROR: Expexted row of length " << num_variables << " but row has length " << matrix_row.size() << std::endl;
if(problem_matrix_row.size() != num_variables){
std::cout << "ERROR: length of matrix row is: " << problem_row.size() << " but length of " << num_variables << " was expected" << std::endl;
}

problem_matrix_.push_back(matrix_row);
matrix_row.clear();
}
problem_matrix_.push_back(problem_matrix_row);
upper_bounds_.push_back(constant_term);
lower_bounds_.push_back(constant_term);

//make bound vectors
for(size_t i = 0; i < num_inequality_rows; ++i){
lower_bounds_.push_back(0);
upper_bounds_.push_back(kMaxInt);
problem_row.clear();
problem_matrix_row.clear();
}

for(size_t i = num_inequality_rows; i < num_inequality_rows+num_equality_rows; ++i){
lower_bounds_.push_back(0);
upper_bounds_.push_back(0);
}

newfile.close();
newfile.close();
}


std::vector<core::int_t> Reader::convertStringToVector(const std::string vector_string){
std::vector<int> Reader::convertStringToVector(const std::string vector_string){

std::string tempstring;
std::vector<std::string> stringvec;
Expand All @@ -179,20 +206,54 @@ std::vector<core::int_t> Reader::convertStringToVector(const std::string vector_
}

//initialise variables
std::vector<core::int_t> rowvector;
core::int_t temp_int;
std::vector<int> rowvector;
int temp_int;
std::string space = " ";

//if string in vector is not empty or a space, convert to int and add to return vector
for(size_t i = 0; i < stringvec.size(); ++i){
tempstring = stringvec.at(i);
if(tempstring.compare(space) != 0 && !tempstring.empty()){
temp_int = static_cast<core::int_t>(atoi(tempstring.c_str()));
temp_int = static_cast<int>(atoi(tempstring.c_str()));
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typecasting

rowvector.push_back(temp_int);
}
}
return rowvector;
}

std::vector<int> Reader::getProblemRowAsIntVector(const std::string problem_row_string){
std::string tempstring;
std::vector<int> row_vector;

//convert single string to vector of strings with space
//character as delimiter
for(size_t i = 0; i < problem_row_string.size(); ++i){
char position_char = problem_row_string.at(i);

if(position_char != ' '){
tempstring.push_back(position_char);
} else {
row_vector.push_back(static_cast<int>(atoi(tempstring.c_str())));
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typecasting

tempstring.clear();
}

if(i == problem_row_string.length()-1){
row_vector.push_back(static_cast<int>(atoi(tempstring.c_str())));
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typecasting

}
}

return row_vector;
}

std::vector<int> Reader::spliceVector(std::vector<int> to_splice, const int range_start, const int range_end) {
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need this? Not initially clear to me?

std::vector<int> spliced_vector;

for (int i = range_start; i < range_end+1; i++) {
spliced_vector.push_back(to_splice.at(i));
}

return spliced_vector;
}

}//namespace utils

38 changes: 23 additions & 15 deletions lib/utils/reader.hpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
#include <string>
#include <vector>
#include <cstdint>
#include "../core/types.hpp"

namespace utils{

class Reader
{
public:
Reader();
/**
* @brief reads the first problem in the problem file and writes formatted contents
* to class members problem_matrix_ and cost_vector_.
*
* @param problems_filepath path to problem text file
*/
void readFirstProblem(const std::string problems_filepath);

/**
* @brief reads the nth problem in the problem file and writes formatted contents
Expand All @@ -24,17 +16,26 @@ namespace utils{
* @param problems_filepath path to problem text file
* @param problem_number number of problem to read in
*/
void readProblem(const std::string problems_filepath, const core::int_t problem_number);
void readProblem(const std::string problems_filepath, const int problem_number);

std::vector<std::vector<core::int_t>> problem_matrix_;
/**
* @brief reads the next problem in the problem file and writes formatted contents
* to class members problem_matrix_ given an open filestream of test problems.
*
* @param problems_filestream the open filestream
*/
void readNextProblem(std::fstream &problems_filestream);

const core::int_t kMaxInt = 32765;
std::vector<std::vector<int>> problem_matrix_;

const core::int_t kMinInt = -32765;
const int kMaxInt = 32765;
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can remove these


std::vector<core::int_t> upper_bounds_;
const int kMinInt = -32765;

std::vector<core::int_t> lower_bounds_;
std::vector<int> upper_bounds_;

std::vector<int> lower_bounds_;



private:
Expand All @@ -46,7 +47,14 @@ namespace utils{
* @param vector_string input string to be formatted
* @return std::vector<int> formatted vector of ints produced from string input
*/
std::vector<core::int_t> convertStringToVector(const std::string vector_string);
std::vector<int> convertStringToVector(const std::string vector_string);

std::vector<int> getProblemRowAsIntVector(const std::string problem_row_string);
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

documentation/docstrings?


std::vector<int> spliceVector(std::vector<int> to_splice, const int range_start, const int range_end);

};



}//namespace utils
6 changes: 3 additions & 3 deletions problems/primal_problems.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
3
5
1 -1 -10 0 0
1 20 1 0 100
0 1 0 1 1
1 -1 -10 0 0 0
0 1 20 1 0 100
0 0 1 0 1 1
~~~~~
Loading