Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 2114 #2119

Merged
merged 20 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
22 changes: 12 additions & 10 deletions check/Avgas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <cassert>
#include <cstdio> // For printf

#include "lp_data/HConst.h"

const bool dev_run = false;

void Avgas::row(HighsInt row, HighsInt& num_row, HighsInt& num_row_nz,
Expand All @@ -27,7 +29,7 @@ void Avgas::row(HighsInt row, HighsInt& num_row, HighsInt& num_row_nz,
ARstart[num_row] = num_row_nz;
if (row == 0) {
rowLower[num_row] = -1;
rowUpper[num_row] = 1e31;
rowUpper[num_row] = kHighsInf;
HighsInt num_new_nz = 2;
ARindex.resize(num_row_nz + num_new_nz);
ARvalue.resize(num_row_nz + num_new_nz);
Expand All @@ -39,7 +41,7 @@ void Avgas::row(HighsInt row, HighsInt& num_row, HighsInt& num_row_nz,
num_row_nz++;
} else if (row == 1) {
rowLower[num_row] = -1;
rowUpper[num_row] = 1e31;
rowUpper[num_row] = kHighsInf;
HighsInt num_new_nz = 2;
ARindex.resize(num_row_nz + num_new_nz);
ARvalue.resize(num_row_nz + num_new_nz);
Expand All @@ -51,7 +53,7 @@ void Avgas::row(HighsInt row, HighsInt& num_row, HighsInt& num_row_nz,
num_row_nz++;
} else if (row == 2) {
rowLower[num_row] = -1;
rowUpper[num_row] = 1e31;
rowUpper[num_row] = kHighsInf;
HighsInt num_new_nz = 2;
ARindex.resize(num_row_nz + num_new_nz);
ARvalue.resize(num_row_nz + num_new_nz);
Expand All @@ -63,7 +65,7 @@ void Avgas::row(HighsInt row, HighsInt& num_row, HighsInt& num_row_nz,
num_row_nz++;
} else if (row == 3) {
rowLower[num_row] = -1;
rowUpper[num_row] = 1e31;
rowUpper[num_row] = kHighsInf;
HighsInt num_new_nz = 2;
ARindex.resize(num_row_nz + num_new_nz);
ARvalue.resize(num_row_nz + num_new_nz);
Expand All @@ -75,7 +77,7 @@ void Avgas::row(HighsInt row, HighsInt& num_row, HighsInt& num_row_nz,
num_row_nz++;
} else if (row == 4) {
rowLower[num_row] = -2;
rowUpper[num_row] = 1e31;
rowUpper[num_row] = kHighsInf;
HighsInt num_new_nz = 4;
ARindex.resize(num_row_nz + num_new_nz);
ARvalue.resize(num_row_nz + num_new_nz);
Expand All @@ -93,7 +95,7 @@ void Avgas::row(HighsInt row, HighsInt& num_row, HighsInt& num_row_nz,
num_row_nz++;
} else if (row == 5) {
rowLower[num_row] = -2;
rowUpper[num_row] = 1e31;
rowUpper[num_row] = kHighsInf;
HighsInt num_new_nz = 4;
ARindex.resize(num_row_nz + num_new_nz);
ARvalue.resize(num_row_nz + num_new_nz);
Expand All @@ -111,7 +113,7 @@ void Avgas::row(HighsInt row, HighsInt& num_row, HighsInt& num_row_nz,
num_row_nz++;
} else if (row == 6) {
rowLower[num_row] = 0;
rowUpper[num_row] = 1e31;
rowUpper[num_row] = kHighsInf;
HighsInt num_new_nz = 3;
ARindex.resize(num_row_nz + num_new_nz);
ARvalue.resize(num_row_nz + num_new_nz);
Expand All @@ -126,7 +128,7 @@ void Avgas::row(HighsInt row, HighsInt& num_row, HighsInt& num_row_nz,
num_row_nz++;
} else if (row == 7) {
rowLower[num_row] = 0;
rowUpper[num_row] = 1e31;
rowUpper[num_row] = kHighsInf;
HighsInt num_new_nz = 4;
ARindex.resize(num_row_nz + num_new_nz);
ARvalue.resize(num_row_nz + num_new_nz);
Expand All @@ -144,7 +146,7 @@ void Avgas::row(HighsInt row, HighsInt& num_row, HighsInt& num_row_nz,
num_row_nz++;
} else if (row == 8) {
rowLower[num_row] = 0;
rowUpper[num_row] = 1e31;
rowUpper[num_row] = kHighsInf;
HighsInt num_new_nz = 4;
ARindex.resize(num_row_nz + num_new_nz);
ARvalue.resize(num_row_nz + num_new_nz);
Expand All @@ -162,7 +164,7 @@ void Avgas::row(HighsInt row, HighsInt& num_row, HighsInt& num_row_nz,
num_row_nz++;
} else if (row == 9) {
rowLower[num_row] = 0;
rowUpper[num_row] = 1e31;
rowUpper[num_row] = kHighsInf;
HighsInt num_new_nz = 3;
ARindex.resize(num_row_nz + num_new_nz);
ARvalue.resize(num_row_nz + num_new_nz);
Expand Down
144 changes: 144 additions & 0 deletions check/TestLpModification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "catch.hpp"
#include "lp_data/HighsLpUtils.h"
#include "util/HighsRandom.h"
#include "util/HighsTimer.h"
#include "util/HighsUtils.h"

const bool dev_run = false;
Expand Down Expand Up @@ -1954,3 +1955,146 @@ TEST_CASE("zero-matrix-entries", "[highs_data]") {
lp.a_matrix_.value_ = {1, 0, 0, 1};
REQUIRE(highs.passModel(lp) == HighsStatus::kOk);
}

TEST_CASE("row-wise-get-row-time", "[highs_data]") {
Highs h;

HighsTimer timer;
HighsStatus return_status;

HighsInt dim = 5;
const HighsInt max_k = 10;
for (HighsInt k = 0; k < max_k; k++) {
double time = -timer.getWallTime();
for (HighsInt i = 0; i < dim; i++) {
return_status = h.addCol(0.0, -kHighsInf, kHighsInf, 0, nullptr, nullptr);
}
HighsInt index[2] = {0, 0};
double value[2] = {1.0, 2.0};
for (HighsInt i = 1; i < dim; i++) {
index[0] = i;
index[1] = i - 1;
return_status = h.addRow(0.0, kHighsInf, 2, index, value);
}
for (HighsInt i = 1; i < dim; i++) {
index[0] = i;
index[1] = i - 1;
return_status = h.addRow(0.0, kHighsInf, 2, index, value);
}
std::vector<double> lower;
std::vector<double> upper;
HighsInt num_row = 0;
HighsInt num_nz = 0;
std::vector<HighsInt> matrix_start;
std::vector<HighsInt> matrix_index;
std::vector<double> matrix_value;
assert(h.getNumRow() == 2 * dim - 2);
for (HighsInt i = 0; i < dim - 1; i++) {
return_status = h.getRows(i, i, num_row, lower.data(), upper.data(),
num_nz, nullptr, nullptr, nullptr);
REQUIRE(num_row == 1);
REQUIRE(num_nz == 2);
matrix_start.resize(num_row);
matrix_index.resize(num_nz);
matrix_value.resize(num_nz);
return_status = h.getRows(i, i, num_row, lower.data(), upper.data(),
num_nz, matrix_start.data(),
matrix_index.data(), matrix_value.data());
REQUIRE(matrix_start[0] == 0);
REQUIRE(matrix_index[0] == i + 1);
REQUIRE(matrix_index[1] == i);
REQUIRE(matrix_value[0] == 1);
REQUIRE(matrix_value[1] == 2);

std::vector<HighsInt> set = {i, 2 * dim - 3};
return_status =
h.getRows(2, set.data(), num_row, lower.data(), upper.data(), num_nz,
nullptr, nullptr, nullptr);
REQUIRE(num_row == 2);
REQUIRE(num_nz == 4);
matrix_start.resize(num_row);
matrix_index.resize(num_nz);
matrix_value.resize(num_nz);
return_status = h.getRows(2, set.data(), num_row, lower.data(),
upper.data(), num_nz, matrix_start.data(),
matrix_index.data(), matrix_value.data());
REQUIRE(matrix_start[0] == 0);
REQUIRE(matrix_start[1] == 2);
REQUIRE(matrix_index[0] == i + 1);
REQUIRE(matrix_index[1] == i);
REQUIRE(matrix_index[2] == dim - 1);
REQUIRE(matrix_index[3] == dim - 2);
REQUIRE(matrix_value[0] == 1);
REQUIRE(matrix_value[1] == 2);
REQUIRE(matrix_value[2] == 1);
REQUIRE(matrix_value[3] == 2);

std::vector<HighsInt> mask;
mask.assign(2 * (dim - 1), 0);
mask[0] = 1;
mask[dim - 1 + i] = 1;
return_status =
h.getRows(mask.data(), num_row, lower.data(), upper.data(), num_nz,
nullptr, nullptr, nullptr);
REQUIRE(num_row == 2);
REQUIRE(num_nz == 4);
matrix_start.resize(num_row);
matrix_index.resize(num_nz);
matrix_value.resize(num_nz);
return_status = h.getRows(mask.data(), num_row, lower.data(),
upper.data(), num_nz, matrix_start.data(),
matrix_index.data(), matrix_value.data());
REQUIRE(matrix_start[0] == 0);
REQUIRE(matrix_start[1] == 2);
REQUIRE(matrix_index[0] == 1);
REQUIRE(matrix_index[1] == 0);
REQUIRE(matrix_index[2] == i + 1);
REQUIRE(matrix_index[3] == i);
REQUIRE(matrix_value[0] == 1);
REQUIRE(matrix_value[1] == 2);
REQUIRE(matrix_value[2] == 1);
REQUIRE(matrix_value[3] == 2);
}
time += timer.getWallTime();
printf("Loop %2d: dim = %5d; time = %6.4f\n", int(k), int(dim), time);
h.clear();
dim *= 2;
}
}

TEST_CASE("row-wise-get-row-avgas", "[highs_data]") {
Avgas avgas;
const HighsInt avgas_num_col = 8;
const HighsInt avgas_num_row = 10;

Highs h;
std::vector<double> colCost;
std::vector<double> colLower;
std::vector<double> colUpper;
std::vector<HighsInt> Astart;
std::vector<HighsInt> Aindex;
std::vector<double> Avalue;
for (HighsInt col = 0; col < avgas_num_col; col++) {
HighsInt num_col = 0;
HighsInt num_col_nz = 0;
avgas.col(col, num_col, num_col_nz, colCost, colLower, colUpper, Astart,
Aindex, Avalue);
REQUIRE(h.addCol(colCost[0], colLower[0], colUpper[0], 0, nullptr,
nullptr) == HighsStatus::kOk);
}

std::vector<double> rowLower;
std::vector<double> rowUpper;
std::vector<HighsInt> ARstart;
std::vector<HighsInt> ARindex;
std::vector<double> ARvalue;

for (HighsInt row = 0; row < avgas_num_row; row++) {
HighsInt num_row = 0;
HighsInt num_row_nz = 0;
avgas.row(row, num_row, num_row_nz, rowLower, rowUpper, ARstart, ARindex,
ARvalue);
REQUIRE(h.addRow(rowLower[0], rowUpper[0], num_row_nz, ARindex.data(),
ARvalue.data()) == HighsStatus::kOk);
}
}
13 changes: 6 additions & 7 deletions src/Highs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1555,15 +1555,14 @@ class Highs {
void deleteRowsInterface(HighsIndexCollection& index_collection);

void getColsInterface(const HighsIndexCollection& index_collection,
HighsInt& num_col, double* col_cost, double* col_lower,
double* col_upper, HighsInt& num_nz,
HighsInt* col_matrix_start, HighsInt* col_matrix_index,
double* col_matrix_value);
HighsInt& num_col, double* cost, double* lower,
double* upper, HighsInt& num_nz, HighsInt* start,
HighsInt* index, double* value);

void getRowsInterface(const HighsIndexCollection& index_collection,
HighsInt& num_row, double* row_lower, double* row_upper,
HighsInt& num_nz, HighsInt* row_matrix_start,
HighsInt* row_matrix_index, double* row_matrix_value);
HighsInt& num_row, double* lower, double* upper,
HighsInt& num_nz, HighsInt* start, HighsInt* index,
double* value);

void getCoefficientInterface(const HighsInt ext_row, const HighsInt ext_col,
double& value);
Expand Down
Loading
Loading