Consulting solve_result_num using AMPL C++ API for baron

64 views
Skip to first unread message

C_Zetina

unread,
Aug 21, 2018, 3:50:16 PM8/21/18
to AMPL Modeling Language
Hello,

I am currently using teh AMPL C++ API to solve a problem in Baron. I would like to know if there is a way to extract the solve_result_num from within my code and assign it to a variable in my own code. 

I need this information to distinguish what instances Baron has solved to optimality andwhen it has timed out. Below is the code in C++ which forms a small part of the larger code I am implementing.  

*******
extern "C" double AMPL_solve(char *instance, char *temp, double obval) {
  double retval;
  try{

  ampl::AMPL ampl;
  ampl.setOption("solver", "baron");
  ampl.setOption("baron_options","optfile BaronOpt.txt outlev 1");
  ampl.setOption("omit_zero_rows","1");
  
  // Read the model and data files.
  std::string modelDirectory = "./PONDP";
  std::string dataDirectory = "./PONDP/NetDesign/";
  ampl.read(modelDirectory + "/PO_UNDP.mod");
  ampl.readData(dataDirectory + instance);
  ampl.readData(dataDirectory + "GE");

  //Adjusting the value of the gravit_exp
  // Solve
  ampl.solve();

          I ASSUME I WOULD QUERY FOR SOLVE_RESULT_NUM HERE.


  // Get objective entity by AMPL name
  ampl::Objective profit = ampl.getObjective("Profit");
  // Print it
  std::cout << instance << " objective function is: " << profit.value() << std::endl;

  // Get the values of the variable in a dataframe object
  ampl::Variable x = ampl.getVariable("x");
  ampl::DataFrame df = x.getValues();
  
  retval=profit.value()-obval;
  // Print them
  ofstream myfile;
  myfile.open (temp);
  myfile << df.toString();
  myfile.close();
  std::cout << df.toString() << std::endl;
  return retval;
  }
  catch (const std::exception &e)
  {
    std::cout << e.what() << "\n";
return -1;
  }
  
}
******

Thank you in advance for your time.

Regards
Carlos

AMPL Google Group

unread,
Aug 21, 2018, 10:09:41 PM8/21/18
to Ampl Modeling Language
Dear Carlos,

You can access solve_result_num with ampl.getValue("solve_result_num").dbl() as follows:
int solve_result_num = (int)ampl.getValue("solve_result_num").dbl();

Best regards,
Filipe

--
Filipe Brandão
am...@googlegroups.com
{#HS:646486887-20501#}
--
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ampl+uns...@googlegroups.com.
To post to this group, send email to am...@googlegroups.com.
Visit this group at https://groups.google.com/group/ampl.
For more options, visit https://groups.google.com/d/optout.



Reply all
Reply to author
Forward
0 new messages