I receive the following error; the reason for the error A[i][j]== 0
and A[i][j]
usage in two other parts. I used this three times in the code to check whether row [i] and column [j] of A matrix equal to zero. However, since A matrix is so big and contains so many zeros, I defined it as a sparse matrix.
But, how to find/look for row [i] and column [j] in the sparse matrix A?
So far, I have tried A.coeff[i][j] == 0
but it did not worked.
Type 'std::tuple_element<0, std::tuple<Eigen::SparseMatrix<double, 0>, std::vector<double>, std::vector<double>>>::type' (aka 'Eigen::SparseMatrix<double, 0>') does not provide a subscript operator
#include <iostream>
#include <fstream>
#include <ostream>
#include <tuple>
#include <vector>
#include <array>
#include "gurobi_c++.h"
#include </home/user/snap/eigen-3.4.0/Eigen/Eigen>
#include </home/user/snap/eigen-3.4.0/Eigen/Sparse>
using TripletVector = std::vector<Eigen::Triplet<double>>;
using SparseMatrix = Eigen::SparseMatrix<double>;
using Vector = std::vector<double>;
std::tuple<SparseMatrix, Vector, Vector> read_Abc(GRBModel& model) {
// number of variables, number of constraints, number of nonzeros in A
std::size_t n = model.get(GRB_IntAttr_NumVars);
std::size_t m = model.get(GRB_IntAttr_NumConstrs);
std::size_t num_nnz = model.get(GRB_IntAttr_NumNZs);
// allocate space
TripletVector triplet(num_nnz);
Vector b(m);
Vector c(n);
// Read the objective coefficients
auto obj_expr = model.getObjective().getLinExpr();
for (std::size_t j = 0; j < n; ++j) {
c[j] = obj_expr.getCoeff(j);
}
// Read the coefficient matrix A and the rhs vector b
std::size_t k = 0;
for (std::size_t i = 0; i < m; ++i) {
auto con = model.getConstr(i);
auto row = model.getRow(con);
for (std::size_t r = 0; r < row.size(); ++r) {
auto col_idx = row.getVar(r).index();
auto coeff = row.getCoeff(r);
triplet[k++] = Eigen::Triplet<double>(i, col_idx, coeff);
}
// read the constraint rhs
b[i] = con.get(GRB_DoubleAttr_RHS);
}
// Construct the sparse matrix.
SparseMatrix A(m, n);
A.setFromTriplets(triplet.begin(), triplet.end());
return {A, b, c};
}
int main(int argc, char* argv[]) {
std::ofstream myfile; //Defining file name
myfile.open("ScalingPrint.txt"); //Opening the file
GRBEnv env = GRBEnv();
GRBModel model = GRBModel(env,"/home/user/CLionProjects/Scaling/example.lp" );
auto [A, b, c] = read_Abc(model);
std::cout<<"Writing to File Started!"<<std::endl;
//Declarations and Initializing of Variables
int m = 3; //Declaring Size of matrix A as above
int n = 4;
double row_max[m]; //Declaring One dimensional scaling matrices for rows like int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
double row_multi[m];
double col_max[m]; //Declaring One dimensional scaling matrices for columns
double col_multi[m];
double max = 0; //Maximum value in the row
int cnt = 0; //Counter
bool exitLoop = false; //Boolean for zero row control
for (int i = 0; i < m; i++) {
row_max[i] = 0.0; //Creating [m][1] zero matrices
row_multi[i] = 0.0; //Creating [n][1] zeros matrices
for (int j = 0; j < n; j++) {
if (A[i][j] == 0.0) { //Finding if a row contains at least one nonzero element
cnt = cnt + 1;
if (cnt == n) //Since we are only checking rows if cnt==n then there is a row that is full of zeros
{
exitLoop = true;
break; //Break the for loop if all rows are zero
}
}
if (!exitLoop) { //Scaling Procedure Started for Matrices whose rows have at least one non-zero element
if (abs(A[i][j]) > max) { //Absolute value of the element in the row
max = A[i][j];
row_max[i] = max; //Adding the biggest abs value to row_max (1D matrix)
}
}
}
//Calculation of specific row scaling factor
row_multi[i] = 1.0 / row_max[i]; //1.0 Written for fractional Division
}
return 0;
}
A.coeff[i][j] == 0
is a syntax error, because coeff
is the name of a member function.
To read an element you need to call coeff
: A.coeff(i, j)
.