Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.7 kB
8
Indexable
Never
import pandas as pd

def johnsons_rule(df_jobs):
    # Convert processing times to numeric values
    df_jobs['Machine 1'] = pd.to_numeric(df_jobs['Machine 1'])
    df_jobs['Machine 2'] = pd.to_numeric(df_jobs['Machine 2'])
    
    # Initialize lists to store jobs for each machine
    machine_1_jobs = []
    machine_2_jobs = []
    
    # Separate jobs based on the processing time on each machine
    for job, row in df_jobs.iterrows():
        if row['Machine 1'] < row['Machine 2']:
            machine_1_jobs.append((job, row['Machine 1'], row['Machine 2']))
        else:
            machine_2_jobs.append((job, row['Machine 1'], row['Machine 2']))
    
    # Sort jobs based on processing time on Machine 1 in ascending order
    machine_1_jobs.sort(key=lambda x: x[1])
    
    # Sort jobs based on processing time on Machine 2 in descending order
    machine_2_jobs.sort(key=lambda x: x[2], reverse=True)
    
    # Combine jobs from both machines based on their sorted order
    sequence = []
    while machine_1_jobs and machine_2_jobs:
        if machine_1_jobs[0][1] < machine_2_jobs[0][2]:
            sequence.append(machine_1_jobs[0][0])
            machine_1_jobs.pop(0)
        else:
            sequence.append(machine_2_jobs[0][0])
            machine_2_jobs.pop(0)
    
    # Add remaining jobs
    sequence.extend([job[0] for job in machine_1_jobs])
    sequence.extend([job[0] for job in machine_2_jobs])
    
    return sequence

# Example DataFrame
jobs_df = pd.DataFrame({'Machine 1': [3, 4, 2], 'Machine 2': [2, 5, 4]}, index=['Job1', 'Job2', 'Job3'])

# Call the function and print the output
print(johnsons_rule(jobs_df))


Leave a Comment