Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding simple example using numpy linear algebra #143

Merged
merged 1 commit into from
Nov 7, 2017

Conversation

pstjohn
Copy link
Contributor

@pstjohn pstjohn commented Nov 1, 2017

Is this helpful? I think its nice to see that optlang symbolics play nicely with numpy datatypes.
Is there a better way to create a vector of symbols?

@phantomas1234
Copy link
Contributor

Sympy itself has linear algebra functions but it is indeed cool to see that numpy works just as well! Should we make this part of the docs? I know that you requested better docs in the past and I agree that we should improve the optlang docs in general.

@pstjohn
Copy link
Contributor Author

pstjohn commented Nov 2, 2017

It could certainly just form a note in the documentation. The sympy vectors would also be neat to see how you could use those to construct constraints.

@KristianJensen
Copy link
Contributor

Yes this is a really nice feature that I've also used a couple of times. If you fix the flake8 complaint(s) then I'll happily merge this. I think examples like this are a great way to showcase what the package can do

@KristianJensen KristianJensen changed the base branch from master to devel November 7, 2017 14:53
@KristianJensen KristianJensen merged commit 9ece974 into opencobra:devel Nov 7, 2017
@pstjohn
Copy link
Contributor Author

pstjohn commented Nov 7, 2017

If you fix the flake8 complaint(s)

Sorry, definitely meant to do that but ended up forgetting...
Thanks doing that!

phantomas1234 pushed a commit that referenced this pull request Aug 9, 2018
* fix: bug when changing variable names

Only update the _variables_to_constriants_mapping dict when new_name !=
old_name

* feat: allow infeasible primals to be retrieved

Only _round_primals_to_bounds if solution is optimal

* feat: add a tolerance option to scipy_interface

Default is set to 1e-9

* fix: support all numeric types

E.g. numpy and sympy

* chore: exception types

* fix: set problem attribute on objectives in new models

* fix: updated in Model __init__ breaks gurobi

* Get linear coefficients from solver (#72)

* feat: add get_linear_coefficients method

Abstract non-implemented method

* feat: implement glpk get_linear_coefficients method

* fix: typo

* feat: implement get_linear_coefficients in cplex

* feat: implement get_linear_coefficients in gurobi

* feat: implement get_linear_coefficients in scipy

* test: add tests for get_linear_coefficients

* test: more tests for get_linear_coefficients

* fix: flake8

* fix: missing arguments in test

* fix: make scipy consistent with other interfaces

the get_linear_coefficients and set_linear_coefficients methods now
raise an exception if the objective/constraint doesn’t belong to a model

* fix: objective.value should return None if not in model

* fix: get correct primal values from glpk MIPs

* test: add test for obj value without model

* fix: allow older versions of cplex

Some cplex versions don’t have all the statuses that optlang tries to
import, which would result in a crash.

* test: non-existing cplex status for testing

* test: fix tests to reflect constraint refactoring

For range constraints, rhs is now equal to lb

* fix: update gurobi after changing bounds

* fix: update gurobi after making changes

* fix: gurobi objective val is None without a model

* chore: simplify set_linear_coefficients in gurobi

* chore: remove unused _internal_var attribute

* fix: constraint construction when adding constraints

* fix: reset solution before optimizing if non-optimal

This is due to a bug where gurobi can get stuck

* Miplib tests (#6)

* checking if __del__ is needed

* testing against miplib

* using problems from miplib2003

* needs to implement variable comparison for problems that converge

* more problems
runtime: 10 min per problem

* fixed milp test value (same as in devel)

* delete problem from memory only if it exists (__del__ method)

* add a .keep file to keep the miplib2003 directory on tests/data

* miplib test with cplex - some problems also fail...

* need to implement a parser for .sol files

* set 9 min limit for optimization to run, otherwise travis halts after 10 min without output

* refactor: tests and move into slow_tests top directory

* style: flake8

* fix: no point in searching ImportError for 'cplex'

* test: add slow MILP tests for gurobi

* Fix constraint bounds issue (#84)

* Fix LB/UB Coefficient Change (#82)

* Fix LB/UB Coefficient Change

If coeff is negative, the lower bound could become larger than the upper bound causing an error. Now, the upper bound is first removed and then both bounds are reapplied with the shift.

* Update interface.py

* test: add unittest for constraint expression standardisation

* fix: flake8

* fix: flake8 again

* refactor: simplify logic in constraint offset handling

* fix: make sure that sloppy arg is being passed to super (#83)

* Netlib tests test also cloned models (#79)

* feat: add cloned model tests to netlib test suites

* chore: add cloned model tests also for gurobi

* fix: critical bug affecting ranged constraints in cplex interface

* test: skip netlib cloning tests on travis

* Add a Gitter chat badge to README.rst (#86) (#88)

* Various fixes (#78)

fix: objective.value should return None if not in model
fix: get correct primal values from glpk MIPs
test: add test for obj value without model
fix: allow older versions of cplex
Numerous gurobi fixes
Miplib test suite

* Add Gitter badge

* Fix gitter badge location

* Fix gitter badge 2

* feat: cplex 12.7.1 compatibility

* Change Module Loading Procedure (#91)

This fixes part of Issue #90 where if any module fails to load, optlang itself cannot load. Basically, default to the best solver that loads.

* Fix MIP dual value behaviour (#93)

Raise a ValueError when trying to get constraint or variable dual values for an integer problem (instead of returning None)

Fix: Adds functionality to automatically change the type of the cplex problem when changing variable types

* fix: cplex problem type change back to LP

When going from MILP to LP cplex problem type needs to be explicitly
changed back to LP

* feat: Model is_integer method

* test: add tests for is_integer and integer duals

* fix: flake8

* test: move tests to cplex interface for now

* fix: add reason to skipTest

* chore: raise ValueErrors for duals in IPs

* chore: raise ValueError when getting IP duals (gurobi)

* feat: is_integer in gurobi

* test: add test for getting duals in batch for MIP

* fix: remove unnecessary checks

* Catch Cplex errors and raise `optlang.SolverError` (#96)

* feat: add `SolverError` exception

This introduces the unified optlang error that should be reraised anywhere that
solver specific errors can occur. Third party code can then rely on catching the
optlang error only.

* feat: catch Cplex errors and raise SolverError

* feat: add infeasible MIP test cases

* fix: correct uncovered problems in properties

* feat: add test cases for unsolved LP

* chore: fix flake8 errors

* Update README.rst

Refer to gitter for questions/problems

* Make glpk get_linear_coefficients respect variables arg. (#100)

* Refactor batch getters of problem values (#101)

Added Model._get_primal_values, _get_constraint_values, _get_shadow_prices, _get_reduced_costs for very efficient retrieval of values

* test: get_linear_coefficients returns correct length

* fix: disable results and logging with cplex verbosity=0

* test: adapt netlib test to cplex 12.7.1

* chore: use swiglpk batch value functions

* Disable _round_primal_to_bounds (#107)

This requires a more accurate tolerance and should already be handled
by the solver

* fix: don't round integer primals

Rounding can make some bugs hard to detect

* refactor: make gurobi use var._get_primal

* chore: remove gurobi under construction warning

Hooray

* Interface to tolerance parameters (#108)

* feat: add tolerance parameters to Configuration

* fix: pickling cplex configurations

* feat: default pickling method for models (json-based)

Fixes scipy pickling

* test: add tolerance param testing

* fix: test case bug

* fix: glpk tolerance names

* chore: update swiglpk requirement

* Objective constant terms (#109)

The expression of an objective can now include constant terms, and the solver will correctly account for this when reporting the objective values.

* chore: update readme

* fix: convert obj offsets to floats

Prevent objective values from being sympy types

* fix: one more sympy offset

* Add support for symengine (experimental) (#87)

Add symbolics.py module that handles sympy/symengine calls
Symengine.py 0.3 is required. Symengine can be installed with conda `conda install -c symengine -c conda-forge python-symengine`
If symengine is available optlang will attempt to use it automatically

* fix: ensure that variable names are not empty str (#127)

Variables with empty names trigger's segfaults in glpk
interface. Easiest fix appears to simply not allow them as anyway not
meaningful.

* Various bug fixes (#126)

- Make gurobi models picklable
- Fix gurobi infinite recursion
- Fix gurobi constraint primal calculation
- Fix a bug where cplex took forever to set a large objective (e.g. 10000 vars)
- Fix some issues with symengine compatibility

* chore: clean up interface.py

* test: add tests and pragmas (for symengine)

* test: more pragmas

* fix: allow setting bounds on binary variables

Bounds can be set to 0 or 1 (both lb and ub). Also added tests.

* chore: add beta warning when using symengine

* fix: convert cplex problem to LP when last integer var is removed

* test: add coverage

* test: more coverage

* test: pragma for symengine

* Add basic types to optlang.symbolics (#134)

* Add basic types to optlang.symbolics

* Handle edge cases in add and mul

* also add Basic and Number to sympy wrapper

* feat: add and mul return identities if called without arguments

* fix: flake8

* test: add tests for symbolics coverage

* Add missing unpacking in symengine mul (#136)

* Add an interface to GLPK's exact solver (#145)

* feat: add GLPK exact solver interface

* fix: don't monkey patch the glpk_exact_interface test suite

* style: flake8

* adding simple example using numpy linear algebra (#143)

* Update simple_numpy.py

* Fix #140 – indicator constraints not being removed (#144)

* fix: fixes #140 (indicator constraints not being removed)

I am not sure this is a good solution. Also, need to extend solution in
case we're adding support quadratic constraints.

* refactor: better error handling

* chore: add glpk_exact_interface to init.py

* chore: pep8 style

* Test symengine on travis (#146)

* test: run symengine tests on travis

* test: fix symengine matrix and properly indent .travis.yml

* test: fix indentation error in .travis.yml

* chore: check variable name type for symengine

* test: raise an exception on travis if symengine could not be used

* fix: add some pickle magic

Tell pickle how to pickle variables

* chore: remove symengine beta warning

* chore: Temporarily disable changing of indicator variable

Does not work currently

* test: add cplex indicator constraint expression test

* test: comment out unused features of scipy_interface

* test: improve cplex test coverage

* fix: flake8

* chore: disable integer variables in glpk_exact_interface

* chore: remove unnecessary logic in glpk_exact_interface

This is done in the super call to glpk_interface.Model._optimize()

* chore: add optlang._USING_SYMENGINE flag (read-only)

* fix: typos in glpk_exact_interface

* chore: adapt glpk_exact tests to not support integer vars

* fix: call update in set_linear_coefficients and get_linear_coefficients

* chore: raise an error if glpk ids are longer than 256 chars

GLPK does not support longer ID’s

* Add QP capability for Gurobi (#149)

* Add gurobi QPs

* add some fixes and QP tests for gurobi

* fix: make releases from only one Python version (#152)

* fix: make releases from only one Python version

* refactor: make use of Travis deployment

* fix: exclude CPLEX on Python 3.6

* refactor: add further condition, remove dependencies

* refactor: reduce clone depth, branches built

* refactor: limit AppVeyor in the same way

* fix: git clone depth

* Fix name issues (#155)

* feat: raise an error when GLPK fails to read/write a file

* feat: implement checks of variable and constraint names

Both during initialization and subsequent changes

* fix: call the validation methods in subinterfaces

* test: added tests for name checks

* fix: call name checks in gurobi

* fix: Variable cannot be initialized with name=None

* fix: Change error message for constraint and objective id errors

* fix: get None if variable.problem is not defined

Don’t crash if variable does not have a problem

* fix: flake8

* refactor: set cplex logger name (#162)

cplex_interface.Configuration was setting the root logger level to CRITICAL which silences most other logging output across the process.

* chore: change logger name

Changed the logger name to optlang.cplex_interface.Model to make it
clear that the logging comes from optlang and not from cplex itself.

* fix: change verbosity first, so subsequent changes are not always logged

* refactor: make base constants real numbers (#160)

* refactor: make base constants real numbers

Using real numbers avoids certain corner cases where integers are either
undesired, e.g., in `set_linear_coefficients` or they would be
simplified out as in `Mul(One, variable)`.

* fix: correct test cases accordingly

* fix: return statement to integer comparison

* chore: only construct Integer(1) once

* test: add constraint and objective construction tests with sloppy

* fix: make scipy get_linear_coefficients return regular numeric types

* Update README.rst

* Update README.rst

* fix: automatically expand expressions when using symengine

Symengine doesn’t do any expansions (even of constants), which causes
bugs. Expanding is very fast in symengine, so performance is not
impacted

* fix: flake8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants