Excel 模型的单目标多整数变量优化 - Python Pymoo 库

问题描述 投票:0回答:1

我正在尝试使用 Pymoo 优化大型 Excel 模型,我已经设置了通过 pyWin32 与 Excel 进行通信的问题。

在 VSCode 中调试时出现以下异常:

Exception
('Problem Error: F can not be set, expected shape (100, 1) but provided (1, 1)', ValueError('cannot reshape array of size 1 into shape (100,1)'))
ValueError: cannot reshape array of size 1 into shape (100,1)

During handling of the above exception, another exception occurred:

  File "C:\Database\Python\RSG\RSG Opt.py", line 71, in <module>
    res = minimize(
          ^^^^^^^^^
Exception: ('Problem Error: F can not be set, expected shape (100, 1) but provided (1, 1)', ValueError('cannot reshape array of size 1 into shape (100,1)'))

我正在尝试优化单个目标,所以我不知道为什么它需要一个包含 100 个值的数组。我绝对不会将变量设置为 100。Pymoo 是否需要一个数组来保存第一个总体 100 的所有 100 个评估?

完全意识到我的代码可能存在其他问题,可能错误地设置了数组大小,因此完整的代码在这里:

import win32com.client as win32
import numpy as np
from pymoo.core.problem import Problem
from pymoo.algorithms.soo.nonconvex.ga import GA
from pymoo.optimize import minimize

# Connect to Excel
excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.Visible = True  # Keep Excel hidden

# Reference the active workbook (assuming it's already open)
workbook = excel.ActiveWorkbook

# Prompt for the sheet name and range
sheet_name = input("Enter the sheet name: ")
range_address = input("Enter the variable range address (e.g., A1:B10): ")
target = input("Enter the objective cell address (e.g., A1:B10): ")

# Reference the specified sheet and range
try:
    worksheet = workbook.Sheets(sheet_name)
    variable_range = worksheet.Range(range_address)
    objective_cell = worksheet.Range(target)

except Exception as e:
    print(f"Error: {e}")
    excel.Quit()
    quit()

# Read the number of variables based on the number of rows in the range
num_vars = variable_range.Rows.Count
Vars=np.array(variable_range.Value)
trans_vars = np.transpose(Vars)

# Read the lower bounds from Excel (assuming they are in column -2)
lower_bounds_range = variable_range.GetOffset(0,-2)
lower_bounds = [int(cell.Value) for cell in lower_bounds_range]

# Read the upper bounds from Excel (assuming they are in column -1)
upper_bounds_range = variable_range.GetOffset(0, -1)
upper_bounds = [int(cell.Value) for cell in upper_bounds_range]


# Define the Optimization Problem
class ExcelOptimizationProblem(Problem):
    def __init__(self, num_vars, lower_bounds, upper_bounds):
        super().__init__(n_var=num_vars, n_obj=1, n_constr=1, xl=lower_bounds, xu=upper_bounds, type_var=int)

    def _evaluate(self, x, out, *args, **kwargs):
        # Set the decision variable values in Excel
        for i, val in enumerate(x):
            variable_range.Cells(1,1).GetOffset(i, 0).Value = val

        excel.Calculate()

        objective_value = float(objective_cell.Value)

        constraint_value = 0

        out["F"] = [-objective_value]
        out["G"] = [-constraint_value]

problem = ExcelOptimizationProblem(
    num_vars, lower_bounds, upper_bounds
    )

algorithm = GA(
    pop_size=100,
    eliminate_duplicates=True)

res = minimize(
    problem, algorithm, ('n_gen', 10), verbose=True
    )

print("Best solution found: \nX = %s\nF = %s" % (res.X, res.F))

workbook.Close()
excel.Quit()

有人可以给我一些指导吗?

提前致谢。

python genetic-algorithm pywin32 pymoo
1个回答
0
投票

你的变量数量

n_vars
与上下限的长度一致吗? 错误到底是在哪一行代码中产生的?

© www.soinside.com 2019 - 2024. All rights reserved.