我想分配 x(i)
个对象 (x E {1...n})
,每个对象的重量为 w(i)
,分配到 n
个部分中。
分配应该这样做:对于所有部分,它们的重量总和尽可能相等。
干杯! Pratik
我想分配 x(i)
个对象 (x E {1...n})
,每个对象的重量为 w(i)
,分配到 n
个部分中。
分配应该这样做:对于所有部分,它们的重量总和尽可能相等。
干杯! Pratik
计算所有重量的总和,然后除以n,即份数,得到所需的每份重量。然后使用装箱问题算法尝试填充n个最大尺寸的箱子。
请注意,为使此方法有效,所有物品的重量都必须小于每份重量。否则,您无法在任何地方放置大重量物品。
"""
Solves the Multiprocessor Scheduling problem using the Longest Processing Time algorithm
PROBLEM: (NP-hard)
Given:
- set of jobs, each with a length
- a number of processors
Find:
- divide the jobs among the processors such that none overlap
which minimizes the total processing time
ALGORITHM:
- sort the jobs by processing time
- assign them to the machine with the earliest end time so far
Achieves an upper bound of 4/3 - 1/(3m) of optimal
RETURNS:
assignments, ith index → machine for the ith job
"""
function multiprocessor_scheduling_longest_processing_time{R<:Real}(
jobs::AbstractVector{R},
m::Integer, # number of processors
)
durations = zeros(R, m)
assignments = Array(Int, length(jobs))
for i in sortperm(jobs, rev=true) # p[1] is the index of the longest job in `jobs`
best_index = indmin(durations)
durations[best_index] += jobs[i]
assignments[i] = best_index
end
assignments
end
如果您使用优先队列,可能会更好。