コード例 #1
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
 def test_data(self):
     result = Amply("param T := 4;")['T']
     assert result == 4
     result = Amply("param T := -4;")['T']
     assert result == -4
     result = Amply("param T := 0.04;")['T']
     assert result == 0.04
     result = Amply("param T := -0.04;")['T']
     assert result == -0.04
コード例 #2
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
 def test_load_file(self):
     a = Amply("param T:= 4; param X{foo};")
     try:
         s = StringIO("param S := 6; param X := 1 2;")
     except TypeError:
         s = StringIO(u"param S := 6; param X := 1 2;")
     a.load_file(s)
     assert a.T == 4
     assert a.S == 6
     assert a.X[1] == 2
コード例 #3
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
    def test_set(self):
        result = Amply("set month := Jan Feb Mar Apr;")['month']
        assert result == ['Jan', 'Feb', 'Mar', 'Apr']

        result = Amply("set month Jan Feb Mar Apr;")['month']
        assert result == ['Jan', 'Feb', 'Mar', 'Apr']
        assert [i for i in result] == ['Jan', 'Feb', 'Mar', 'Apr']
        assert result != []

        assert 'Jan' in result
        assert 'Foo' not in result
        assert len(result) == 4
コード例 #4
0
def read_in_datafile(path_to_datafile: str, config: Dict) -> Amply:
    """Read in a datafile using the Amply parsing class

    Arguments
    ---------
    path_to_datafile: str
    config: Dict
    """
    parameter_definitions = load_parameter_definitions(config)
    datafile_parser = Amply(parameter_definitions)
    logger.debug(datafile_parser)

    with open(path_to_datafile, "r") as datafile:
        datafile_parser.load_file(datafile)

    return datafile_parser
コード例 #5
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
 def test_set_splice(self):
     result = Amply("""
         set A dimen 3;
         set A := (1, 2, 3), (1, 1, *) 2 4 (3, *, *) 1 1;
         """)
     a = result.A
     assert a == [(1, 2, 3), (1, 1, 2), (1, 1, 4), (3, 1, 1)]
コード例 #6
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
    def test_2d_param_transpose(self):
        result = Amply("""
            param demand {location, item};
            param demand default 42 (tr)
                :   FRA DET LAN :=
            spoons  200 . 30
            plates  30  120 .
            cups    . .  29 ;
            """)['demand']

        self.assertEqual(result['FRA'], {
            'spoons': 200,
            'plates': 30,
            'cups': 42
        })
        self.assertEqual(result['DET'], {
            'spoons': 42,
            'plates': 120,
            'cups': 42
        })
        self.assertEqual(result['LAN'], {
            'spoons': 30,
            'plates': 42,
            'cups': 29
        })
コード例 #7
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
    def test_2d_param(self):
        result = Amply("""
            param demand {item, location};
            param demand
                :   FRA DET LAN :=
            spoons  200 100 30
            plates  30  120 90
            cups    666 13  29 ;
            """)['demand']

        options = [('spoons', {
            'FRA': 200,
            'DET': 100,
            'LAN': 30
        }), ('plates', {
            'FRA': 30,
            'DET': 120,
            'LAN': 90
        }), ('cups', {
            'FRA': 666,
            'DET': 13,
            'LAN': 29
        })]
        for name, _dict in options:
            self.assertDictEqual(result[name], _dict)
コード例 #8
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
    def test_2d_param_defaults(self):
        result = Amply("""
            param demand {item, location};
            param demand default 42
                :   FRA DET LAN :=
            spoons  200 . 30
            plates  30  120 .
            cups    . .  29 ;
            """)['demand']

        options = [('spoons', {
            'FRA': 200,
            'DET': 42,
            'LAN': 30
        }), ('plates', {
            'FRA': 30,
            'DET': 120,
            'LAN': 42
        }), ('cups', {
            'FRA': 42,
            'DET': 42,
            'LAN': 29
        })]
        for name, _dict in options:
            self.assertDictEqual(result[name], _dict)
コード例 #9
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
 def test_empty_parameter_statement(self):
     result = Amply("""
         param square {x};
         param square default 99 :=
         ;
         """)
     assert 'square' in result.symbols.keys()
     assert result.square == {}
コード例 #10
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
 def test_param_default(self):
     result = Amply("""
         param foo {s} default 3;
         param foo := Jan 1 Feb 2 Mar 3;
         """)
     options = [('Jan', 1), ('Mar', 3), ('FOO', 3)]
     for name, value in options:
         self.assertEqual(result['foo'][name], value)
コード例 #11
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
 def test_2d_slice1(self):
     result = Amply("""
         param demand {location, item};
         param demand :=
             [Jan, *] Foo 1 Bar 2;
         """)['demand']
     f = result['Jan']['Foo']
     assert f == 1
     assert result['Jan']['Bar'] == 2
コード例 #12
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
 def test_sub2_params(self):
     result = Amply("""
         param foo {s, t};
         param foo := 1 2 Hi 99 3 4;
         """)
     h = result['foo'][1][2]
     assert h == 'Hi'
     f = result['foo'][99][3]
     assert f == 4
コード例 #13
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
 def test_param_undefined(self):
     result = Amply("""
         param foo {s} ;
         param foo := Jan 1 Feb 2 Mar 3;
         """)
     j = result['foo']['Jan']
     assert j == 1
     with self.assertRaises(KeyError):
         a = result['foo']['Apr']
コード例 #14
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
 def test_sub1_params(self):
     result = Amply("""
         param foo {s};
         param foo := 1 Jan 2 Feb 3 Mar;
         """)
     j = result['foo'][1]
     assert j == 'Jan'
     f = result['foo'][2]
     assert f == 'Feb'
コード例 #15
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
 def test_undefined_tabbing_param(self):
     a = """
         param cost{elem};
         param : cost value :=
         0       1   2
         3       4   5
         ;
         """
     self.assertRaises(AmplyError, lambda: Amply(a))
コード例 #16
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
 def test_set_matrix(self):
     result = Amply("""
         set A : 1 2 3 :=
             1   + - -
             2   + + -
             3   - + -
         ;
         """)
     a = result.A
     assert a == [(1, 1), (2, 1), (2, 2), (3, 2)]
コード例 #17
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
 def test_set_subscript2_tuples(self):
     result = Amply("""
         set days{months, days};
         set days[Jan, 3] := 1 2 3 4;
         set days[Feb, 'Ham '] := 5 6 7 8;
         """)['days']
     j = result['Jan', 3]
     assert j == [1, 2, 3, 4]
     f = result['Feb', 'Ham ']
     assert f == [5, 6, 7, 8]
コード例 #18
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
 def test_set_subscript(self):
     result = Amply("""
         set days{months};
         set days[Jan] := 1 2 3 4;
         set days[Feb] := 5 6 7 8;
         """)['days']
     j = result['Jan']
     assert j == [1, 2, 3, 4]
     f = result['Feb']
     assert f == [5, 6, 7, 8]
コード例 #19
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
 def test_set_matrix_tr(self):
     result = Amply("""
         set A (tr) : 1 2 3 :=
                  1   + - -
                  2   + + -
                  3   - + -
         ;
         """)
     a = result.A
     assert a == [(1, 1), (1, 2), (2, 2), (2, 3)]
コード例 #20
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
    def test_empty_tabbing_parameters(self):

        result = Amply("""
            set x;
            param square {x};
            param triangle {x};
            param default 99 : square triangle :=
            ;
            """)
        assert 'square' in result.symbols.keys()
        assert result.square == {}
コード例 #21
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
 def test_2d_numeric_param(self):
     result = Amply("""
         param square {x, y};
         param square : 1 2 :=
             4       4   8
             3       3   6
         ;
         """)['square']
     f = result[4, 1]
     assert f == 4
     assert result[4, 2] == 8
     assert result[3, 1] == 3
     assert result[3, 2] == 6
コード例 #22
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
    def test_set2_tabbing(self):
        result = Amply("""
            set elem dimen 2;
            set elem := 0 0 1 1 2 2;
            param cost{elem};
            param value{elem};
            param : cost value :=
            0 0     7   25
            1 1     35  3
            ;
            """)

        assert result['elem'] == [(0, 0), (1, 1), (2, 2)]
コード例 #23
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
    def test_tuple_param(self):
        result = Amply("""
            set elem dimen 2;
            param foo{elem};
            param foo :=
                1   2   3
                2   3   4
                3   4   5
            ;
            """)['foo']

        f = result[1, 2]
        assert f == 3
        assert result[2, 3] == 4
        assert result[3, 4] == 5
コード例 #24
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
    def test_2dset_simpleparam(self):
        result = Amply("""
            set elem dimen 2;
            param foo{elem};
            param foo :=
                1   2   3
                2   3   4
                3   4   5
            ;
            """)['foo']

        f = result[1][2]
        assert f == 3
        assert result[2][3] == 4
        assert result[3][4] == 5
コード例 #25
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
 def test_single_tabbing_data_with_set(self):
     result = Amply("""
         set elem;
         param init_stock{elem};
         param cost{elem};
         param value{elem};
         param : elem : init_stock  cost    value :=
         iron    7           25      1
         nickel  35          3       2
         ;
         """)
     s = result['init_stock']
     assert s == {'iron': 7, 'nickel': 35}
     assert result['cost'] == {'iron': 25, 'nickel': 3}
     assert result['value'] == {'iron': 1, 'nickel': 2}
コード例 #26
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
 def test_set_splice_matrix(self):
     result = Amply("""
         set A dimen 3;
         set A (1, *, *) : 1 2 3 :=
                     1     + - -
                     2     + - +
                     3     - - -
               (2, *, *) : 1 2 3 :=
                     1     + - +
                     2     - + -
                     3     - - +
         ;
         """)
     a = result.A
     assert a == [(1, 1, 1), (1, 2, 1), (1, 2, 3), (2, 1, 1), (2, 1, 3),
                  (2, 2, 2), (2, 3, 3)]
コード例 #27
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
    def test_2tables(self):
        result = Amply("""
            param demand {item, location};
            param demand default 42
                :   FRA DET LAN :=
            spoons  200 . 30
            plates  30  120 .
            cups    . .  29
            ;

            param square {foo, foo};
            param square
                :   A   B :=
            A       1   6
            B       6   36
            ;
            """)
        demand = result['demand']
        options = [('spoons', {
            'FRA': 200,
            'DET': 42,
            'LAN': 30
        }), ('plates', {
            'FRA': 30,
            'DET': 120,
            'LAN': 42
        }), ('cups', {
            'FRA': 42,
            'DET': 42,
            'LAN': 29
        })]
        for name, _dict in options:
            self.assertDictEqual(demand[name], _dict)

        square = result['square']
        options = [
            ('A', {
                'A': 1,
                'B': 6
            }),
            ('B', {
                'A': 6,
                'B': 36
            }),
        ]
        for name, _dict in options:
            self.assertDictEqual(square[name], _dict)
コード例 #28
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
 def test_high_dim_tabbing(self):
     result = Amply("""
         set x;
         set y;
         param square {x,y};
         param default 99 : square :=
         a a 34
         a b 35
         a c 36
         b a 53
         b b 45.3
         b c 459.2
         ;
         """)
     assert 'square' in result.symbols.keys()
     print(result.square['b'])
     assert result.square['a'] == {'a': 34.0, 'b': 35.0, 'c': 36.0}
     assert result.square['b'] == {'a': 53.0, 'b': 45.3, 'c': 459.2}
コード例 #29
0
def get_data(file_path):
    """
    Get the data contained in the Ampl input file
    :param file_path: the file_path of the input file
    :return: An object that contains the data contained in the input file
    """

    # I put in file_content the content of the file
    with open(file_path, 'r') as ampl_file:
        file_content = ampl_file.read()

    # Now I use the variable temp to transform the file content in order to be compatible with amply
    first_row = file_content.split('\n', 1)[0]

    if first_row == "data;":
        # I remove the first line in order to remove "data;"
        temp = file_content.split('\n', 1)[1]
    else:
        temp = file_content

    aux_str1 = """param coordX{random_text};
    param coordX"""

    aux_str2 = """param coordY{random_text};
    param coordY"""

    aux_str3 = """param d{random_text};
    param d"""

    # Now I use the three auxiliary strings that I declared above to modify temp

    temp = temp.split('param coordX', 1)
    temp = temp[0] + aux_str1 + temp[1]

    temp = temp.split('param coordY', 1)
    temp = temp[0] + aux_str2 + temp[1]

    temp = temp.split('param d', 1)
    temp = temp[0] + aux_str3 + temp[1]

    # Now temp is compatible with Amply
    return Amply(temp)
コード例 #30
0
ファイル: test_amply.py プロジェクト: pchtsp/pulp-or
    def test_3d_slice2b(self):
        result = Amply("""
            param trans_cost{src, product, dest};
            param trans_cost :=
                [*,bands,*]: FRA DET LAN :=
                    GARY     30  10  8
                    CLEV     22  7   10
                [*,coils,*]: FRA DET LAN :=
                    GARY     39  14  11
                    CLEV     27  9   12
                [*,plate,*]: FRA DET LAN :=
                    GARY     41  15  12
                    CLEV     29  9   13
            ;
            """)['trans_cost']

        f = result['GARY']['bands']['FRA']
        assert f == 30
        assert result['GARY']['plate']['DET'] == 15
        assert result['CLEV']['coils']['LAN'] == 12
コード例 #31
0
ファイル: f_t_parser_ff.py プロジェクト: chenleji/ryu-1
def parse_ampl_results(filename='results.txt'):

    data = Amply("""
    set PrimaryPath{REQUESTS};
    set DetourPath{NODES, NODES, REQUESTS};
    param DetectNode{NODES, NODES, REQUESTS};
    """)

    data.load_file(open(filename))

    requests = dict()
    faults = dict()
    stats = dict()
    pp_edges = dict()

    print "Parsing requests..."
    for i in data.PrimaryPath:
        rid = int(i)
        pp = [int(x) for x in data.PrimaryPath[rid]]
        pp_edge = (pp[0], pp[-1])
        requests[pp_edge] = {
            'pp_edge': pp_edge,
            'primary_path': pp,
            'faults': dict()}
        pp_edges[rid] = pp_edge
        #print rid, pp_edge, pp

    for i in data.DetectNode.data:

        na = int(i)
        val_na = data.DetectNode.data[na]

        for j in val_na:

            nb = int(j)
            val_nb = val_na[j]

            # if (nb > na):
            #     continue
            # if (nb == 0):
            #     fault_type = 'node'
            #     fid = "N-" + str(na)
            # else:
            #     fault_type = 'link'
            #     fid = "L-" + str(na) + "-" + str(nb)

            for d in val_nb:
                rid = int(d)

                if na < nb:
                    fault_edge = (na, nb)
                else:
                    fault_edge = (nb, na)

                pp_edge = pp_edges[rid]
                pp = requests[pp_edge]['primary_path']

                detect_node = int(val_nb[rid])
                dp = [int(x) for x in data.DetourPath.data[na][nb][rid]]
                redirect_node = dp[0]

                # Fw back path is the sequence of node from detect to redirect node (included)
                idx_d = pp.index(detect_node)
                idx_r = pp.index(redirect_node)
                if(idx_d - idx_r == 0):
                    fw_back_path = None
                else:
                    fw_back_path = pp[idx_r:idx_d + 1]

                fault = {'detect_node': detect_node,
                         'redirect_node': redirect_node,
                         'detour_path': dp,
                         'fw_back_path': fw_back_path}
                # For each request, we populate the corresponding faults...
                requests[pp_edge]['faults'][fault_edge] = fault

                # And viceversa, for each fault, we populate the requests...
                if fault_edge not in faults:
                    faults[fault_edge] = {
                        'requests': {}}

                faults[fault_edge]['requests'][pp_edge] = {
                    'primary_path': pp,
                    'detect_node': detect_node,
                    'redirect_node': redirect_node,
                    'detour_path': dp,
                    'fw_back_path': fw_back_path}

    with open('./tmp/' + hh + '-requests.p', 'wb') as fp:
        pickle.dump(requests, fp)
    with open('./tmp/' + hh + '-faults.p', 'wb') as fp:
        pickle.dump(faults, fp)

    return requests, faults