对于我的硕士论文,我坚持使用“简单”模型。我想将向量分解为块,其附加值应尽可能接近。我编写了一个目标函数,该函数采用大向量和索引向量,并为我的块计算标准偏差。 然后,我将其提供给scipy的最小化例程,但我不知道是否(然后如何)可以告诉最小化,我的价值观必须提升(我努力地将其保持在给定范围内),因为它们是索引。
优化前的标准偏差为39,之后得到1882。我认为我不太了解此最小化器的工作原理。
由于我没有听过任何有关离散优化的讲座,所以我不知道如何正确解决此问题。是否有一种算法可以最小化从N ^ n到R的函数?我希望得到一些建议,希望可以在此处发布我的代码。
import numpy as np
import statistics as stat
from scipy.optimize import minimize
# define objective function to minimize
# returns standard deviation of the distributed computational effort
def distribution_deviation(cell_list_vec, index_vec):
# initialize computational loads for every chunk
loads = np.zeros(index_vec.size + 1)
index_vec = np.insert(index_vec, 0, 0)
index_vec = np.append(index_vec, cell_list_vec.size - 1)
index_vec = index_vec.astype(int)
for i in range(index_vec.size - 1):
loads[i] = np.sum(cell_list_vec[index_vec[i]:index_vec[i+1]])
return stat.stdev(loads)
np.random.seed(123) # set random seed for reproducibility
# initialize array with random numbers between 0 and 100 (representing cells and their computational cost)
n_cells = 100 # number of cells
n_chunks = 5 # number of chunks/ processors
cell_list = np.random.uniform(low=0, high=100, size=n_cells)
# index vector for chunks
index = np.linspace(start=0, stop=cell_list.size, num=n_chunks + 1) # linear spaced
index = index[1:n_chunks] # remove endpoints
index = index.astype(int) # cast to integer
# test objective function
dev = distribution_deviation(cell_list, index)
print("Index vector before optimization (without endpoints):")
print(index)
print("Inter chunk standard deviation: " + str(dev))
print("Start optimization...")
# use optimizer here
# initialize bounds
bound_sequence = []
for i in range(index.size):
bound_sequence += [(0, cell_list.size)]
# these can handle bounds:
# L-BFGS-B, TNC, SLSQP
index_res = minimize(distribution_deviation, x0=index, args=cell_list, method='SLSQP', bounds=bound_sequence)
# get array from solution object
index_res = index_res.x
# construct index vector
index_res = np.insert(index_res, 0, 0)
index_res = np.append(index_res, cell_list.size - 1)
index_res = index_res.astype(int)
print("Index vector after optimization:")
print(index_res) # cast result
dev = distribution_deviation(cell_list, index_res)
print("Inter chunk deviation after optimization: " + str(dev))