我正在编写一种优化算法,该算法使用几种不同的初始条件来增加查找全局最佳最佳的机会。我正在尝试通过使用多处理库使代码运行速度更快,并在不同的过程上运行优化。
这是我的代码现在基本工作的方式:
from multiprocessing import Process, Queue
from SupportCostModel.SupportStructure import SupportStructure, SupportType
# Method the processes will execute
def optimizeAlgoritm(optimizeObject, qOut):
optimizeObject.Optimize()
qOut.put(optimizeObject)
# Method the main thread will execute
def getOptimumalObject(n):
for i in range(n):
# Create a new process with a new nested object that should be optimized
p = Process(target = optimizeAlgoritm, args = (SupportStructure(SupportType.Monopile), qOut))
processes.append(p)
p.deamon = True
p.start()
# Part the main thread is running
if __name__ == '__main__':
qOut = Queue()
processes = []
# Run the code on 6 processes
getOptimumalObject(6)
for i in range(len(processes)):
processes[i].join()
# Get the best optimized object and print the resulting value
minimum = 1000000000000000000000000.
while not qOut.empty():
optimizeObject = qOut.get()
if optimizeObject.GetTotalMass() < minimum:
bestObject = optimizeObject
minumum = optimizeObject.GetTotalMass()
print(bestObject.GetTotalMass())
只要我只使用4个进程,此代码就可以使用。如果我运行超过4个,例如6,则在示例中,两个进程将陷入代码末尾,并且代码将永远不会停止运行,因为主线程仍然卡在processes[i].join()
上。我认为这两个过程在OptimizealGorithM中的qOut.put()
中存在问题。当我删除qOut.put()
时,代码退出给出了BestObject不存在的错误,如预期。但是,奇怪的是,如果我打印,例如,qOut.put()
之后的对象将打印出来,但是使用我的CPU的0%,该过程将保持生命。这也迫使主要代码也活着。
我是多处处理的新手,并且读到OOP和多处理并不总是能很好地搭配良好的效果。我在这里使用错误的方法吗?它几乎有效,但不适用于四个过程,这是令人沮丧的。
预先感谢!
我让它使用管道发送我的对象!
这是我使用的代码:
from multiprocessing import Process, Pipe
from SupportCostModel.SupportStructure import SupportStructure, SupportType
import random
# Method the processes will execute
def optimizeAlgoritm(optimizeObject, conn):
optimizeObject.Optimize()
# Send the optimized object
conn.send(optimizeObject)
# Method the main thread will execute
def getOptimumalObject(n):
connections = []
for i in range(n):
# Create a pipe for each of the processes that is started
parent_conn, child_conn = Pipe()
# Save the parent connections
connections.append(parent_conn)
# Create objects that needs to by optimized using different initial conditions
if i == 0:
structure = SupportStructure(SupportType.Monopile)
else:
structure = SupportStructure(SupportType.Monopile)
structure.properties.D_mp = random.randrange(4., 10.)
structure.properties.Dtrat_tower = random.randrange(90., 120.)
structure.properties.Dtrat_mud = random.randrange(60., 100.)
structure.properties.Dtrat_mp = random.randrange(60., 100.)
structure.UpdateAll()
# Create a new process with a new nested object that should be optimized
p = Process(target = optimizeAlgoritm, args = (structure, child_conn))
processes.append(p)
p.deamon = True
p.start()
# Receive the optimized objects
for i in range(n):
optimizedObjects.append(connections[i].recv())
# Part the main thread is running
if __name__ == '__main__':
processes = []
optimizedObjects = []
# Run the code on 6 processes
getOptimumalObject(6)
for i in range(len(processes)):
processes[i].join()
# Get the best optimized object and print the resulting value
minimum = 1000000000000000000000000.
for i in range(len(optimizedObjects)):
optimizeObject = optimizedObjects[i]
if optimizeObject.GetTotalMass() < minimum:
bestObject = optimizeObject
minumum = optimizeObject.GetTotalMass()
print(bestObject.GetTotalMass())