如何在另一个类的实例中存储一个类的实例的变量

时间:2019-03-27 21:10:03

标签: python class gurobi

我有两个自定义类,一个是gurobipy类的子类,应该创建lp模型。我制作的另一个用于存储变量。现在,我想将模型类的一些变量存储在变量类中。 这是我的课程:

class Model(gb.Model):
    def __init__(self):
        super().__init__()

    def create_model(self, var):
        dim = var.dimensions()
        # variables
        x = self.addVars(dim[0], dim[1], vtype=gb.GRB.BINARY, name="x")
        D_l = self.addVars(dim[1], lb=0, name='D_l')
        D_max = self.addVar(lb=0, name='D_max')
        # objective
        self.setObjective(D_max, gb.GRB.MINIMIZE)
        # constraints
        self.addConstrs((x.sum(i, '*') == 1 for i in range(dim[0])), name="b")
        self.addConstrs((D_max >= D_l[l] for l in range(dim[1])), name="c")
        self.addConstrs((D_l[l] >= var.dist_mat()[i, j] * (x[i, l] + x[j, l] - 1) for i in range(dim[0])
                          for j in range(dim[0]) for l in range(dim[1])), name='a')
        self.update()


class Variables:
    def __init__(self, data, number_of_clusters, neighbourhood_size):
        self.data = data
        self.number_of_clusters = number_of_clusters
        self.neighbourhood_size = neighbourhood_size
        self.variables_before = None
        self.variables_now = None
        self.ofv_before = None
        self.ofv_now = None
        self.x = None

    def dist_mat(self):
        from scipy.spatial import distance_matrix
        return distance_matrix(self.data, self.data)

    def dimensions(self):
        from numpy import shape
        data_objects = shape(self.data)[0]
        number_of_clusters = self.number_of_clusters
        return data_objects, number_of_clusters

    def print_dist_mat(self):
        print(self.dist_mat())

这是我要存储的x变量。首先,我尝试将其存储在Model类的实例中。我在这行self.x = None中添加了 init 函数。但它会引发一个AttributeError: 'x' is not a model attribute。我猜这是因为gurobipy类没有x属性。

接下来,我想将其存储在变量类的实例中。我想在模型类中编写一个函数,该函数可以解决问题。这是功能:

def store_x(self, var):
    var.x = self.x

然后,我收到此错误:gurobipy.GurobiError: Unable to retrieve attribute 'x',我不明白为什么。

我什至无法从函数外部访问x变量。我可以从函数内部进行打印,仅此而已。问题是,我稍后需要这个x变量。

我该如何实现?如何存储x变量以在以后访问它?它不必在变量类中,任何其他解决方案也应得到赞赏。

1 个答案:

答案 0 :(得分:0)

首先,我发现您的代码存在问题:

def store_x(self, var):
    var.x = self.x

它需要更改为:

def store_x(self, var):
    self.x = var.x

这是因为您在'var'参数中发送的内容仅是您实际传递的内容的副本。然后,其作用域将仅持续到该store_x方法的末尾。因此,您可以传递该副本,并告诉您的变量类实例将其存储在x值中。

关于您遇到的错误:

self.x = None # inside your Model class

我不确定为什么,因为我尝试了以下操作并且运行正常:

class Variables:
    def __init__(self):
        self.data = data
        self.number_of_clusters = number_of_clusters
        self.neighbourhood_size = neighbourhood_size
        self.variables_before = None
        self.variables_now = None
        self.ofv_before = None
        self.ofv_now = None
        self.x = None

因此,在弄清所需条件之后,我将用更深层的示例更新我的答案。这里分别是两个名为“变量”,“模型”的骨架类:

class Variables:
    def __init__(self):
        self.data = None
        self.number_of_clusters = None
        self.neighbourhood_size = None
        self.variables_before = None
        self.variables_now = None
        self.ofv_before = None
        self.ofv_now = None
        self.x = None


    def get_x(self,modelx):

        self.x = modelx


class Model:
    def __init__(self):
        self.x = ({}, {})



# create your class instances here

newVar = Variables()
newModel = Model()

# one way to assign your Variable class's x attribute the tuple dict in question.  
newVar.x = newModel.x 

# alternate way is to create a function inside your Variable class that updates the x variable based on the argument you send it.  
newVar.get_x(newModel.x)