Skip to content

Commit

Permalink
Merge branch 'master' into c-api
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgen-lentz committed Oct 12, 2024
2 parents 901595b + f0234e6 commit 7b4e72b
Show file tree
Hide file tree
Showing 19 changed files with 67 additions and 37 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/build-and-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ jobs:
build:
runs-on: macos-13
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: r-lib/actions/setup-r@v2
- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
python-version: "3.9"
- run: |
Expand All @@ -18,7 +18,7 @@ jobs:
cp rAMPL_*.tar.gz upload/release/
cp rAMPL_*.tar.gz upload/release/rAMPL.tar.gz
cp -r docs/build/html upload/doc/R
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
name: drop
path: upload/
Expand All @@ -36,12 +36,12 @@ jobs:
bundle: https://portal.ampl.com/dl/amplce/ampl.mswin64.zip
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: r-lib/actions/setup-r@v2
- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
python-version: "3.9"
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4.1.7
with:
name: drop
- name: Copy package
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: rAMPL
Title: AMPL API for R
Version: 2.0.12.0-20230210
Version: 2.0.13.0-20241012
Author: Filipe Brandao
Maintainer: Filipe Brandão <[email protected]>
Description: AMPL API for R.
Expand Down
9 changes: 8 additions & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ stages:
pool: {vmImage: 'macos-latest'}
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '3.9.x'
addToPath: true
- bash: |
set -ex
brew install r
python updatelib.py
bash build.sh
mkdir -p upload/{macos,release,doc}
Expand Down Expand Up @@ -54,7 +58,10 @@ stages:
inputs: {artifactName: 'drop', downloadPath: '.'}
- bash: cp drop/release/* .
displayName: Copy package
- bash: bash dependencies.sh
- bash: |
set -ex
brew install r
bash dependencies.sh
displayName: Install Rcpp and testthat
- bash: bash install.sh
displayName: Install package
Expand Down
3 changes: 2 additions & 1 deletion docs/requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pydata-sphinx-theme==0.7.1
setuptools
ampl-sphinx-theme
ampl-sphinx-theme @ git+https://github.com/ampl/ampl.github.io.git#subdirectory=ampl-sphinx-theme
sphinx==5.0.0
myst_parser>=0.17.0
sphinx_design==0.3.0
3 changes: 2 additions & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pydata-sphinx-theme==0.7.1
setuptools
ampl-sphinx-theme @ git+https://github.com/ampl/ampl.github.io.git#subdirectory=ampl-sphinx-theme
sphinx==4.2.0
sphinx==5.0.0
myst_parser>=0.17.0
sphinx_design==0.3.0
6 changes: 3 additions & 3 deletions docs/source/class-structure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Commands and options
~~~~~~~~~~~~~~~~~~~~

Some AMPL commands are encapsulated by functions in the :class:`AMPL` class for ease of access.
These comprise :meth:`.AMPL.solve()` and others.
These comprise :meth:`.AMPL.solve("", "")` and others.
To access and set options in AMPL, the functions :meth:`.AMPL.getOption()` and :meth:`.AMPL.setOption()` are provided.
These functions provide an easier programmatic access to the AMPL options.
In general, when an encapsulation is available for an AMPL command, the programmatic access to it is to be preferred to calling the same command using
Expand Down 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
4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# -- General configuration -----------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
needs_sphinx = "4.2.0"
needs_sphinx = "5.0.0"

# Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
Expand Down Expand Up @@ -49,7 +49,7 @@
# The short X.Y version.
version = "2.0"
# The full version, including alpha/beta/rc tags.
release = "2.0.11.0-20220823"
release = "2.0.13.0-20241012"


# The language for content autogenerated by Sphinx. Refer to documentation
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 docs/source/reference/ramplcpp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ AMPL

Returns ``TRUE`` if the underlying engine is running.

.. method:: AMPL.solve()
.. method:: AMPL.solve("", "")

Solve the current model.

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("", "gurobi")
ampl$solve("", "")

# Print out the result
cat(sprintf("Objective: %f\n", ampl$getObjective("Total_Cost")$value()))
Expand Down
10 changes: 9 additions & 1 deletion 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("", "gurobi")
ampl$solve("", "")
# Calibrate the efficient frontier range
minret <- portfolioReturn$value()
values <- averageReturn$getValues()
Expand All @@ -60,14 +60,22 @@ efficientfrontier <- function(solver=NULL, modelDirectory=NULL) {
ampl$eval("let stockopall:={};let stockrun:=stockall;")
# Relax integrality
ampl$setOption("relax_integrality", TRUE)
<<<<<<< HEAD
ampl$solve("", "gurobi")
=======
ampl$solve("", "")
>>>>>>> master
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)
<<<<<<< HEAD
ampl$solve("", "gurobi")
=======
ampl$solve("", "")
>>>>>>> master
cat(sprintf("QMIP result = %g\n", variance$value()))
# Store data of corrent frontier point
returns <- c(returns, maxret - (i - 1) * stepsize)
Expand Down
10 changes: 9 additions & 1 deletion 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("", "gurobi")
ampl$solve("", "")

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

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

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

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

# Get the values of the variable Buy in a dataframe object
Expand Down
6 changes: 5 additions & 1 deletion 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("", "gurobi")
ampl$solve("", "")
cat(sprintf("QP objective value ", ampl$getObjectives()[[1]]$value()))

lowcutoff <- 0.04
Expand All @@ -62,6 +62,10 @@ trackingmodel <- function(solver=NULL, modelDirectory=NULL) {
# Get back to the integer problem
ampl$setOption("relax_integrality", FALSE)
# Solve the (integer) problem
<<<<<<< HEAD
ampl$solve("", "gurobi")
=======
ampl$solve("", "")
>>>>>>> master
cat(sprintf("QMIP objective value %g\n", ampl$getObjectives()[[1]]$value()))
}
9 changes: 5 additions & 4 deletions src/rampl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,16 +310,17 @@ bool RAMPL::isRunning() const {
return _impl.isRunning();
}

/*.. method:: AMPL.solve()
/*.. method:: AMPL.solve(problem, solver)
Solve the current model.
:param string problem: The problem that will be solved.
: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 = "") {
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
//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(std::string problem, std::string solver);
void solve(std::string problem="", std::string solver="");

Rcpp::DataFrame getData(Rcpp::List statements) const;
SEXP getValue(std::string scalarExpression) const;
Expand Down
4 changes: 2 additions & 2 deletions src/rset_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ std::string RSetInstance::toString() const {
:return: The number of tuples in the set.
*/
int RSetInstance::size() { //const {
int RSetInstance::size() {
return _impl.size();
}

Expand All @@ -51,7 +51,7 @@ int RSetInstance::size() { //const {
:return: The list of members of the set.
*/
std::vector<Rcpp::List> RSetInstance::members() { //const {
std::vector<Rcpp::List> RSetInstance::members() {
std::vector<Rcpp::List> members;
ampl::SetInstance::MemberRange range = _impl.members();
for(ampl::SetInstance::MemberRange::iterator it = range.begin(); it != range.end(); it++){
Expand Down
4 changes: 2 additions & 2 deletions src/rset_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class RSetInstance {
std::string name() const;
std::string toString() const;
//int arity() const; FIXME: ?
int size();// const;
std::vector<Rcpp::List> members();// const;
int size();
std::vector<Rcpp::List> members();
bool contains(Rcpp::List tuple) const;
Rcpp::DataFrame getValues() const;
void setValues(SEXP values);
Expand Down
2 changes: 1 addition & 1 deletion src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Rcpp::XPtr<T> getObj(const std::string &type, SEXP s) {
std::string rtypename = "Rcpp_" + type;
Rcpp::S4 s4obj(s);
if ( !s4obj.is( rtypename.c_str() ) ) {
Rf_error( (std::string("object is not of the type ")+type).c_str() );
Rf_error("%s", (std::string("object is not of the type ")+type).c_str());
}
Rcpp::Environment env(s4obj);
Rcpp::XPtr<T> xptr(env.get(".pointer"));
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test_set.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ test_that("test set instance", {
s2 <- ampl$getSet("s2")$get(c())

expect_equal(s$name(), "s")
expect_equal(s$toString(), "set s = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};")
expect_equal(s$toString(), "set s;")

# expect_equal(s$arity(), 1) # FIXME: missing in C++?
expect_equal(s$size(), 10)
Expand Down

0 comments on commit 7b4e72b

Please sign in to comment.