Coder Social home page Coder Social logo

ampl / ampls-api Goto Github PK

View Code? Open in Web Editor NEW
4.0 4.0 3.0 8.67 MB

AMPL Solver APIs

Home Page: https://ampls.readthedocs.io

License: BSD 3-Clause "New" or "Revised" License

CMake 1.26% C++ 78.21% C# 1.29% Python 13.63% Shell 0.31% Jupyter Notebook 4.19% SWIG 0.75% AMPL 0.34% Batchfile 0.01% C 0.01%

ampls-api's Introduction

ampls-api's People

Contributors

dependabot[bot] avatar fdabrandao avatar gomfy avatar mapgccv avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ampls-api's Issues

Update AMPL Ipopt executable

I would like to use a new version of Ipopt with AMPL, precisely the newest one, 3.14.14. However, the file ampl.linux64.tgz gives me a folder with an Ipopt of version 3.12.3.

(base) user@pop-os:~/ampl/$ ./ipopt --version
Ipopt 3.12.13 (Linux x86_64), ASL(20221115)

(base) user@pop-os:~/$ ipopt --version # outside of folder ampl
Ipopt 3.14.14 (x86_64-pc-linux-gnu), ASL(20190605)

How can I force AMPL to use the new Ipopt version?

Thanks and regards.

C++ CMake

Hello.

I am trying to use the AMPL C++ API by importing it through CMake. However, I am struggling to make it work with C++20. Below follows my FindAMPL.cmake file:

if (AMPL_LIBRARY)
    SET(AMPL_FIND_QUIETLY TRUE)
endif (AMPL_LIBRARY)

IF ( UNIX )
    # target not already created
    if (NOT TARGET ampl)

        # ampl dir
        find_path(AMPL_DIR NAMES "ampl" PATHS "/opt/ampl/" "/opt/AMPL/" "~/ampl/" "~/AMPL/")

        # edge case
        IF( NOT AMPL_FIND_QUIETLY AND NOT AMPL_DIR)
            message ( "AMPL_DIR not found ")
        ENDIF()

        # amplapi
        set(AMPL_API_DIR "${AMPL_DIR}/amplapi")
        set(AMPL_API_INCLUDE "${AMPL_API_DIR}/include")

        # runpath
        set(CMAKE_INSTALL_RPATH "${AMPL_API_DIR}/lib")
        set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)

        # lib
        set(AMPL_LIBRARY_NAME "ampl")
        add_library(ampl STATIC IMPORTED)
        find_library(AMPL_LIBRARY ${AMPL_LIBRARY_NAME} PATHS ${CMAKE_INSTALL_RPATH})

        # edge case
        if(NOT(AMPL_LIBRARY))
            message(FATAL_ERROR "AMPL library \"${AMPL_LIBRARY_NAME}\" could not be found")
        endif()

        set_target_properties(ampl PROPERTIES IMPORTED_LOCATION ${AMPL_LIBRARY} INTERFACE_INCLUDE_DIRECTORIES ${AMPL_API_DIR}/include)
      target_compile_options(ampl PRIVATE ${CXX11_FLAG})

    endif ()

    include(FindPackageHandleStandardArgs)

    find_package_handle_standard_args(AMPL  DEFAULT_MSG AMPL_LIBRARY)

ENDIF()

And my CMakeLists.txt is the one below:

cmake_minimum_required(VERSION 3.16)
project(AMPLCMake)

##### SETs
set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/support)
set (CMAKE_CXX_STANDARD 20)

##### Additional packages 

find_package (AMPL)

##### Include Dirs 

include_directories(SYSTEM includes "${PROJECT_BINARY_DIR}")

file(GLOB_RECURSE SOURCES "src/*.cpp")

add_executable(main ${SOURCES})

target_link_libraries(main ampl)

After running the make command, I obtain the following messages:

[ 50%] Building CXX object CMakeFiles/main.dir/src/main.cpp.o
In file included from /opt/ampl/amplapi/include/ampl/amplexception.h:8,
                 from /opt/ampl/amplapi/include/ampl/ep/errorinfo_ep.h:6,
                 from /opt/ampl/amplapi/include/ampl/variant.h:7,
                 from /opt/ampl/amplapi/include/ampl/ep/arg.h:4,
                 from /opt/ampl/amplapi/include/ampl/dataframe.h:4,
                 from /opt/ampl/amplapi/include/ampl/ampl.h:4,
                 from ~/ampl_test/src/main.cpp:3:
/opt/ampl/amplapi/include/ampl/format.h: 
    In instantiation of ‘void fmt::internal::MemoryBuffer<T, SIZE, Allocator>::grow(std::size_t) [with T = wchar_t; long unsigned int SIZE = 500; Allocator = std::allocator<wchar_t>; std::size_t = long unsigned int]’:
/opt/ampl/amplapi/include/ampl/format.h:799:6:
    required from here
/opt/ampl/amplapi/include/ampl/format.h:802:30:
    error: no matching function for call to ‘fmt::internal::MemoryBuffer<wchar_t, 500, std::allocator<wchar_t> >::allocate(std::size_t&, std::nullptr_t)’
  802 |   T *new_ptr = this->allocate(new_capacity, FMT_NULL);
      |                ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/11/string:41,
                 from /usr/include/c++/11/bits/locale_classes.h:40,
                 from /usr/include/c++/11/bits/ios_base.h:41,
                 from /usr/include/c++/11/ios:42,
                 from /usr/include/c++/11/ostream:38,
                 from /usr/include/c++/11/iostream:39,
                 from ~/ampl_test/src/main.cpp:1:
/usr/include/c++/11/bits/allocator.h:179:7:
    note: candidate: ‘constexpr _Tp* std::allocator< <template-parameter-1-1> >::allocate(std::size_t) [with _Tp = wchar_t; std::size_t = long unsigned int]’
  179 |       allocate(size_t __n)
      |       ^~~~~~~~
/usr/include/c++/11/bits/allocator.h:179:7:
    note:   candidate expects 1 argument, 2 provided
In file included from /opt/ampl/amplapi/include/ampl/amplexception.h:8,
                 from /opt/ampl/amplapi/include/ampl/ep/errorinfo_ep.h:6,
                 from /opt/ampl/amplapi/include/ampl/variant.h:7,
                 from /opt/ampl/amplapi/include/ampl/ep/arg.h:4,
                 from /opt/ampl/amplapi/include/ampl/dataframe.h:4,
                 from /opt/ampl/amplapi/include/ampl/ampl.h:4,
                 from ~/ampl_test/src/main.cpp:3:
/opt/ampl/amplapi/include/ampl/format.h:
    In instantiation of ‘void fmt::internal::MemoryBuffer<T, SIZE, Allocator>::grow(std::size_t) [with T = char; long unsigned int SIZE = 500; Allocator = std::allocator<char>; std::size_t = long unsigned int]’:
/opt/ampl/amplapi/include/ampl/format.h:799:6:
    required from here
/opt/ampl/amplapi/include/ampl/format.h:802:30:
    error: no matching function for call to ‘fmt::internal::MemoryBuffer<char, 500, std::allocator<char> >::allocate(std::size_t&, std::nullptr_t)’
  802 |   T *new_ptr = this->allocate(new_capacity, FMT_NULL);
      |                ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/11/string:41,
                 from /usr/include/c++/11/bits/locale_classes.h:40,
                 from /usr/include/c++/11/bits/ios_base.h:41,
                 from /usr/include/c++/11/ios:42,
                 from /usr/include/c++/11/ostream:38,
                 from /usr/include/c++/11/iostream:39,
                 from ~/ampl_test/src/main.cpp:1:
/usr/include/c++/11/bits/allocator.h:179:7:
    note: candidate: ‘constexpr _Tp* std::allocator< <template-parameter-1-1> >::allocate(std::size_t) [with _Tp = char; std::size_t = long unsigned int]’
  179 |       allocate(size_t __n)
      |       ^~~~~~~~
/usr/include/c++/11/bits/allocator.h:179:7:
    note:   candidate expects 1 argument, 2 provided
make[2]: *** [CMakeFiles/main.dir/build.make:76: CMakeFiles/main.dir/src/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

However, if I disable the command set (CMAKE_CXX_STANDARD 20). The compilation and execution works just fine.

I would like to know if it is possible to compile a C++20 project with AMPL. The link of the above codes is here.

Thanks and BR.

Update 1: The problem doesn't seems to be with CMake actually, it seems that AMPL C++ API doesn't support C++20. I tried the following commands:

cd /opt/ampl/amplapi/examples/cpp
g++ -std=c++11 -Wall -o firstexample firstexample.cc -I../../include -L../../lib -lampl
g++ -std=c++14 -Wall -o firstexample firstexample.cc -I../../include -L../../lib -lampl
g++ -std=c++17 -Wall -o firstexample firstexample.cc -I../../include -L../../lib -lampl

And all of them work just fine, but, when I set -std=c++20 the above errors appear.

Community License RuntimeError

Hello. With some models I get a Runtime Error when attempting to .exportGurobiModel() an amplpy.AMPL object into the ampls-api for use with a community licensed environment. However, when I run the same example in a fully licensed environment it works fine.

It is also worth mentioning that it is not all models that have this behavior, running the same examples will always error in the community licensed environment but running some other models will always work just fine.

Additionally, if I stay within amplpy and use amplpy.AMPL.solve() without exporting to gurobi model it works just fine...

Not sure what's going on here. I have attached a python script that will always error in my community licensed environment and will never error in my full licensed environment.

recreateAmplpyGurobiRuntimeError.py.txt

Installation error

Hi,

I would like to use AMPL through C++. I tried to install the C++ library by naively following the instructions on this page https://ampls.readthedocs.io/en/latest/cpp/quickstart.html But the make command fails:

/ampls-api$ mkdir build
/ampls-api$ cd build
/ampls-api/build$ cmake ..
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning at CMakeLists.txt:65 (message):
  Specify the solvers to build with -DBUILD_AMPLS=solvername1,..,solvernamen
  or -DBUILD=all


-- Could NOT find SWIG (missing: SWIG_EXECUTABLE SWIG_DIR) (Required is at least version "3.0")
CMake Warning at CMakeLists.txt:105 (message):
  SWIG not found, Python and C# bindings will not be generated


-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
ampls examples linked with 
-- Configuring done
-- Generating done
-- Build files have been written to: /ampls-api/build
/ampls-api/build$ make
Scanning dependencies of target ampls
[ 33%] Building CXX object cpp/ampls/CMakeFiles/ampls.dir/src/csvReader.cpp.o
[ 66%] Building CXX object cpp/ampls/CMakeFiles/ampls.dir/src/ampls.cpp.o
In file included from /ampls-api/cpp/ampls/src/ampls.cpp:1:
/ampls-api/cpp/ampls/include/ampls/ampls.h:493:14: error: extra qualification ‘ampls::impl::Records::’ on member ‘operator=’ [-fpermissive]
  493 |     Records& Records::operator=(const Records& other)
      |              ^~~~~~~
/ampls-api/cpp/ampls/include/ampls/ampls.h:503:14: error: extra qualification ‘ampls::impl::Records::’ on member ‘operator=’ [-fpermissive]
  503 |     Records& Records::operator=(Records&& other)
      |              ^~~~~~~
In file included from /ampls-api/cpp/ampls/src/ampls.cpp:1:
/ampls-api/cpp/ampls/include/ampls/ampls.h: In member function ‘int ampls::AMPLModel::numConsAdded()’:
/ampls-api/cpp/ampls/include/ampls/ampls.h:1004:38: error: ‘std::vector<ampls::Constraint> ampls::impl::Records::cons_’ is private within this context
 1004 |     return static_cast<int>(records_.cons_.size()+1);
      |                                      ^~~~~
In file included from /ampls-api/cpp/ampls/src/ampls.cpp:1:
/ampls-api/cpp/ampls/include/ampls/ampls.h:476:29: note: declared private here
  476 |     std::vector<Constraint> cons_;
      |                             ^~~~~
In file included from /ampls-api/cpp/ampls/src/ampls.cpp:1:
/ampls-api/cpp/ampls/include/ampls/ampls.h: In member function ‘int ampls::AMPLModel::numVarsAdded()’:
/ampls-api/cpp/ampls/include/ampls/ampls.h:1007:38: error: ‘std::vector<ampls::Variable> ampls::impl::Records::vars_’ is private within this context
 1007 |     return static_cast<int>(records_.vars_.size()+1);
      |                                      ^~~~~
In file included from /ampls-api/cpp/ampls/src/ampls.cpp:1:
/ampls-api/cpp/ampls/include/ampls/ampls.h:475:27: note: declared private here
  475 |     std::vector<Variable> vars_;
      |                           ^~~~~
In file included from /ampls-api/cpp/ampls/src/ampls.cpp:1:
/ampls-api/cpp/ampls/include/ampls/ampls.h: In constructor ‘ampls::AMPLModel::AMPLModel(ampls::AMPLModel&&)’:
/ampls-api/cpp/ampls/include/ampls/ampls.h:1028:14: error: ‘ampls::AMPLModel* ampls::impl::Records::parent_’ is private within this context
 1028 |     records_.parent_ = this;
      |              ^~~~~~~
In file included from /ampls-api/cpp/ampls/src/ampls.cpp:1:
/ampls-api/cpp/ampls/include/ampls/ampls.h:474:16: note: declared private here
  474 |     AMPLModel* parent_;
      |                ^~~~~~~
In file included from /ampls-api/cpp/ampls/src/ampls.cpp:1:
/ampls-api/cpp/ampls/include/ampls/ampls.h: In member function ‘ampls::AMPLModel& ampls::AMPLModel::operator=(ampls::AMPLModel&&)’:
/ampls-api/cpp/ampls/include/ampls/ampls.h:1050:16: error: ‘ampls::AMPLModel* ampls::impl::Records::parent_’ is private within this context
 1050 |       records_.parent_ = this;
      |                ^~~~~~~
In file included from /ampls-api/cpp/ampls/src/ampls.cpp:1:
/ampls-api/cpp/ampls/include/ampls/ampls.h:474:16: note: declared private here
  474 |     AMPLModel* parent_;
      |                ^~~~~~~
/ampls-api/cpp/ampls/src/ampls.cpp: In member function ‘virtual std::string ampls::Constraint::toAMPLString(const std::map<int, std::__cxx11::basic_string<char> >&, const std::map<int, std::__cxx11::basic_string<char> >&, const ampls::impl::Records&) const’:
/ampls-api/cpp/ampls/src/ampls.cpp:177:22: error: ‘std::vector<ampls::Variable> ampls::impl::Records::vars_’ is private within this context
  177 |       name = records.vars_[index - varMap.size()].name();
      |                      ^~~~~
In file included from /ampls-api/cpp/ampls/src/ampls.cpp:1:
/ampls-api/cpp/ampls/include/ampls/ampls.h:475:27: note: declared private here
  475 |     std::vector<Variable> vars_;
      |                           ^~~~~
/ampls-api/cpp/ampls/src/ampls.cpp: In member function ‘virtual std::string ampls::Variable::toAMPLString(const std::map<int, std::__cxx11::basic_string<char> >&, const std::map<int, std::__cxx11::basic_string<char> >&, const ampls::impl::Records&) const’:
/ampls-api/cpp/ampls/src/ampls.cpp:216:22: error: ‘std::vector<ampls::Constraint> ampls::impl::Records::cons_’ is private within this context
  216 |       name = records.cons_[index - consMap.size() + 1].name();
      |                      ^~~~~
In file included from /ampls-api/cpp/ampls/src/ampls.cpp:1:
/ampls-api/cpp/ampls/include/ampls/ampls.h:476:29: note: declared private here
  476 |     std::vector<Constraint> cons_;
      |                             ^~~~~
make[2]: *** [cpp/ampls/CMakeFiles/ampls.dir/build.make:76 : cpp/ampls/CMakeFiles/ampls.dir/src/ampls.cpp.o] Erreur 1
make[1]: *** [CMakeFiles/Makefile2:94 : cpp/ampls/CMakeFiles/ampls.dir/all] Erreur 2
make: *** [Makefile:84 : all] Erreur 2

Operating system: Ubuntu 20.04.5 LTS

amplpy_gurobi callback variable names and amplpy variable names not matching.

Hello, this is less of a bug and more of a quality of life change...

When you create an amplpy.AMPL model and get get its amplpy.Variable variable instances, its var.name() field returns something of the like "x[(1, 1)]". However, when you export the model with amplpy.exportGurobiModel() and use .getSolutionDict() within a callback, it's keys will be "x[(1,1)]". It would be very convenient if these could be unified.

I have attached a python file to demonstrate what I mean.

amplGurobiVarNameMismatch.py.txt

I could do a string replace replacing ", " with ",", but then if my index includes a string that contains ', ', then it would break so its not a perfect solution. I know it may be difficult to change this if those strings are generated by gurobi itself, but if it isn't, then it would be great if these "names" could be unified so you could use them together more seemlessly

P.S. Also the file RuntimeError's for some reason and I'm not sure why. I didn't bother looking into that because it wasn't relevant to the point. I might have done something wrong to make that happen. If you happen to notice what was wrong, it would be good to know. Luckily, the file still demonstrates my point because it prints what it needs to before erroring

GurobiCallback.getWhere() fails

Hello,

I can't seem to ever get anything out of self.getWhere() within a GurobiCallback. Below is a script that shoes it throws an exception every time. Below is a script that shows that .getWhere only seems to fail.

Below is a script that demonstrates the behavior.
amplgurobiGetWhereFails.py.txt

Infeasible Model -> Feasible Model leads to a Segmentation fault (core dumped)

Hello. If you do the following in a single python execution, you get a Segmentation fault
NOTE: I have ampl's presolve set to false because otherwise results in a different issue I plan to submit to amplpy's git. Additionally, this is a fully licensed environment with both an ampl license and a gurobi license set with the appropriate environment variables.

  1. Create Infeasible Model with amplpy
  2. Export infeasible model to amplpy_gurobi with .exportGurobiModel()
  3. "solve" the infeasible gurobi model (and correctly get told it is infeasible)
  4. (Optional) import the solution back to amplpy with .importSolution() (this part isn't necessary to make the bug happen)
  5. Create a feasible model with amplpy
  6. Export the feasible model to amplpy_gurobi with .exportGurobiModel() -> Segmentation Fault

Here is an example python file that replicates this behavior.

There is a similar bug around when you flip the order and do an feasible model first but I have submitted that as a separate issue because it results in a RuntimeError instead of a segmentation fault so it might be a different situation.

amplGurobiInfeasibleIntoFeasibleSegFault.py.txt

Python libraries getSolutionDict

Currently the swig wrappers are built with the -builtin switch, that does not generate python wrapper classes.
This makes it impossible to use %pythonappend to convert the dictionaries returned by the functions getSolutionDict to real python dictionaries.
It should be possible - and desirable - to do so by using a typemap.

Feasible Model -> Infeasible Model leads to a RunTimeError

Hello. If you do the following in a single python execution, you get a RunTimeError: Unknown exception
NOTE: I have ampl's presolve set to false because otherwise results in a different issue I plan to submit to amplpy's git. Additionally, this is a fully licensed environment with both an ampl license and a gurobi license set with the appropriate environment variables.

  1. Create Feasible Model with amplpy
  2. Export feasible model to amplpy_gurobi with .exportGurobiModel()
  3. solve the feasible gurobi model
    4.(Optional) import the solution back to amplpy with .importSolution() (this part isn't neccesary to make the bug happen)
  4. Create an infeasible model with amplpy
  5. Export the infeasible model to amplpy_gurobi with .exportGurobiModel() -> RuntimeError: Unknown Exception

I wonder if this has something to do with releasing the gurobi license, but that is just speculation based on the fact that the console will print out ampl license information and then say "No gurobi license for this machine." right before erroring (so the first, feasible model is able to find the gurobi license, but the second, infeasible is not). More detail is in the file below

Here is an example python file that replicates this behavior.

There is a similar bug around when you flip the order and do an infeasible model first but I will submit that as a separate issue because it results in a segmentation fault instead of a runtime error so it might be a different situation.

amplGurobiFeasibleIntoInfeasibleRuntimeError.py.txt

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.