美文网首页
【工程】Pulp-Amply(三)

【工程】Pulp-Amply(三)

作者: longgb246 | 来源:发表于2018-04-07 12:30 被阅读0次

    一、Amply

    Amply允许您以Python数据结构加载和操作AMPL数据。
    Amply只支持AMPL语法的特定子集:
    1、设置声明
    2、设置数据语句
    3、参数声明
    4、参数数据语句

    二、声明和数据语句

    通常,在AMPL中表达的问题包括两个部分,一个模型部分和一个数据部分。Amply只设计用于解析AMPL数据段中包含的参数和设置语句。然而,为了正确地解析这些语句,可能需要在模型部分中包含的信息。例如,它不可能从它的数据语句中推断出一个集合的维数。因此,Amply也支持设置和参数声明。这些不必放在单独的部分中,它们只需要在相应的数据语句之前发生。
    支持的声明语法非常有限,并且不包含AMPL编程语言的大多数元素。其目的是将该库用作加载类似于amplam语法的数据的一种方式。
    此外,Amply不对数据语句执行任何验证。
    该文档的目的是指导Amply所支持的语法,而不是一般的AMPL参考手册。

    三、快速入门指南

    导入的类:

    from pulp import Amply
    

    一个简单的集合。

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

    集合可以包含多维数据,但是我们必须先声明它们。

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

    集合他们自己可以是多维的:

    >>> 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']
    

    注意,在上面的例子中,集合COUNTRIES实际上并不需要存在。

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

    可以使用切片指定多维数据

    >>> 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)]>
    

    集合数据也可以使用矩阵符号来指定。“+”表示这一对被包含在集合中,而“-”表示一对不在集合中。

    >>> 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')]>
    

    矩阵也可以被调换:

    >>> 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')]>
    

    矩阵只指定2d数据,但是它们可以与切片结合来定义更高维度的数据:

    >>> 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)]>
    

    参数也支持:

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

    参数通常在集合上被索引。

    >>> 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
    

    参数可以包含默认值

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

    集合的复合索引:

    >>> 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
    >>> 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}}>
    
    >>> 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}}>
    >>> 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}}>
    

    高维:

    >>> 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

    class Amply(string=""):
        load_string(string): Parse string data.
        load_file(file): Parse contents of file or file-like object (has a read() method).
        static from_file(file): Alternate constructor. Create Amply object from contents of file or file-like object.
    

    相关文章

      网友评论

          本文标题:【工程】Pulp-Amply(三)

          本文链接:https://www.haomeiwen.com/subject/axqyhftx.html