MCPcopy Create free account
hub / github.com/ERGO-Code/HiGHS / writePrimalSolution

Function writePrimalSolution

highs/lp_data/HighsModelUtils.cpp:204–244  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

202}
203
204void writePrimalSolution(FILE* file, const HighsLogOptions& log_options,
205 const HighsLp& lp,
206 const std::vector<double>& primal_solution,
207 const bool sparse) {
208 // Use when writing out the solution file (when names can be assumed
209 // to exist) and the improving solution in the MIP solver (when
210 // names cannot be assumed to exist)
211 HighsInt num_nonzero_primal_value = 0;
212 const bool have_col_names = lp.col_names_.size() > 0;
213 if (have_col_names)
214 assert(lp.col_names_.size() == static_cast<size_t>(lp.num_col_));
215 if (sparse) {
216 // Determine the number of nonzero primal solution values
217 for (HighsInt iCol = 0; iCol < lp.num_col_; iCol++)
218 if (primal_solution[iCol]) num_nonzero_primal_value++;
219 }
220 // Indicate the number of column values to be written out, depending
221 // on whether format is sparse: either lp.num_col_ if not sparse, or
222 // the negation of the number of nonzero values, if sparse
223
224 std::stringstream ss;
225 ss.str(std::string());
226 HighsInt num_col_field = sparse ? -num_nonzero_primal_value : lp.num_col_;
227 ss << highsFormatToString("# Columns %d\n", int(num_col_field));
228 highsFprintfString(file, log_options, ss.str());
229 for (HighsInt ix = 0; ix < lp.num_col_; ix++) {
230 if (sparse && !primal_solution[ix]) continue;
231 auto valStr = highsDoubleToString(primal_solution[ix],
232 kHighsSolutionValueToStringTolerance);
233 // Don't invent names locallty: if none exist, then indicate this
234 // - so that the (sparse) solution line format remains "name value
235 // (index)"
236 const std::string name = have_col_names ? lp.col_names_[ix] : "NoName";
237 ss.str(std::string());
238 ss << highsFormatToString("%-s %s", name.c_str(), valStr.data());
239 if (sparse) ss << highsFormatToString(" %d", int(ix));
240 ss << "\n";
241 highsFprintfString(file, log_options, ss.str());
242 }
243 fflush(file);
244}
245
246void writeModelSolution(FILE* file, const HighsLogOptions& log_options,
247 const HighsModel& model, const HighsSolution& solution,

Callers 2

saveReportMipSolutionMethod · 0.85
writeModelSolutionFunction · 0.85

Calls 7

highsFormatToStringFunction · 0.85
highsFprintfStringFunction · 0.85
highsDoubleToStringFunction · 0.85
strMethod · 0.80
c_strMethod · 0.80
dataMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected