メインコンテンツへスキップ

Transfering a solution of binary variables into right order

回答済み

コメント

6件のコメント

  • 正式なコメント
    Simranjit Kaur
    • Gurobi Staff
    This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?.
  • Eli Towle
    • Gurobi Staff

    The code below creates a list \( \texttt{surgs} \) that contains the surgeries, ordered by precedence.

    # Extract indices (tuples) from sequencing variables equal to 1 in solution
    seq = [k for k, v in sequencing.items() if v.X > 0.5]

    # Determine starting surgery
    s1, s2 = zip(*seq)
    surgs = [a for a in s1 if a not in s2]

    # Iteratively add next sequential surgery
    while len(surgs) < len(seq) + 1:
        surgs += [b for a, b in seq if a == surgs[-1]]

    # Display results
    print(" --> ".join(surgs))

    What it does:

    1. Builds \( \texttt{seq} \), the list of tuples corresponding to \( \texttt{sequencing} \) variables equal to \( 1 \) in the optimal solution.
    2. Finds the first surgery, which is the first element of tuple in \( \texttt{seq} \) that is not the second element of any tuple in \( \texttt{seq} \).
    3. Iteratively adds the next surgery to \( \texttt{surgs} \). This is always the second element of a tuple in \( \texttt{seq} \) whose first element equals the last element of \( \texttt{surgs} \).
    1
  • Alice M.
    • Gurobi-versary
    • First Comment
    • First Question

    Hey Eli, 

    Thanks a lot! It's working :) 

    The explanation is great!

     

    0
  • Alice M.
    • Gurobi-versary
    • First Comment
    • First Question

    Hey Eli, 

    one more question. I try to add the start times into the sequence, but it's not really working. 

    The first surgery should start at time 0, the second at start_time 1, the third at start_time 1+2 and so on. 

    The result should look like this (if it is possible)

    surg3 [start: 0] --> surg5 [start: 10] --> surg4 [start: 40] --> surg1 [start: 61] --> surg2 [start: 121]

    My solution so far is: 

        Variable            x 
    -------------------------
    start_time[surg1]           60 
    start_time[surg2]           90 
    start_time[surg3]           10 
    start_time[surg4]           21 
    start_time[surg5]           30 
    sequencing[surg1,surg2]            1 
    sequencing[surg3,surg5]            1 
    sequencing[surg4,surg1]            1 
    sequencing[surg5,surg4]            1 
       tardiness           11 
    ____________________________________
    Minimal total costs: 44.0
    Optimal sequence of surgeries: surg3 --> surg5 --> surg4 --> surg1 --> surg2

    Best regards,

    Alice

    0
  • Eli Towle
    • Gurobi Staff

    We can build a list \( \texttt{starts} \) of the same length as list \( \texttt{surgs} \) that stores the cumulative elapsed time before each surgery starts:

    # Extract indices (tuples) from sequencing variables equal to 1 in solution
    seq = [k for k, v in sequencing.items() if v.X > 0.5]

    # Determine starting surgery
    s1, s2 = zip(*seq)
    surgs = [a for a in s1 if a not in s2]

    # Iteratively add next sequential surgery and track cumulative time
    starts = [0]
    while len(surgs) < len(seq) + 1:
        starts.append(starts[-1] + start_time[surgs[-1]].X)
        surgs += [b for a, b in seq if a == surgs[-1]]

    # Display results
    print(" --> ".join([f"{s} [start: {t}]" for s, t in zip(surgs, starts)]))

    The output:

    surg3 [start: 0] --> surg5 [start: 10.0] --> surg4 [start: 40.0] --> surg1 [start: 61.0] --> surg2 [start: 121.0]
    1
  • Alice M.
    • Gurobi-versary
    • First Comment
    • First Question

    Thank you for your help, Eli! 

    0

投稿コメントは受け付けていません。