@@ -82,29 +82,50 @@ void PDLPSolver::PreprocessLp(const HighsLp& original_lp, HighsLp& processed_lp)
8282 }
8383
8484 // 5. Formulate the new constraints matrix (A') and RHS (b')
85- // We build it in COO format first for easier construction, then convert to CSC.
86- std::vector<std::tuple<int , int , double >> coo_entries;
87-
88- // Copy original matrix entries, transforming rows as needed
89- for (int iCol = 0 ; iCol < nCols_orig; ++iCol) {
90- for (int iEl = original_lp.a_matrix_ .start_ [iCol]; iEl < original_lp.a_matrix_ .start_ [iCol + 1 ]; ++iEl) {
91- int iRow = original_lp.a_matrix_ .index_ [iEl];
92- double value = original_lp.a_matrix_ .value_ [iEl];
93-
94- if (constraint_types_[iRow] == LEQ) {
95- coo_entries.emplace_back (iRow, iCol, -value);
96- } else {
97- coo_entries.emplace_back (iRow, iCol, value);
98- }
99- }
85+ //
86+ // Take a copy of the original LP's constraint matrix since we
87+ // need it rowwise and it's const
88+ HighsSparseMatrix original_matrix = original_lp.a_matrix_ ;
89+ original_matrix.ensureRowwise ();
90+ // Set up the processed constraint matrix as an empty row-wise
91+ // matrix that can have nCols_orig columns
92+ HighsSparseMatrix& processed_matrix = processed_lp.a_matrix_ ;
93+ processed_matrix.clear ();
94+ processed_matrix.num_col_ = nCols_orig;
95+ processed_matrix.format_ = MatrixFormat::kRowwise ;
96+ HighsSparseMatrix row;
97+ const double negative_one = -1.0 ;
98+ const double * negative_one_p = &negative_one;
99+ for (int i = 0 ; i < nRows_orig; ++i) {
100+ // Get the row from the original constraint matrix
101+ original_matrix.getRow (i, row);
102+ // Scale the row by -1 if it's a LEQ
103+ if (constraint_types_[i] == LEQ)
104+ row.scaleRows (negative_one_p);
105+ // Add the row to the processed constraint matrix
106+ processed_matrix.addRows (row);
100107 }
108+ // Convert the processed constraint matrix to column-wise orientation
109+ processed_matrix.ensureColwise ();
101110
102111 // Add slack variable entries (-1) for BOUND and FREE constraints
103112 current_slack_col = nCols_orig;
113+ // Set up a negated identity column as a column-wise matrix with
114+ // nRows_orig rows and a single column containing -1 in row 0 (for
115+ // now)
116+ HighsSparseMatrix col;
117+ assert (col.isColwise ());
118+ col.num_col_ = 1 ;
119+ col.num_row_ = nRows_orig;
120+ col.start_ .push_back (1 );
121+ col.index_ .push_back (0 );
122+ col.value_ .push_back (-1 );
104123 for (int iRow = 0 ; iRow < nRows_orig; ++iRow) {
105124 if (constraint_types_[iRow] == BOUND || constraint_types_[iRow] == FREE) {
106- coo_entries.emplace_back (iRow, current_slack_col, -1.0 );
107- current_slack_col++;
125+ // Put the -1 in row iRow, and add the column to the matrix
126+ col.index_ [0 ] = iRow;
127+ processed_matrix.addCols (col);
128+ current_slack_col++;
108129 }
109130 }
110131
@@ -133,37 +154,9 @@ void PDLPSolver::PreprocessLp(const HighsLp& original_lp, HighsLp& processed_lp)
133154 }
134155 }
135156
136- /*
137157 // 7. Convert COO matrix to CSC for the processed_lp
138- std::sort(coo_entries.begin(), coo_entries.end(),
139- [](const auto& a, const auto& b) {
140- if (std::get<1>(a) != std::get<1>(b)) return std::get<1>(a) < std::get<1>(b);
141- return std::get<0>(a) < std::get<0>(b);
142- });
143-
144- processed_lp.a_matrix_.start_.assign(processed_lp.num_col_ + 1, 0);
145- processed_lp.a_matrix_.index_.resize(coo_entries.size());
146- processed_lp.a_matrix_.value_.resize(coo_entries.size());
147-
148- int current_col_idx = 0;
149- processed_lp.a_matrix_.start_[0] = 0;
150- for (size_t i = 0; i < coo_entries.size(); ++i) {
151- int row = std::get<0>(coo_entries[i]);
152- int col = std::get<1>(coo_entries[i]);
153- double val = std::get<2>(coo_entries[i]);
154-
155- processed_lp.a_matrix_.index_[i] = row;
156- processed_lp.a_matrix_.value_[i] = val;
157-
158- while (current_col_idx < col) {
159- current_col_idx++;
160- processed_lp.a_matrix_.start_[current_col_idx] = i;
161- }
162- }
163- for (int col = current_col_idx; col < processed_lp.num_col_; ++col) {
164- processed_lp.a_matrix_.start_[col + 1] = coo_entries.size();
165- }
166- */
158+ //
159+ // Already achieved by construction
167160 logger_.info (" Preprocessing complete. New dimensions: " + std::to_string (processed_lp.num_row_ ) + " rows, " + std::to_string (processed_lp.num_col_ ) + " cols." );
168161}
169162
0 commit comments