有没有办法动态更新稀疏部分索引

Is there any way to update the sparse partial indices on the fly

假设我有这个带有稀疏部分声明的显式组件。在我设置问题并开始优化后,有什么方法可以更改部分索引。部分的大小不会改变(比如它总是 (4,1)),但位置 (rows/columns) 会改变。这可能吗?也许有窍门?

import numpy as np
import openmdao.api as om
class SparsePartialComp(om.ExplicitComponent):
    def setup(self):
        self.add_input('x', shape=(4,))
        self.add_output('f', shape=(2,))

        self.declare_partials(of='f', wrt='x',
                              rows=[0, 1, 1, 1],
                              cols=[0, 1, 2, 3])

    def compute_partials(self, inputs, partials):
        # Corresponds to the [(0,0), (1,1), (1,2), (1,3)] entries.
        partials['f', 'x'] = [1., 2., 3., 4.]

model = om.Group()
comp = om.IndepVarComp()
comp.add_output('x', np.ones(4))

model.add_subsystem('input', comp)
model.add_subsystem('example', SparsePartialComp())

model.connect('input.x', 'example.x')

problem = om.Problem(model=model)
problem.setup()
problem.run_model()
totals = problem.compute_totals(['example.f'], ['input.x'])

print(totals['example.f', 'input.x'])

从 OpenMDAO V2.8 开始,不允许即时更改稀疏模式。您必须找出所有可能为非零的条目并声明所有条目。

我们有可能在未来取消此限制,尽管这样做不在明年的路线图上。即使我们确实添加了它,进行这种即时切换也会有一些显着的开销,并且不建议在执行过程中经常这样做。设置开销会淹没其他所有内容,除非稀疏性节省很大 --- 4x1 并不大 :)