Skip to content

Commit

Permalink
Baron: removed PL as not supported. Added signal handling/propagation.
Browse files Browse the repository at this point in the history
  • Loading branch information
mapgccv committed Oct 22, 2024
1 parent 21bc658 commit 68e2052
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 38 deletions.
20 changes: 15 additions & 5 deletions solvers/baronmp/baronmpbackend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
#include "mp/flat/model_api_base.h"
#include "baronmpbackend.h"


#ifndef WIN32
#include <csignal> // for kill()
#endif


extern "C" {
#include "baronmp-ampls-c-api.h" // Baronmp AMPLS C API
}
Expand All @@ -15,12 +21,18 @@ namespace {


bool InterruptBaronmp(void* prob) {

//return BARONMP_Interrupt((baronmp_prob*)prob);
#ifndef WIN32
kill(mp::BaronmpCommon::pid, SIGINT);
#else
if (!GenerateConsoleCtrlEvent(CTRL_C_EVENT, mp::BaronmpCommon::pid)) {
std::cerr << "GenerateConsoleCtrlEvent failed (" << GetLastError() << ").\n";
return false;
}
#endif
return true;
}

} // namespace {}
}

std::unique_ptr<mp::BasicBackend> CreateBaronmpBackend() {
return std::unique_ptr<mp::BasicBackend>{new mp::BaronmpBackend()};
Expand Down Expand Up @@ -121,8 +133,6 @@ int BaronmpBackend::BarrierIterations() const {

void BaronmpBackend::SetInterrupter(mp::Interrupter *inter) {
inter->SetHandler(InterruptBaronmp, lp());
// TODO Check interrupter
//BARONMP_CCALL( CPXsetterminate (env(), &terminate_flag) );
}

void BaronmpBackend::Solve() {
Expand Down
18 changes: 10 additions & 8 deletions solvers/baronmp/baronmpcommon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace mp {
#else
constexpr const char* EXENAME = "baronin";
char SEP = '/';
volatile pid_t BaronmpCommon::pid=-5;
#endif

int BaronmpCommon::runBaron(const std::string& arg) {
Expand Down Expand Up @@ -102,11 +103,12 @@ int BaronmpCommon::run(const std::vector<std::string>& args) {
ZeroMemory(&pi, sizeof(pi));

// Create the process
if (!CreateProcess(NULL, cmdline.data(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
if (!CreateProcess(NULL, cmdline.data(), NULL,
NULL, TRUE, CREATE_NEW_PROCESS_GROUP, NULL, NULL, &si, &pi)) {
fmt::print(stderr, "CreateProcess failed ({})\n", GetLastError());
return -1;
}

pid = pi.dwProcessId;
// Wait until the process exits
WaitForSingleObject(pi.hProcess, INFINITE);

Expand All @@ -126,7 +128,7 @@ int BaronmpCommon::run(const std::vector<std::string>& args) {
char **environ= *_NSGetEnviron();
#endif
// Unix-like system implementation using fork and execve
pid_t pid = fork();
pid = fork();

if (pid == -1) {
fmt::print(stderr, "fork failed: {}\n", strerror(errno));
Expand All @@ -147,17 +149,17 @@ int BaronmpCommon::run(const std::vector<std::string>& args) {
else {
// Parent process: wait for the child to complete
int status;
if (waitpid(pid, &status, 0) == -1) {
fmt::print(stderr, "waitpid failed: {}\n", strerror(errno));
return -1;
}
do {
pid = waitpid(pid, &status, 0);
} while (pid == -1 && errno == EINTR); // Retry if interrupted by a signal


if (WIFEXITED(status)) {
return WEXITSTATUS(status);
}
else if (WIFSIGNALED(status)) {
fmt::print(stderr, "Process terminated by signal {}\n", WTERMSIG(status));
return -1;
return 0;
}
}
#endif
Expand Down
8 changes: 8 additions & 0 deletions solvers/baronmp/baronmpcommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ class BaronmpCommon :
public Backend2ModelAPIConnector<BaronmpCommonInfo> {
public:

#ifndef WIN32
static volatile pid_t pid;
#else
DWORD pid;
#endif
static constexpr double Infinity() { return INFINITY; }
static constexpr double MinusInfinity() { return -INFINITY; }
const std::string FILENAME_BAR = "amplmodel.bar";
Expand Down Expand Up @@ -402,6 +407,9 @@ class ResFileData {


double ObjectiveValue() const {

if(objective_values.size() == 0)
return 0;
return objective_values[objective_values.size() - 1];
}

Expand Down
54 changes: 31 additions & 23 deletions solvers/baronmp/baronmpmodelapi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,29 @@ namespace mp {
nVarsInteger(indicesInt.size());
}

void addCoefficient(fmt::MemoryWriter &w, double coeff, bool first)
{
if (coeff > 0) {
if (!first) w << " + ";
// Omit '1*' for positive coefficient 1
if (coeff != 1)
w << coeff << "*";
}
else { // Coefficient is negative
// If the coefficient is -1, just print ' - ', otherwise print the coefficient
if (coeff == -1) w << " - ";
else w << coeff << "*";
}
}

void appendLinearTerm(fmt::MemoryWriter &w, double coeff, const char* vname, bool first = false)
{
if ((!first) && (coeff > 0))
w << "+";
if (std::fabs(coeff) != 1.0) w << coeff << "*";
addCoefficient(w, coeff, first);
w << vname;
}
void appendQuadTerm(fmt::MemoryWriter &w, double coeff, const char* v1, const char* v2=nullptr, bool first = false)
{
if ((!first) && (coeff > 0))
w << " +";
if (std::fabs(coeff) != 1.0) w << coeff << "*";
addCoefficient(w, coeff, first);
if(v2)
w << v1 << "*" << v2;
else
Expand Down Expand Up @@ -303,31 +313,30 @@ void BaronmpModelAPI::AddConstraint(const LogConstraint& cc) {
}

void BaronmpModelAPI::AddConstraint(const ExpAConstraint& cc) {
// TODO
fmt::print("Adding ExpA constraint \"{}\"\n", cc.GetName());
// a^v
fmt::MemoryWriter w;
w<< createConName(cc.GetName()) << ": ";
w << fmt::format("{} = {} ^ {};\n", varName(cc.GetResultVar()), cc.GetParameters()[0],varName( cc.GetArguments()[0]));
cons.push_back(w.str());
}



void BaronmpModelAPI::AddConstraint(const LogAConstraint& cc) {
// TODO
fmt::print("Adding LogA constraint \"{}\"\n", cc.GetName());
fmt::MemoryWriter w;
w<< createConName(cc.GetName()) << ": ";
w << fmt::format("{} = log({})/log({});\n", varName(cc.GetResultVar()),
varName( cc.GetArguments()[0]), cc.GetParameters()[0]);
cons.push_back(w.str());
}

void BaronmpModelAPI::AddConstraint(const PowConstraint& cc) {
// TODO
}



void BaronmpModelAPI::AddConstraint(const PLConstraint& plc) {
fmt::print("Adding PL constraint \"{}\"\n", plc.GetName());
// todo
// v ^ a
fmt::MemoryWriter w;
w<< createConName(cc.GetName()) << ": ";
w << fmt::format("{} = {} ^ {};\n", varName(cc.GetResultVar()),varName( cc.GetArguments()[0]), cc.GetParameters()[0]);
cons.push_back(w.str());
}


template <int SENSE> void BaronmpModelAPI::addTopLevel(const NLBaseAssign<SENSE>& c) {
fmt::print("addTopLevel(const NLBaseAssign)\n");
const auto var = GetVariable(c);
fmt::MemoryWriter w;
w << createConName(c.GetName()) << ": ";
Expand All @@ -336,7 +345,6 @@ template <int SENSE> void BaronmpModelAPI::addTopLevel(const NLBaseAssign<SENSE>
const auto& frm = GetExpression(c);
w << frm.ToString(false);
w << ";\n";
fmt::print("{}\n", w.str());
cons.push_back(w.str());
}

Expand Down
2 changes: 0 additions & 2 deletions solvers/baronmp/baronmpmodelapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,6 @@ class BaronmpModelAPI :

/// Some non linear constraints.
/// See constr_std.h for more.
ACCEPT_CONSTRAINT(PLConstraint, Recommended, CG_General)
void AddConstraint(const PLConstraint& cc);
ACCEPT_CONSTRAINT(MaxConstraint, Recommended, CG_General)
void AddConstraint(const MaxConstraint& mc);
ACCEPT_CONSTRAINT(MinConstraint, Recommended, CG_General)
Expand Down

0 comments on commit 68e2052

Please sign in to comment.