Skip to content

Commit

Permalink
add solve
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgen-lentz committed Sep 29, 2024
1 parent 7bdb417 commit 4a00223
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion examples/dietmodel.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ dietmodel <- function(solver=NULL, modelDirectory=NULL) {
ampl$setData(df, 2, "")

# Solve the model
ampl$solve()
ampl$solve("", "gurobi")

# Print out the result
cat(sprintf("Objective: %f\n", ampl$getObjective("Total_Cost")$value()))
Expand Down
6 changes: 3 additions & 3 deletions examples/efficientfrontier.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ efficientfrontier <- function(solver=NULL, modelDirectory=NULL) {
# Relax the integrality
ampl$setOption("relax_integrality", TRUE)
# Solve the problem
ampl$solve()
ampl$solve("", "gurobi")
# Calibrate the efficient frontier range
minret <- portfolioReturn$value()
values <- averageReturn$getValues()
Expand All @@ -60,14 +60,14 @@ efficientfrontier <- function(solver=NULL, modelDirectory=NULL) {
ampl$eval("let stockopall:={};let stockrun:=stockall;")
# Relax integrality
ampl$setOption("relax_integrality", TRUE)
ampl$solve()
ampl$solve("", "gurobi")
cat(sprintf("QP result = %g\n", variance$value()))
# Adjust included stocks
ampl$eval("let stockrun:={i in stockrun:weights[i]>0};")
ampl$eval("let stockopall:={i in stockrun:weights[i]>0.5};")
# Set integrality back
ampl$setOption("relax_integrality", FALSE)
ampl$solve()
ampl$solve("", "gurobi")
cat(sprintf("QMIP result = %g\n", variance$value()))
# Store data of corrent frontier point
returns <- c(returns, maxret - (i - 1) * stepsize)
Expand Down
6 changes: 3 additions & 3 deletions examples/firstexample.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ firstexample <- function(solver=NULL, modelDirectory=NULL) {
ampl$readData(paste(modelDirectory, "/diet/diet.dat", sep=""))

# Solve
ampl$solve()
ampl$solve("", "gurobi")

# Get objective entity by AMPL name
totalcost <- ampl$getObjective("Total_Cost")
Expand All @@ -34,7 +34,7 @@ firstexample <- function(solver=NULL, modelDirectory=NULL) {
cat(sprintf("Increased costs of beef and ham.\n"))

# Resolve and display objective
ampl$solve()
ampl$solve("", "gurobi")
cat(sprintf("New objective value: %f\n", totalcost$value()))

# Reassign data - all instances
Expand All @@ -43,7 +43,7 @@ firstexample <- function(solver=NULL, modelDirectory=NULL) {
cat(sprintf("Updated all costs.\n"))

# Resolve and display objective
ampl$solve()
ampl$solve("", "gurobi")
cat(sprintf("New objective value: %f\n", totalcost$value()))

# Get the values of the variable Buy in a dataframe object
Expand Down
4 changes: 2 additions & 2 deletions examples/trackingmodel.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ trackingmodel <- function(solver=NULL, modelDirectory=NULL) {
# Relax the integrality
ampl$setOption("relax_integrality", TRUE)
# Solve the problem
ampl$solve()
ampl$solve("", "gurobi")
cat(sprintf("QP objective value ", ampl$getObjectives()[[1]]$value()))

lowcutoff <- 0.04
Expand All @@ -62,6 +62,6 @@ trackingmodel <- function(solver=NULL, modelDirectory=NULL) {
# Get back to the integer problem
ampl$setOption("relax_integrality", FALSE)
# Solve the (integer) problem
ampl$solve()
ampl$solve("", "gurobi")
cat(sprintf("QMIP objective value %g\n", ampl$getObjectives()[[1]]$value()))
}
5 changes: 3 additions & 2 deletions src/rampl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,9 @@ bool RAMPL::isRunning() const {
:raises Error: If the underlying interpreter is not running.
*/
void RAMPL::solve() {
_impl.eval("solve;");
void RAMPL::solve(std::string problem = "", std::string solver = "") {
_impl.solve(problem, solver);
//_impl.eval("solve;");
//return _impl.solve(); // FIXME: does not print to stdout with R IDE on Windows
}

Expand Down
2 changes: 1 addition & 1 deletion src/rampl.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class RAMPL {
void reset();
void close();
bool isRunning() const;
void solve();
void solve(std::string problem, std::string solver);

Rcpp::DataFrame getData(Rcpp::List statements) const;
SEXP getValue(std::string scalarExpression) const;
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test_ampl.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ test_that("test AMPL", {
expect_equal(ampl$getSets()$S$name(), "S")
expect_equal(ampl$getParameters()$l$name(), "l")

ampl$solve()
ampl$solve("", "gurobi")
expect_equal(ampl$getObjective("obj")$value(), 5)

out <- ""
Expand Down

0 comments on commit 4a00223

Please sign in to comment.