@@ -205,8 +205,13 @@ void writePrimalSolution(FILE* file, const HighsLogOptions& log_options,
205205 const HighsLp& lp,
206206 const std::vector<double >& primal_solution,
207207 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)
208211 HighsInt num_nonzero_primal_value = 0 ;
209- assert (lp.col_names_ .size () == static_cast <size_t >(lp.num_col_ ));
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_ ));
210215 if (sparse) {
211216 // Determine the number of nonzero primal solution values
212217 for (HighsInt iCol = 0 ; iCol < lp.num_col_ ; iCol++)
@@ -225,8 +230,12 @@ void writePrimalSolution(FILE* file, const HighsLogOptions& log_options,
225230 if (sparse && !primal_solution[ix]) continue ;
226231 auto valStr = highsDoubleToString (primal_solution[ix],
227232 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" ;
228237 ss.str (std::string ());
229- ss << highsFormatToString (" %-s %s" , lp. col_names_ [ix] .c_str (), valStr.data ());
238+ ss << highsFormatToString (" %-s %s" , name .c_str (), valStr.data ());
230239 if (sparse) ss << highsFormatToString (" %d" , int (ix));
231240 ss << " \n " ;
232241 highsFprintfString (file, log_options, ss.str ());
@@ -273,9 +282,9 @@ void writeModelSolution(FILE* file, const HighsLogOptions& log_options,
273282 for (HighsInt ix = 0 ; ix < lp.num_row_ ; ix++) {
274283 auto valStr = highsDoubleToString (solution.row_value [ix],
275284 kHighsSolutionValueToStringTolerance );
276- // Create a row name
277285 ss.str (std::string ());
278- ss << highsFormatToString (" %-s %s\n " , lp.row_names_ [ix].c_str (), valStr.data ());
286+ ss << highsFormatToString (" %-s %s\n " , lp.row_names_ [ix].c_str (),
287+ valStr.data ());
279288 highsFprintfString (file, log_options, ss.str ());
280289 }
281290 }
@@ -296,7 +305,8 @@ void writeModelSolution(FILE* file, const HighsLogOptions& log_options,
296305 auto valStr = highsDoubleToString (solution.col_dual [ix],
297306 kHighsSolutionValueToStringTolerance );
298307 ss.str (std::string ());
299- ss << highsFormatToString (" %-s %s\n " , lp.col_names_ [ix].c_str (), valStr.data ());
308+ ss << highsFormatToString (" %-s %s\n " , lp.col_names_ [ix].c_str (),
309+ valStr.data ());
300310 highsFprintfString (file, log_options, ss.str ());
301311 }
302312 ss.str (std::string ());
@@ -306,7 +316,8 @@ void writeModelSolution(FILE* file, const HighsLogOptions& log_options,
306316 auto valStr = highsDoubleToString (solution.row_dual [ix],
307317 kHighsSolutionValueToStringTolerance );
308318 ss.str (std::string ());
309- ss << highsFormatToString (" %-s %s\n " , lp.row_names_ [ix].c_str (), valStr.data ());
319+ ss << highsFormatToString (" %-s %s\n " , lp.row_names_ [ix].c_str (),
320+ valStr.data ());
310321 highsFprintfString (file, log_options, ss.str ());
311322 }
312323 }
@@ -359,8 +370,8 @@ HighsStatus normaliseNames(const HighsLogOptions& log_options, HighsLp& lp) {
359370 if (call_status == HighsStatus::kError ) return call_status;
360371 HighsStatus return_status = call_status;
361372 call_status =
362- normaliseNames (log_options, false , lp.num_row_ , lp.row_name_prefix_ ,
363- lp.row_name_suffix_ , lp.row_names_ , lp.row_hash_ );
373+ normaliseNames (log_options, false , lp.num_row_ , lp.row_name_prefix_ ,
374+ lp.row_name_suffix_ , lp.row_names_ , lp.row_hash_ );
364375 if (call_status != HighsStatus::kOk ) return call_status;
365376 return return_status;
366377}
0 commit comments