Skip to content

Commit

Permalink
use default empty parameters in solve function
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgen-lentz committed Feb 14, 2025
1 parent 3f4c693 commit bc764d7
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 19 deletions.
4 changes: 2 additions & 2 deletions docs/source/class-structure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ The currently defined entities are obtained from the various get methods of the
(see section :ref:`secAMPLClass`). Once a reference to an entity is created, the entity is automatically kept up-to-date
with the corresponding entity in the AMPL interpreter. That is, if a reference to a newly created AMPL variable
is obtained by means of :meth:`.AMPL.getVariable()`, and the model the variable is part of is then solved
by means of :meth:`.AMPL.solve("", "")`, the values of the instances of the variable will automatically be updated.
by means of :meth:`.AMPL.solve()`, the values of the instances of the variable will automatically be updated.
The following code snippet should demonstrate the concept.

.. code-block:: R
Expand All @@ -271,7 +271,7 @@ The following code snippet should demonstrate the concept.
# At this point x$value() evaluates to 0
print(x$value()) # prints 0
ampl$solve("", "")
ampl$solve()
# At this point x$value() evaluates to 10
print(x$value()) # prints 10
Expand Down
12 changes: 6 additions & 6 deletions docs/source/quick-start.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ This is the complete listing of the example. Please note that, for clarity of pr
ampl$readData("models/diet.dat")
# Solve
ampl$solve("", "")
ampl$solve()
# Get objective entity by AMPL name
totalcost <- ampl$getObjective("Total_Cost")
Expand All @@ -43,7 +43,7 @@ This is the complete listing of the example. Please note that, for clarity of pr
cat("Increased costs of beef and ham.\n")
# Resolve and display objective
ampl$solve("", "")
ampl$solve()
cat(sprintf("New objective value: %g\n", totalcost$value()))
# Reassign data - all instances
Expand All @@ -52,7 +52,7 @@ This is the complete listing of the example. Please note that, for clarity of pr
cat("Updated all costs.\n")
# Resolve and display objective
ampl$solve("", "")
ampl$solve()
cat(sprintf("New objective value: %g\n", totalcost$value()))
# Get the values of the variable Buy in a dataframe object
Expand Down Expand Up @@ -123,7 +123,7 @@ To solve the currently loaded problem instance, it is sufficient to issue the co

.. code-block:: R
ampl$solve("", "")
ampl$solve()
Get an AMPL entity in the programming environment (get objective value)
Expand Down Expand Up @@ -175,7 +175,7 @@ values is overloaded, and is in both cases :meth:`Parameter.setValues()`.
cost <- ampl$getParameter("cost")
cost$setValues(data.frame(index=c("BEEF", "HAM"), value=c(5.01, 4.55)))
cat("Increased costs of beef and ham.\n")
ampl$solve("", "")
ampl$solve()
cat(sprintf("New objective value: %g\n", totalcost$value()))
The code above assigns the values 5.01 and 4.55 to the parameter cost for the objects beef and ham respectively.
Expand All @@ -186,7 +186,7 @@ both the index and the value. A collection of values is assigned to each of the
cost$setValues(c(3, 5, 5, 6, 1, 2, 5.01, 4.55))
cat("Updated all costs.\n")
ampl$solve("", "")
ampl$solve()
cat(sprintf("New objective value: %g\n", totalcost$value()))
The statements above produce the following output::
Expand Down
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()

# 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()
# 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()
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()
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()

# 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()
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()
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()
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()
cat(sprintf("QMIP objective value %g\n", ampl$getObjectives()[[1]]$value()))
}
13 changes: 11 additions & 2 deletions src/rampl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,17 @@ bool RAMPL::isRunning() const {
:param string solver: The solver that will be used to solve the problem.
:raises Error: If the underlying interpreter is not running.
*/
void RAMPL::solve(std::string problem, std::string solver) {
_impl.solve(problem, solver);
void RAMPL::solve(Rcpp::Nullable<std::string> problem = R_NilValue, Rcpp::Nullable<std::string> solver = R_NilValue) {
if (problem.isNotNull() && solver.isNotNull())
_impl.solve(problem, solver);
else if (problem.isNotNull())
_impl.solve(problem, "");
else if (solver.isNotNull())
_impl.solve("", solver);
else
_impl.solve("", "")


//return _impl.solve("", ""); // FIXME: does not print to stdout with R IDE on Windows
}

Expand Down

0 comments on commit bc764d7

Please sign in to comment.