• 大小: 13KB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2021-06-06
  • 语言: Python
  • 标签: python  cplex  

资源简介

python+cplex进行网络规划,代码简单清晰,适合初学者学习,修改参数后可直接使用

资源截图

代码片段和文件信息

import subprocess
“““
Cosc364 Assignment 2: Flow Planning
Jessica Slaney Harrigan Davenport
31/05/2017
“““

def create_cplex_string(paths source_links dest_links u_num):
    “““Creates the text for a minimization problem solvable by CPLEX.
    The returned string is formatted to be inserted into a blank
    LP file such that it is readable by CPLEX.“““

    # Initialize all strings so they can added to in iterations
    demand_flow_string = “\n“ # The demand flow over each path
    equal_share_of_flow_string = “Equal Flow:\n“ # Constraints such that the flow is equal over paths used in parallel
    capacities_string = “Capacities:\n“ # Constraints such that the flow is restricted to capacity per link
    bounds_string = ““ # Bounds such that variables are non negative
    binary_string = ““ # Those variables which should be restricted to {0 1}
    path_number_string = “Number of Flows per Route:\n“ # There are a specific number of paths used to get to a destination
    load_balance_string = “Balance That Load!:\n“ # Constraints such that the load is balanced on transit nodes using auxiliary variable r

    for path in paths.keys():
        flow_string = ““ # temporary variables to be iterated over and added to
        path_string = ““

        for variable demand_volume in paths[path]: # For every decision variable required for this path
            u = “u“ + ““.join(list(variable)[1:])  # Describes whether the path is used or not
            flow_string += “{} + “.format(variable)
            path_string += “{} + “.format(u)

            binary_string += “{} “.format(u)
            equal_share_of_flow_string += “{} {} - {} {} = 0\n“.format(u_num variable demand_volume u)
            bounds_string += “{} >= 0\n“.format(variable)
            load_balance_string += “{} - r <= 0\n“.format(variable)

        # Add decision variable constraints/bounds created so far to their relevant strings
        flow_string = ““.join(list(flow_string)[:-2]) + “= {}\n“.format(demand_volume)
        demand_flow_string += flow_string
        path_string = ““.join(list(path_string)[:-3]) + “ = {}\n“.format(u_num)
        path_number_string += path_string

    # Restrain the decision variable values in terms of link capacity from source to transit node
    for c in source_links.keys():
        cap_string = ““
        for variable in source_links[c]:
            cap_string += “{} + “.format(variable)
        cap_string = ““.join(list(cap_string)[:-2]) + “- {} <= 0\n“.format(c)
        capacities_string += cap_string
        bounds_string += “{} >= 0\n“.format(c)

    # Restrain the decision variable values in terms of link capacity from transit to destination node
    for d in dest_links.keys():
        cap_string = ““
        for variable in dest_links[d]:
            cap_string += “{} + “.format(variable)
        cap_string = ““.join(list(cap_string)[:-2]) + “- {} <= 0\n“.format(d)
        capacities_string += cap_string
        bounds_string += “{} >= 0\n“.f

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-08-06 14:50  networing_assignment_route_planning-master\
     文件        6481  2017-05-31 09:11  networing_assignment_route_planning-master\cosc364_ass_2.py
     文件        3802  2018-08-06 14:50  networing_assignment_route_planning-master\cplex.log
     目录           0  2018-06-04 19:49  networing_assignment_route_planning-master\cplexPy-master\
     文件         203  2018-01-03 05:29  networing_assignment_route_planning-master\cplexPy-master\README.md
     目录           0  2018-06-04 19:49  networing_assignment_route_planning-master\cplexPy-master\lshaped\
     目录           0  2018-06-04 19:49  networing_assignment_route_planning-master\cplexPy-master\lshaped\farmer\
     文件          86  2018-01-03 05:29  networing_assignment_route_planning-master\cplexPy-master\lshaped\farmer\master.lp
     文件          17  2018-01-03 05:29  networing_assignment_route_planning-master\cplexPy-master\lshaped\farmer\scenario.dat
     文件         188  2018-01-03 05:29  networing_assignment_route_planning-master\cplexPy-master\lshaped\farmer\subproblem1.lp
     文件         188  2018-01-03 05:29  networing_assignment_route_planning-master\cplexPy-master\lshaped\farmer\subproblem2.lp
     文件         188  2018-01-03 05:29  networing_assignment_route_planning-master\cplexPy-master\lshaped\farmer\subproblem3.lp
     文件       13569  2018-01-03 05:29  networing_assignment_route_planning-master\cplexPy-master\lshaped\lshaped.py
     文件        1887  2018-01-03 05:29  networing_assignment_route_planning-master\cplexPy-master\lshaped\main.py
     文件          73  2018-01-03 05:29  networing_assignment_route_planning-master\cplexPy-master\lshaped\master.lp
     文件          23  2018-01-03 05:29  networing_assignment_route_planning-master\cplexPy-master\lshaped\scenario.dat
     文件         188  2018-01-03 05:29  networing_assignment_route_planning-master\cplexPy-master\lshaped\subproblem1.lp
     文件         188  2018-01-03 05:29  networing_assignment_route_planning-master\cplexPy-master\lshaped\subproblem2.lp
     文件         188  2018-01-03 05:29  networing_assignment_route_planning-master\cplexPy-master\lshaped\subproblem3.lp
     文件         188  2018-01-03 05:29  networing_assignment_route_planning-master\cplexPy-master\lshaped\subproblem4.lp
     文件        4138  2018-08-06 14:50  networing_assignment_route_planning-master\output.txt
     文件        2709  2018-08-06 14:50  networing_assignment_route_planning-master\tm.lp

评论

共有 条评论