amply


Nameamply JSON
Version 0.1.6 PyPI version JSON
download
home_pagehttp://github.com/willu47/amply
SummaryAmply allows you to load and manipulate AMPL/GLPK data as Python data structures
upload_time2023-06-20 15:43:10
maintainer
docs_urlNone
authorWill Usher,Christophe-Marie Duquesne,Stuart Mitchell,Franco Peschiera,Stu,smit023,Q. Lim
requires_python>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
licenseEclipse Public License 1.0 (EPL-1.0)
keywords ampl gmpl
VCS
bugtrack_url
requirements pyparsing
Travis-CI
coveralls test coverage No coveralls.
            Amply
======

.. image:: https://travis-ci.com/willu47/amply.svg?branch=master
    :target: https://travis-ci.com/willu47/amply
.. image:: https://img.shields.io/pypi/v/amply?style=plastic
     :alt: PyPI
     :target: https://pypi.org/project/amply/
.. image:: https://coveralls.io/repos/github/willu47/amply/badge.svg?branch=master
    :target: https://coveralls.io/github/willu47/amply?branch=master


Introduction
------------

Amply allows you to load and manipulate AMPL data as Python data structures.

Amply only supports a specific subset of the AMPL syntax:

* set declarations
* set data statements
* parameter declarations
* parameter data statements

Declarations and data statements
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Typically, problems expressed in AMPL consist of two parts, a *model* section and a *data* section.
Amply is only designed to parse the parameter and set statements contained within AMPL data sections.
However, in order to parse these statements correctly, information that would usually be contained
within the model section may be required. For instance, it may not be possible to infer the dimension
of a set purely from its data statement. Therefore, Amply also supports set and parameter declarations.
These do not have to be put in a separate section, they only need to occur before the corresponding
data statement.


The declaration syntax supported is extremely limited, and does not include most
elements of the AMPL programming language. The intention is that this library
is used as a way of loading data specified in an AMPL-like syntax.

Furthermore, Amply does not perform any validation on data statements.

About this document
^^^^^^^^^^^^^^^^^^^^

This document is intended as a guide to the syntax supported by Amply, and not as a general
AMPL reference manual. For more in depth coverage see the `GNU MathProg manual, Chapter 5: Model data
<http://gusek.sourceforge.net/gmpl.pdf>`_ or the following links:

* `Sets in AMPL <http://twiki.esc.auckland.ac.nz/twiki/bin/view/OpsRes/SetsInAMPL>`_
* `Parameters in AMPL <http://twiki.esc.auckland.ac.nz/twiki/bin/view/OpsRes/ParametersInAMPL>`_

Quickstart Guide
----------------

  >>> from amply import Amply

Import the class: ::

  >>> from amply import Amply

A simple set. Sets behave a lot like lists.

  >>> data = Amply("set CITIES := Auckland Wellington Christchurch;")
  >>> print data.CITIES
  <SetObject: ['Auckland', 'Wellington', 'Christchurch']>
  >>> print data['CITIES']
  <SetObject: ['Auckland', 'Wellington', 'Christchurch']>
  >>> for c in data.CITIES: print c
  ...
  Auckland
  Wellington
  Christchurch
  >>> print data.CITIES[0]
  Auckland
  >>> print len(data.CITIES)
  3


Data can be integers, reals, symbolic, or quoted strings:

  >>> data = Amply("""
  ...   set BitsNPieces := 0 3.2 -6e4 Hello "Hello, World!";
  ... """)
  >>> print data.BitsNPieces
  <SetObject: [0.0, 3.2000000000000002, -60000.0, 'Hello', 'Hello, World!']>

Sets can contain multidimensional data, but we have to declare them to be so first.

  >>> data = Amply("""
  ... set pairs dimen 2;
  ... set pairs := (1, 2) (2, 3) (3, 4);
  ... """)
  >>> print data.pairs
  <SetObject: [(1, 2), (2, 3), (3, 4)]>

Sets themselves can be multidimensional (i.e. be subscriptable):

  >>> data = Amply("""
  ... set CITIES{COUNTRIES};
  ... set CITIES[Australia] := Adelaide Melbourne Sydney;
  ... set CITIES[Italy] := Florence Milan Rome;
  ... """)
  >>> print data.CITIES['Australia']
  ['Adelaide', 'Melbourne', 'Sydney']
  >>> print data.CITIES['Italy']
  ['Florence', 'Milan', 'Rome']

Note that in the above example, the set COUNTRIES didn't actually have to exist itself.
Amply does not perform any validation on subscripts, it only uses them to figure out
how many subscripts a set has. To specify more than one, separate them by commas:

  >>> data = Amply("""
  ... set SUBURBS{COUNTRIES, CITIES};
  ... set SUBURBS[Australia, Melbourne] := Docklands 'South Wharf' Kensington;
  ... """)
  >>> print data.SUBURBS['Australia', 'Melbourne']
  ['Docklands', 'South Wharf', 'Kensington']

*Slices* can be used to simplify the entry of multi-dimensional data.

  >>> data=Amply("""
  ... set TRIPLES dimen 3;
  ... set TRIPLES := (1, 1, *) 2 3 4 (*, 2, *) 6 7 8 9 (*, *, *) (1, 1, 1);
  ... """)
  >>> print data.TRIPLES
  <SetObject: [(1, 1, 2), (1, 1, 3), (1, 1, 4), (6, 2, 7), (8, 2, 9), (1, 1, 1)]>
  >

Set data can also be specified using a matrix notation.
A '+' indicates that the pair is included in the set whereas a '-' indicates a
pair not in the set.

  >>> data=Amply("""
  ... set ROUTES dimen 2;
  ... set ROUTES : A B C D :=
  ...            E + - - +
  ...            F + + - -
  ... ;
  ... """)
  >>> print data.ROUTES
  <SetObject: [('E', 'A'), ('E', 'D'), ('F', 'A'), ('F', 'B')]>

Matrices can also be transposed:

  >>> data=Amply("""
  ... set ROUTES dimen 2;
  ... set ROUTES (tr) : E F :=
  ...                 A + +
  ...                 B - +
  ...                 C - -
  ...                 D + -
  ... ;
  ... """)
  >>> print data.ROUTES
  <SetObject: [('E', 'A'), ('F', 'A'), ('F', 'B'), ('E', 'D')]>

Matrices only specify 2d data, however they can be combined with slices
to define higher-dimensional data:

  >>> data = Amply("""
  ... set QUADS dimen 2;
  ... set QUADS :=
  ... (1, 1, *, *) : 2 3 4 :=
  ...              2 + - +
  ...              3 - + +
  ... (1, 2, *, *) : 2 3 4 :=
  ...              2 - + -
  ...              3 + - -
  ... ;
  ... """)
  >>> print data.QUADS
  <SetObject: [(1, 1, 2, 2), (1, 1, 2, 4), (1, 1, 3, 3), (1, 1, 3, 4), (1, 2, 2, 3), (1, 2, 3, 2)]>

Parameters are also supported:

  >>> data = Amply("""
  ... param T := 30;
  ... param n := 5;
  ... """)
  >>> print data.T
  30
  >>> print data.n
  5

Parameters are commonly indexed over sets. No validation is done by Amply,
and the sets do not have to exist. Parameter objects are represented
as a mapping.

  >>> data = Amply("""
  ... param COSTS{PRODUCTS};
  ... param COSTS :=
  ...   FISH 8.5
  ...   CARROTS 2.4
  ...   POTATOES 1.6
  ... ;
  ... """)
  >>> print data.COSTS
  <ParamObject: {'POTATOES': 1.6000000000000001, 'FISH': 8.5, 'CARROTS': 2.3999999999999999}>
  >>> print data.COSTS['FISH']
  8.5

Parameter data statements can include a *default* clause. If a '.' is included
in the data, it is replaced with the default value:

  >>> data = Amply("""
  ... param COSTS{P};
  ... param COSTS default 2 :=
  ... F 2
  ... E 1
  ... D .
  ... ;
  ... """)
  >>> print data.COSTS['D']
  2.0

Parameter declarations can also have a default clause. For these parameters,
any attempt to access the parameter for a key that has not been defined
will return the default value:

  >>> data = Amply("""
  ... param COSTS{P} default 42;
  ... param COSTS :=
  ... F 2
  ... E 1
  ... ;
  ... """)
  >>> print data.COSTS['DOES NOT EXIST']
  42.0

Parameters can be indexed over multiple sets. The resulting values can be
accessed by treating the parameter object as a nested dictionary, or by
using a tuple as an index:

  >>> data = Amply("""
  ... param COSTS{CITIES, PRODUCTS};
  ... param COSTS :=
  ...  Auckland FISH 5
  ...  Auckland CHIPS 3
  ...  Wellington FISH 4
  ...  Wellington CHIPS 1
  ... ;
  ... """)
  >>> print data.COSTS
  <ParamObject: {'Wellington': {'FISH': 4.0, 'CHIPS': 1.0}, 'Auckland': {'FISH': 5.0, 'CHIPS': 3.0}}>
  >>> print data.COSTS['Wellington']['CHIPS'] # nested dict
  1.0
  >>> print data.COSTS['Wellington', 'CHIPS'] # tuple as key
  1.0

Parameters support a slice syntax similar to that of sets:

  >>> data = Amply("""
  ... param COSTS{CITIES, PRODUCTS};
  ... param COSTS :=
  ...  [Auckland, * ]
  ...   FISH 5
  ...   CHIPS 3
  ...  [Wellington, * ]
  ...   FISH 4
  ...   CHIPS 1
  ... ;
  ... """)
  >>> print data.COSTS
  <ParamObject: {'Wellington': {'FISH': 4.0, 'CHIPS': 1.0}, 'Auckland': {'FISH': 5.0, 'CHIPS': 3.0}}>



Parameters indexed over two sets can also be specified in tabular format:


  >>> data = Amply("""
  ... param COSTS{CITIES, PRODUCTS};
  ... param COSTS: FISH CHIPS :=
  ...  Auckland    5    3
  ...  Wellington  4    1
  ... ;
  ... """)
  >>> print data.COSTS
  <ParamObject: {'Wellington': {'FISH': 4.0, 'CHIPS': 1.0}, 'Auckland': {'FISH': 5.0, 'CHIPS': 3.0}}>

Tabular data can also be transposed:

  >>> data = Amply("""
  ... param COSTS{CITIES, PRODUCTS};
  ... param COSTS (tr): Auckland Wellington :=
  ...            FISH   5        4
  ...            CHIPS  3        1
  ... ;
  ... """)
  >>> print data.COSTS
  <ParamObject: {'Wellington': {'FISH': 4.0, 'CHIPS': 1.0}, 'Auckland': {'FISH': 5.0, 'CHIPS': 3.0}}>


Slices can be combined with tabular data for parameters indexed over more than
2 sets:

  >>> data = Amply("""
  ... param COSTS{CITIES, PRODUCTS, SIZE};
  ... param COSTS :=
  ...  [Auckland, *, *] :   SMALL LARGE :=
  ...                 FISH  5     9
  ...                 CHIPS 3     5
  ...  [Wellington, *, *] : SMALL LARGE :=
  ...                 FISH  4     7
  ...                 CHIPS 1     2
  ... ;
  ... """)
  >>> print data.COSTS
  <ParamObject: {'Wellington': {'FISH': {'SMALL': 4.0, 'LARGE': 7.0}, 'CHIPS': {'SMALL': 1.0, 'LARGE': 2.0}}, 'Auckland': {'FISH': {'SMALL': 5.0, 'LARGE': 9.0}, '


API
---

All functionality is contained within the ``Amply`` class.

.. class:: Amply(string="")

  load_string(string)

    Parse string data.

  load_file(file)

    Parse contents of file or file-like object (has a read() method).

  from_file(file)

    Alternate constructor. Create Amply object from contents of file or file-like object.


The parsed data structures can then be accessed from an ``Amply`` object via
attribute lookup (if the name of the symbol is a valid Python name) or item
lookup. ::

    from pulp import Amply

    data = Amply("set CITIES := Auckland Hamilton Wellington")

    # attribute lookup
    assert data.CITIES == ['Auckland', 'Hamilton', 'Wellington']

    # item lookup
    assert data['CITIES'] == data.CITIES

Note that additional data may be loaded into an Amply object simply by calling
one of its methods. A common idiom might be to specify the set and parameter
declarations within your Python script, then load the actual data from
external files. ::

    from pulp import Amply

    data = Amply("""
      set CITIES;
      set ROUTES dimen 2;
      param COSTS{ROUTES};
      param DISTANCES{ROUTES};
    """)

    for data_file in ('cities.dat', 'routes.dat', 'costs.dat', 'distances.dat'):
        data.load_file(open(data_file))

.. Commented out the below, not sure if we need it (incomplete)

    Reference
    ---------

    Sets
    ^^^^

    Set declarations
    ~~~~~~~~~~~~~~~~

    A set declaration is an extremely limited version of set statements which are valid in AMPL models.
    They determine the *subscript domain* and *data dimension* of the set. If not specified, the default
    subscript domain is an empty set and the default dimension is 1.

    .. productionlist::
        set_def_stmt: "set" `name` [`subscript_domain`] ["dimen" `integer`] ";"
        subscript_domain: "{" `name` ("," `name`)* "}"

    The following statment declares a set named "countries". ::

        set countries;

    The following statement declares a set named "cities" which is indexed over "countries". ::

        set cities {countries};

    The following declares a set named "routes" with 2d data. ::

        set routes dimen 2;

    Set data statements
    ~~~~~~~~~~~~~~~~~~~~~

    A set data statement is used to specify the members of a set. It consists of one or more
    *data records*. There are four types of data records: simple data, slice records, matrix
    data and transposed matrix data.

    .. productionlist::
        set_stmt: "set" `name` [`set_member`] `data_record`+ ";"
        data_record: `simple_data` | `set_slice_record` | `matrix_data` | `tr_matrix_data`

    Simple Data
    ############

    A simple data record is an optionally comma-separated list of data values.

    .. productionlist::
        simple_data: `data` ([","] `data`)*

    For instance: ::

        set CITIES := Auckland Hamilton 'Palmerston North' Wellington;

    ::

        set ROUTES dimen 2;
        set ROUTES := (Auckland, Hamilton) (Auckland, Wellington);

    Slice Records
    ###############

    Slice records are used to simplify the entry of multi-dimensional sets. They allow you to partially
    specify the values of elements. A slice affects all data records that follow it (until a new slice
    is specified).

    .. productionlist::
        set_slice_record: "(" `set_slice_component` ("," `set_slice_component`)* ")"
        set_slice_component: `number` | `symbol` | "*"

    This is best demonstrated by some examples. The sets A and B are identical: ::

        set A dimen 3;
        set B dimen 3;

        set A := (1, 2, 3) (1, 3, 2) (1, 4, 6) (1, 8, 8) (2, 1, 3) (2, 1, 1) (2, 1, 2);
        set B := (1, *, *) (2, 3) (3, 2) (4, 6) (8, 8) (2, 1, *) 3 1 2;

    The number of asterisks in a slice is called the *slice dimension*. Any data records that follow
    are interpreted as being of the same dimension; the value is taken as the value of the slice with
    the asterisks replaced with the value of the record.

    Matrix records
    ################

    Matrix records are a convenient way of specifying 2-dimensional data. The data record looks like
    a matrix with row and column headings, where the values are either '+' if the combination is in
    the set, and '-' if the combination is not in the set. A common use-case is for defining the
    set of arcs that exist between a set of nodes.

    .. productionlist::
        matrix_data: ":" `matrix_columns` ":=" `matrix_row`+
        matrix_columns: `data`+
        matrix_row: `data` ("+"|"-")+
        tr_matrix_data: "(tr)" `matrix_data`

    Matrices can also be transposed by including ``(tr)`` immediately preceding the record.

    In the example below the sets A, B and C are identical: ::

        set A dimen 2;
        set B dimen 2;
        set C dimen 2;

        set A := (1, 1) (1, 3) (2, 2) (3, 1) (3, 2) (3, 3);
        set B : 1 2 3 :=
              1 + - +
              2 - + -
              3 + + +
        ;
        set C (tr) : 1 2 3 :=
                   1 + - +
                   2 - + +
                   3 + - +
        ;


    Matrices can be used for sets with higher dimensions by placing them after 2 dimensional
    slice records.


    Set examples
    ~~~~~~~~~~~~

    Parameters
    ^^^^^^^^^^^^

    Plain Data
    ~~~~~~~~~~~~~

    Tabular data
    ~~~~~~~~~~~~~~

    Tabbing Data
    ~~~~~~~~~~~~~~

Development Notes
-----------------

Many thanks to Johannes Ragam (@thet), former custodian of the "amply" project on PyPi.
Johannes graciously transferred the project to this. Thanks!

            

Raw data

            {
    "_id": null,
    "home_page": "http://github.com/willu47/amply",
    "name": "amply",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
    "maintainer_email": "",
    "keywords": "ampl gmpl",
    "author": "Will Usher,Christophe-Marie Duquesne,Stuart Mitchell,Franco Peschiera,Stu,smit023,Q. Lim",
    "author_email": "wusher@kth.se",
    "download_url": "https://files.pythonhosted.org/packages/6a/35/99243f9aed9848f9ab505b201cc09fd86a8d0c8e35aafeee5b830cc4919d/amply-0.1.6.tar.gz",
    "platform": null,
    "description": "Amply\n======\n\n.. image:: https://travis-ci.com/willu47/amply.svg?branch=master\n    :target: https://travis-ci.com/willu47/amply\n.. image:: https://img.shields.io/pypi/v/amply?style=plastic\n     :alt: PyPI\n     :target: https://pypi.org/project/amply/\n.. image:: https://coveralls.io/repos/github/willu47/amply/badge.svg?branch=master\n    :target: https://coveralls.io/github/willu47/amply?branch=master\n\n\nIntroduction\n------------\n\nAmply allows you to load and manipulate AMPL data as Python data structures.\n\nAmply only supports a specific subset of the AMPL syntax:\n\n* set declarations\n* set data statements\n* parameter declarations\n* parameter data statements\n\nDeclarations and data statements\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nTypically, problems expressed in AMPL consist of two parts, a *model* section and a *data* section.\nAmply is only designed to parse the parameter and set statements contained within AMPL data sections.\nHowever, in order to parse these statements correctly, information that would usually be contained\nwithin the model section may be required. For instance, it may not be possible to infer the dimension\nof a set purely from its data statement. Therefore, Amply also supports set and parameter declarations.\nThese do not have to be put in a separate section, they only need to occur before the corresponding\ndata statement.\n\n\nThe declaration syntax supported is extremely limited, and does not include most\nelements of the AMPL programming language. The intention is that this library\nis used as a way of loading data specified in an AMPL-like syntax.\n\nFurthermore, Amply does not perform any validation on data statements.\n\nAbout this document\n^^^^^^^^^^^^^^^^^^^^\n\nThis document is intended as a guide to the syntax supported by Amply, and not as a general\nAMPL reference manual. For more in depth coverage see the `GNU MathProg manual, Chapter 5: Model data\n<http://gusek.sourceforge.net/gmpl.pdf>`_ or the following links:\n\n* `Sets in AMPL <http://twiki.esc.auckland.ac.nz/twiki/bin/view/OpsRes/SetsInAMPL>`_\n* `Parameters in AMPL <http://twiki.esc.auckland.ac.nz/twiki/bin/view/OpsRes/ParametersInAMPL>`_\n\nQuickstart Guide\n----------------\n\n  >>> from amply import Amply\n\nImport the class: ::\n\n  >>> from amply import Amply\n\nA simple set. Sets behave a lot like lists.\n\n  >>> data = Amply(\"set CITIES := Auckland Wellington Christchurch;\")\n  >>> print data.CITIES\n  <SetObject: ['Auckland', 'Wellington', 'Christchurch']>\n  >>> print data['CITIES']\n  <SetObject: ['Auckland', 'Wellington', 'Christchurch']>\n  >>> for c in data.CITIES: print c\n  ...\n  Auckland\n  Wellington\n  Christchurch\n  >>> print data.CITIES[0]\n  Auckland\n  >>> print len(data.CITIES)\n  3\n\n\nData can be integers, reals, symbolic, or quoted strings:\n\n  >>> data = Amply(\"\"\"\n  ...   set BitsNPieces := 0 3.2 -6e4 Hello \"Hello, World!\";\n  ... \"\"\")\n  >>> print data.BitsNPieces\n  <SetObject: [0.0, 3.2000000000000002, -60000.0, 'Hello', 'Hello, World!']>\n\nSets can contain multidimensional data, but we have to declare them to be so first.\n\n  >>> data = Amply(\"\"\"\n  ... set pairs dimen 2;\n  ... set pairs := (1, 2) (2, 3) (3, 4);\n  ... \"\"\")\n  >>> print data.pairs\n  <SetObject: [(1, 2), (2, 3), (3, 4)]>\n\nSets themselves can be multidimensional (i.e. be subscriptable):\n\n  >>> data = Amply(\"\"\"\n  ... set CITIES{COUNTRIES};\n  ... set CITIES[Australia] := Adelaide Melbourne Sydney;\n  ... set CITIES[Italy] := Florence Milan Rome;\n  ... \"\"\")\n  >>> print data.CITIES['Australia']\n  ['Adelaide', 'Melbourne', 'Sydney']\n  >>> print data.CITIES['Italy']\n  ['Florence', 'Milan', 'Rome']\n\nNote that in the above example, the set COUNTRIES didn't actually have to exist itself.\nAmply does not perform any validation on subscripts, it only uses them to figure out\nhow many subscripts a set has. To specify more than one, separate them by commas:\n\n  >>> data = Amply(\"\"\"\n  ... set SUBURBS{COUNTRIES, CITIES};\n  ... set SUBURBS[Australia, Melbourne] := Docklands 'South Wharf' Kensington;\n  ... \"\"\")\n  >>> print data.SUBURBS['Australia', 'Melbourne']\n  ['Docklands', 'South Wharf', 'Kensington']\n\n*Slices* can be used to simplify the entry of multi-dimensional data.\n\n  >>> data=Amply(\"\"\"\n  ... set TRIPLES dimen 3;\n  ... set TRIPLES := (1, 1, *) 2 3 4 (*, 2, *) 6 7 8 9 (*, *, *) (1, 1, 1);\n  ... \"\"\")\n  >>> print data.TRIPLES\n  <SetObject: [(1, 1, 2), (1, 1, 3), (1, 1, 4), (6, 2, 7), (8, 2, 9), (1, 1, 1)]>\n  >\n\nSet data can also be specified using a matrix notation.\nA '+' indicates that the pair is included in the set whereas a '-' indicates a\npair not in the set.\n\n  >>> data=Amply(\"\"\"\n  ... set ROUTES dimen 2;\n  ... set ROUTES : A B C D :=\n  ...            E + - - +\n  ...            F + + - -\n  ... ;\n  ... \"\"\")\n  >>> print data.ROUTES\n  <SetObject: [('E', 'A'), ('E', 'D'), ('F', 'A'), ('F', 'B')]>\n\nMatrices can also be transposed:\n\n  >>> data=Amply(\"\"\"\n  ... set ROUTES dimen 2;\n  ... set ROUTES (tr) : E F :=\n  ...                 A + +\n  ...                 B - +\n  ...                 C - -\n  ...                 D + -\n  ... ;\n  ... \"\"\")\n  >>> print data.ROUTES\n  <SetObject: [('E', 'A'), ('F', 'A'), ('F', 'B'), ('E', 'D')]>\n\nMatrices only specify 2d data, however they can be combined with slices\nto define higher-dimensional data:\n\n  >>> data = Amply(\"\"\"\n  ... set QUADS dimen 2;\n  ... set QUADS :=\n  ... (1, 1, *, *) : 2 3 4 :=\n  ...              2 + - +\n  ...              3 - + +\n  ... (1, 2, *, *) : 2 3 4 :=\n  ...              2 - + -\n  ...              3 + - -\n  ... ;\n  ... \"\"\")\n  >>> print data.QUADS\n  <SetObject: [(1, 1, 2, 2), (1, 1, 2, 4), (1, 1, 3, 3), (1, 1, 3, 4), (1, 2, 2, 3), (1, 2, 3, 2)]>\n\nParameters are also supported:\n\n  >>> data = Amply(\"\"\"\n  ... param T := 30;\n  ... param n := 5;\n  ... \"\"\")\n  >>> print data.T\n  30\n  >>> print data.n\n  5\n\nParameters are commonly indexed over sets. No validation is done by Amply,\nand the sets do not have to exist. Parameter objects are represented\nas a mapping.\n\n  >>> data = Amply(\"\"\"\n  ... param COSTS{PRODUCTS};\n  ... param COSTS :=\n  ...   FISH 8.5\n  ...   CARROTS 2.4\n  ...   POTATOES 1.6\n  ... ;\n  ... \"\"\")\n  >>> print data.COSTS\n  <ParamObject: {'POTATOES': 1.6000000000000001, 'FISH': 8.5, 'CARROTS': 2.3999999999999999}>\n  >>> print data.COSTS['FISH']\n  8.5\n\nParameter data statements can include a *default* clause. If a '.' is included\nin the data, it is replaced with the default value:\n\n  >>> data = Amply(\"\"\"\n  ... param COSTS{P};\n  ... param COSTS default 2 :=\n  ... F 2\n  ... E 1\n  ... D .\n  ... ;\n  ... \"\"\")\n  >>> print data.COSTS['D']\n  2.0\n\nParameter declarations can also have a default clause. For these parameters,\nany attempt to access the parameter for a key that has not been defined\nwill return the default value:\n\n  >>> data = Amply(\"\"\"\n  ... param COSTS{P} default 42;\n  ... param COSTS :=\n  ... F 2\n  ... E 1\n  ... ;\n  ... \"\"\")\n  >>> print data.COSTS['DOES NOT EXIST']\n  42.0\n\nParameters can be indexed over multiple sets. The resulting values can be\naccessed by treating the parameter object as a nested dictionary, or by\nusing a tuple as an index:\n\n  >>> data = Amply(\"\"\"\n  ... param COSTS{CITIES, PRODUCTS};\n  ... param COSTS :=\n  ...  Auckland FISH 5\n  ...  Auckland CHIPS 3\n  ...  Wellington FISH 4\n  ...  Wellington CHIPS 1\n  ... ;\n  ... \"\"\")\n  >>> print data.COSTS\n  <ParamObject: {'Wellington': {'FISH': 4.0, 'CHIPS': 1.0}, 'Auckland': {'FISH': 5.0, 'CHIPS': 3.0}}>\n  >>> print data.COSTS['Wellington']['CHIPS'] # nested dict\n  1.0\n  >>> print data.COSTS['Wellington', 'CHIPS'] # tuple as key\n  1.0\n\nParameters support a slice syntax similar to that of sets:\n\n  >>> data = Amply(\"\"\"\n  ... param COSTS{CITIES, PRODUCTS};\n  ... param COSTS :=\n  ...  [Auckland, * ]\n  ...   FISH 5\n  ...   CHIPS 3\n  ...  [Wellington, * ]\n  ...   FISH 4\n  ...   CHIPS 1\n  ... ;\n  ... \"\"\")\n  >>> print data.COSTS\n  <ParamObject: {'Wellington': {'FISH': 4.0, 'CHIPS': 1.0}, 'Auckland': {'FISH': 5.0, 'CHIPS': 3.0}}>\n\n\n\nParameters indexed over two sets can also be specified in tabular format:\n\n\n  >>> data = Amply(\"\"\"\n  ... param COSTS{CITIES, PRODUCTS};\n  ... param COSTS: FISH CHIPS :=\n  ...  Auckland    5    3\n  ...  Wellington  4    1\n  ... ;\n  ... \"\"\")\n  >>> print data.COSTS\n  <ParamObject: {'Wellington': {'FISH': 4.0, 'CHIPS': 1.0}, 'Auckland': {'FISH': 5.0, 'CHIPS': 3.0}}>\n\nTabular data can also be transposed:\n\n  >>> data = Amply(\"\"\"\n  ... param COSTS{CITIES, PRODUCTS};\n  ... param COSTS (tr): Auckland Wellington :=\n  ...            FISH   5        4\n  ...            CHIPS  3        1\n  ... ;\n  ... \"\"\")\n  >>> print data.COSTS\n  <ParamObject: {'Wellington': {'FISH': 4.0, 'CHIPS': 1.0}, 'Auckland': {'FISH': 5.0, 'CHIPS': 3.0}}>\n\n\nSlices can be combined with tabular data for parameters indexed over more than\n2 sets:\n\n  >>> data = Amply(\"\"\"\n  ... param COSTS{CITIES, PRODUCTS, SIZE};\n  ... param COSTS :=\n  ...  [Auckland, *, *] :   SMALL LARGE :=\n  ...                 FISH  5     9\n  ...                 CHIPS 3     5\n  ...  [Wellington, *, *] : SMALL LARGE :=\n  ...                 FISH  4     7\n  ...                 CHIPS 1     2\n  ... ;\n  ... \"\"\")\n  >>> print data.COSTS\n  <ParamObject: {'Wellington': {'FISH': {'SMALL': 4.0, 'LARGE': 7.0}, 'CHIPS': {'SMALL': 1.0, 'LARGE': 2.0}}, 'Auckland': {'FISH': {'SMALL': 5.0, 'LARGE': 9.0}, '\n\n\nAPI\n---\n\nAll functionality is contained within the ``Amply`` class.\n\n.. class:: Amply(string=\"\")\n\n  load_string(string)\n\n    Parse string data.\n\n  load_file(file)\n\n    Parse contents of file or file-like object (has a read() method).\n\n  from_file(file)\n\n    Alternate constructor. Create Amply object from contents of file or file-like object.\n\n\nThe parsed data structures can then be accessed from an ``Amply`` object via\nattribute lookup (if the name of the symbol is a valid Python name) or item\nlookup. ::\n\n    from pulp import Amply\n\n    data = Amply(\"set CITIES := Auckland Hamilton Wellington\")\n\n    # attribute lookup\n    assert data.CITIES == ['Auckland', 'Hamilton', 'Wellington']\n\n    # item lookup\n    assert data['CITIES'] == data.CITIES\n\nNote that additional data may be loaded into an Amply object simply by calling\none of its methods. A common idiom might be to specify the set and parameter\ndeclarations within your Python script, then load the actual data from\nexternal files. ::\n\n    from pulp import Amply\n\n    data = Amply(\"\"\"\n      set CITIES;\n      set ROUTES dimen 2;\n      param COSTS{ROUTES};\n      param DISTANCES{ROUTES};\n    \"\"\")\n\n    for data_file in ('cities.dat', 'routes.dat', 'costs.dat', 'distances.dat'):\n        data.load_file(open(data_file))\n\n.. Commented out the below, not sure if we need it (incomplete)\n\n    Reference\n    ---------\n\n    Sets\n    ^^^^\n\n    Set declarations\n    ~~~~~~~~~~~~~~~~\n\n    A set declaration is an extremely limited version of set statements which are valid in AMPL models.\n    They determine the *subscript domain* and *data dimension* of the set. If not specified, the default\n    subscript domain is an empty set and the default dimension is 1.\n\n    .. productionlist::\n        set_def_stmt: \"set\" `name` [`subscript_domain`] [\"dimen\" `integer`] \";\"\n        subscript_domain: \"{\" `name` (\",\" `name`)* \"}\"\n\n    The following statment declares a set named \"countries\". ::\n\n        set countries;\n\n    The following statement declares a set named \"cities\" which is indexed over \"countries\". ::\n\n        set cities {countries};\n\n    The following declares a set named \"routes\" with 2d data. ::\n\n        set routes dimen 2;\n\n    Set data statements\n    ~~~~~~~~~~~~~~~~~~~~~\n\n    A set data statement is used to specify the members of a set. It consists of one or more\n    *data records*. There are four types of data records: simple data, slice records, matrix\n    data and transposed matrix data.\n\n    .. productionlist::\n        set_stmt: \"set\" `name` [`set_member`] `data_record`+ \";\"\n        data_record: `simple_data` | `set_slice_record` | `matrix_data` | `tr_matrix_data`\n\n    Simple Data\n    ############\n\n    A simple data record is an optionally comma-separated list of data values.\n\n    .. productionlist::\n        simple_data: `data` ([\",\"] `data`)*\n\n    For instance: ::\n\n        set CITIES := Auckland Hamilton 'Palmerston North' Wellington;\n\n    ::\n\n        set ROUTES dimen 2;\n        set ROUTES := (Auckland, Hamilton) (Auckland, Wellington);\n\n    Slice Records\n    ###############\n\n    Slice records are used to simplify the entry of multi-dimensional sets. They allow you to partially\n    specify the values of elements. A slice affects all data records that follow it (until a new slice\n    is specified).\n\n    .. productionlist::\n        set_slice_record: \"(\" `set_slice_component` (\",\" `set_slice_component`)* \")\"\n        set_slice_component: `number` | `symbol` | \"*\"\n\n    This is best demonstrated by some examples. The sets A and B are identical: ::\n\n        set A dimen 3;\n        set B dimen 3;\n\n        set A := (1, 2, 3) (1, 3, 2) (1, 4, 6) (1, 8, 8) (2, 1, 3) (2, 1, 1) (2, 1, 2);\n        set B := (1, *, *) (2, 3) (3, 2) (4, 6) (8, 8) (2, 1, *) 3 1 2;\n\n    The number of asterisks in a slice is called the *slice dimension*. Any data records that follow\n    are interpreted as being of the same dimension; the value is taken as the value of the slice with\n    the asterisks replaced with the value of the record.\n\n    Matrix records\n    ################\n\n    Matrix records are a convenient way of specifying 2-dimensional data. The data record looks like\n    a matrix with row and column headings, where the values are either '+' if the combination is in\n    the set, and '-' if the combination is not in the set. A common use-case is for defining the\n    set of arcs that exist between a set of nodes.\n\n    .. productionlist::\n        matrix_data: \":\" `matrix_columns` \":=\" `matrix_row`+\n        matrix_columns: `data`+\n        matrix_row: `data` (\"+\"|\"-\")+\n        tr_matrix_data: \"(tr)\" `matrix_data`\n\n    Matrices can also be transposed by including ``(tr)`` immediately preceding the record.\n\n    In the example below the sets A, B and C are identical: ::\n\n        set A dimen 2;\n        set B dimen 2;\n        set C dimen 2;\n\n        set A := (1, 1) (1, 3) (2, 2) (3, 1) (3, 2) (3, 3);\n        set B : 1 2 3 :=\n              1 + - +\n              2 - + -\n              3 + + +\n        ;\n        set C (tr) : 1 2 3 :=\n                   1 + - +\n                   2 - + +\n                   3 + - +\n        ;\n\n\n    Matrices can be used for sets with higher dimensions by placing them after 2 dimensional\n    slice records.\n\n\n    Set examples\n    ~~~~~~~~~~~~\n\n    Parameters\n    ^^^^^^^^^^^^\n\n    Plain Data\n    ~~~~~~~~~~~~~\n\n    Tabular data\n    ~~~~~~~~~~~~~~\n\n    Tabbing Data\n    ~~~~~~~~~~~~~~\n\nDevelopment Notes\n-----------------\n\nMany thanks to Johannes Ragam (@thet), former custodian of the \"amply\" project on PyPi.\nJohannes graciously transferred the project to this. Thanks!\n",
    "bugtrack_url": null,
    "license": "Eclipse Public License 1.0 (EPL-1.0)",
    "summary": "Amply allows you to load and manipulate AMPL/GLPK data as Python data structures",
    "version": "0.1.6",
    "project_urls": {
        "Bug Tracker": "http://github.com/willu47/amply/issues",
        "Documentation": "http://github.com/willu47/amply/README.rst",
        "Homepage": "http://github.com/willu47/amply",
        "Source Code": "http://github.com/willu47/amply"
    },
    "split_keywords": [
        "ampl",
        "gmpl"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "43b15ff785027761a1d29d6fabbad15fe5d9327a38bc2a1eb753f9d467bbbb5d",
                "md5": "70f26bca94853261091114e1c49203cc",
                "sha256": "44c372cf60dbdfda06e80cbf201a28d1e6384485b6a2386ca21160dccd0e82a8"
            },
            "downloads": -1,
            "filename": "amply-0.1.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "70f26bca94853261091114e1c49203cc",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
            "size": 16768,
            "upload_time": "2023-06-20T15:43:09",
            "upload_time_iso_8601": "2023-06-20T15:43:09.230188Z",
            "url": "https://files.pythonhosted.org/packages/43/b1/5ff785027761a1d29d6fabbad15fe5d9327a38bc2a1eb753f9d467bbbb5d/amply-0.1.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6a3599243f9aed9848f9ab505b201cc09fd86a8d0c8e35aafeee5b830cc4919d",
                "md5": "3cfd4a231f59b8232c8b6b56625f5b53",
                "sha256": "61421103ccf8e1066717115fe7917610d831d551c68d31a110876a5b6c78aea4"
            },
            "downloads": -1,
            "filename": "amply-0.1.6.tar.gz",
            "has_sig": false,
            "md5_digest": "3cfd4a231f59b8232c8b6b56625f5b53",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
            "size": 29153,
            "upload_time": "2023-06-20T15:43:10",
            "upload_time_iso_8601": "2023-06-20T15:43:10.728132Z",
            "url": "https://files.pythonhosted.org/packages/6a/35/99243f9aed9848f9ab505b201cc09fd86a8d0c8e35aafeee5b830cc4919d/amply-0.1.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-20 15:43:10",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "willu47",
    "github_project": "amply",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "pyparsing",
            "specs": []
        }
    ],
    "lcname": "amply"
}
        
Elapsed time: 0.11054s