用ALNS算法求解并行机,3台并行机20个工作

问题遇到的现象和发生背景

img

问题相关代码,请勿粘贴截图
import copy

from functools import partial

import matplotlib.pyplot as plt
import numpy as np
import numpy.random as rnd

from alns import ALNS, State
from alns.criteria import HillClimbing
from alns.weight_schemes import SimpleWeights

%matplotlib inline
SEED = 5432

with open('单阶段同质并行机.csp') as file:
    data = file.readlines()

NUM_JOBS = int(data[0])
                     
BEAMS = [int(length)
         for datum in data[-NUM_JOBS:]                            
         for amount,length in [datum.strip().split()]]                              
print("所有工作的总量:", sum(BEAMS))           
print(BEAMS)
print(NUM_JOBS)
machine1=[]
machine2=[]
machine3=[]
machine=[machine1,machine2,machine3]
workingtime=[]
workingtime = sorted(BEAMS,reverse=True)

class CspState(State):
    
    def __init__(self,worktime,machine1,machine2,machine3):                           
        self.worktime = worktime
        self.machine1 = machine1
        self.machine2 = machine2
        self.machine3 = machine3
   
          
    def copy(self):
        return CspState(copy.deepcopy(self.worktime),copy.deepcopy(self.machine1),copy.deepcopy(self.machine2),copy.deepcopy(self.machine3))
    
    
    def objective(self):
        a=max(np.sum(machine1),np.sum(machine2),np.sum(machine3))
        return a

degree_of_destruction = 0.25

def works_to_remove(num_works):
    return int(num_works * degree_of_destruction) 
def random_removal(state, random_state):
  
    state = state.copy()

    for _ in range(works_to_remove(state.objective())):                # 有几个是用完的
        idx = random_state.randint(state.objective())
        state.workingtime.extend(state.assignments.pop(idx))

    return state
def greedy_insert(state, random_state):
    
    machine1.append(workingtime.pop(0))
    print(machine1)
    machine2.append(workingtime.pop(0))
    print(machine2)
    machine3.append(workingtime.pop(0))
    print(machine3)
    WORKINGTIME = copy.deepcopy(workingtime) 
    print(WORKINGTIME)
    print(workingtime)   
    print (np.sum(machine1))
    
    for i in WORKINGTIME:
        if np.sum(machine1)<=np.sum(machine2) and np.sum(machine1)<=np.sum(machine3):
            machine1.append(workingtime.pop(0))
        elif np.sum(machine3)<=np.sum(machine2) and np.sum(machine3)<=np.sum(machine1):
            machine3.append(workingtime.pop(0))
        elif np.sum(machine2)<=np.sum(machine3) and np.sum(machine2)<=np.sum(machine1):
            machine2.append(workingtime.pop(0))

    return state

rnd_state = rnd.RandomState(SEED)

state = CspState([],machine1,machine2,machine3)
init_sol = greedy_insert(state, rnd_state)

print("Initial solution has objective value:", init_sol.objective())
print(machine1)
print(machine2)
print(machine3)


alns = ALNS(rnd_state)
alns.add_destroy_operator(random_removal)
alns.add_repair_operator(greedy_insert)

criterion = HillClimbing()
weights = SimpleWeights([3, 2, 1, 0.5], 2, 2, 0.8)                            # 使用样本权重

result = alns.iterate(init_sol, weights, criterion, iterations=5000)
solution = result.best_state
objective = solution.objective()
print("Heuristic solution has objective value:", solution.objective())
运行结果及报错内容

AttributeError: 'CspState' object has no attribute 'workingtime'

我的解答思路和尝试过的方法

就完全不知道应该怎么做,但是总体思路是20个随机分组,根据分组来让总和最大的那组最小

我想要达到的结果