Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ObjV不合法的问题 #333

Open
zzkzsm opened this issue Jul 9, 2023 · 2 comments
Open

ObjV不合法的问题 #333

zzkzsm opened this issue Jul 9, 2023 · 2 comments

Comments

@zzkzsm
Copy link

zzkzsm commented Jul 9, 2023

-- coding: utf-8 --

import numpy as np
import geatpy as ea

A = 2 # x方向有A个货位
B = 2 # y方向有B个货架
C = 3 # z方向有C层
L = 10 # 货位的长、宽、高
vx = 1 # 叉车的x速度
vy = 0.5 # 叉车的y速度
vz = 2 # 叉车的z速度
Num_jz = ABC

good_list = [[1, [1, 1, 1], 0.05], [2, [1, 1, 2], 0.03], [3, [1, 1, 3], 0.06]]

生成 x, y, z 坐标的矩阵

x, y, z = np.meshgrid(np.arange(A), np.arange(B), np.arange(C), indexing='ij')

将三维矩阵转换为一维数组并编号

xyz = np.column_stack((x.reshape(-1), y.reshape(-1), z.reshape(-1)))

class MyProblem(ea.Problem): # 继承Problem父类

def __init__(self):
    name = 'MyProblem'  # 初始化name(函数名称,可以随意设置)
    M = 1  # 初始化M(目标维数)
    maxormins = [1]  # 初始化maxormins(目标最小最大化标记列表,1:最小化该目标;-1:最大化该目标)
    Dim = len(good_list)  # 初始化Dim(决策变量维数)
    varTypes = [1] * Dim  # 初始化varTypes(决策变量的类型,元素为0表示对应的变量是连续的;1表示是离散的)
    lb = [0] * Dim  # 决策变量下界
    ub = [Num_jz] * Dim  # 决策变量上界
    lbin = [0] * Dim  # 决策变量下边界(0表示不包含该变量的下边界,1表示包含)
    ubin = [0] * Dim  # 决策变量上边界(0表示不包含该变量的上边界,1表示包含)
    # 调用父类构造方法完成实例化
    ea.Problem.__init__(self,
                        name,
                        M,
                        maxormins,
                        Dim,
                        varTypes,
                        lb,
                        ub,
                        lbin,
                        ubin)


def evalVars(self, pop):  # 目标函数
    x = pop.astype(int)
    t = x.shape[0]
    fx=[]
    for i in range(0, t):
        t1 = x[[i], :]
        for j in t1:
            f = 0
            for k in range(xyz.shape[0]):
                if np.array_equal(j, k):
                    f = f +((xyz[k][0]+xyz[k][0]//2)/vx+xyz[k][1]/vy+xyz[k][2]/vz)*L*good_list[j][2]
                    fx.append(f)
    fx = np.array(fx).reshape(-1, 1)
    ObjV = fx
    return ObjV

调用main,会报错:Objv不合法
怎么解决呢

@geatpy-dev
Copy link
Owner

《Geatpy数据结构》的文档有讲解ObjV要符合什么样的格式。

@zzkzsm
Copy link
Author

zzkzsm commented Jul 9, 2023

但是现在我的ObjV就是二维数组,不知道哪里有问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants