“缩放器”是否乘以两次?

时间:2019-02-26 13:48:06

标签: openmdao

我一直在openmdao中的缩放选项方面遇到一些麻烦。由于有时缩放在矩阵求解器中会隐式工作,而在其他时候会显式等等。我现在遇到的问题是约束的缩放并记录下来。我用过Cantilever beam example

我添加了scaler = 2

    self.add_constraint('volume_comp.volume', equals=volume,scaler=2)

我没有更改音量= 0.01约束值。如果我将scaler设置为1,则在优化结束时,约束量的记录输出等于0.01。但是,如果我将scaler = 2设置为0,则体积变量为0.04。对于定标器= 10,音量输出等于1等。是否有额外的乘法。

优化值无论如何都不会改变,我认为这是期望的,因为这种缩放仅用于归一化(据我所知)。

下面是一行更改的示例代码和记录器,我正在使用OpenMDAO 2.5.0

from __future__ import division
import numpy as np

from openmdao.api import Group, IndepVarComp

from openmdao.test_suite.test_examples.beam_optimization.components.moment_comp import MomentOfInertiaComp
from openmdao.test_suite.test_examples.beam_optimization.components.local_stiffness_matrix_comp import LocalStiffnessMatrixComp
from openmdao.test_suite.test_examples.beam_optimization.components.states_comp import StatesComp
from openmdao.test_suite.test_examples.beam_optimization.components.displacements_comp import DisplacementsComp
from openmdao.test_suite.test_examples.beam_optimization.components.compliance_comp import ComplianceComp
from openmdao.test_suite.test_examples.beam_optimization.components.volume_comp import VolumeComp


class BeamGroup(Group):

    def initialize(self):
        self.options.declare('E')
        self.options.declare('L')
        self.options.declare('b')
        self.options.declare('volume')
        self.options.declare('num_elements', int)

    def setup(self):
        E = self.options['E']
        L = self.options['L']
        b = self.options['b']
        volume = self.options['volume']
        num_elements = self.options['num_elements']
        num_nodes = num_elements + 1

        force_vector = np.zeros(2 * num_nodes)
        force_vector[-2] = -1.

        inputs_comp = IndepVarComp()
        inputs_comp.add_output('h', shape=num_elements)
        self.add_subsystem('inputs_comp', inputs_comp)

        I_comp = MomentOfInertiaComp(num_elements=num_elements, b=b)
        self.add_subsystem('I_comp', I_comp)

        comp = LocalStiffnessMatrixComp(num_elements=num_elements, E=E, L=L)
        self.add_subsystem('local_stiffness_matrix_comp', comp)

        comp = StatesComp(num_elements=num_elements, force_vector=force_vector)
        self.add_subsystem('states_comp', comp)

        comp = DisplacementsComp(num_elements=num_elements)
        self.add_subsystem('displacements_comp', comp)

        comp = ComplianceComp(num_elements=num_elements, force_vector=force_vector)
        self.add_subsystem('compliance_comp', comp)

        comp = VolumeComp(num_elements=num_elements, b=b, L=L)
        self.add_subsystem('volume_comp', comp)

        self.connect('inputs_comp.h', 'I_comp.h')
        self.connect('I_comp.I', 'local_stiffness_matrix_comp.I')
        self.connect(
            'local_stiffness_matrix_comp.K_local',
            'states_comp.K_local')
        self.connect(
            'states_comp.d',
            'displacements_comp.d')
        self.connect(
            'displacements_comp.displacements',
            'compliance_comp.displacements')
        self.connect(
            'inputs_comp.h',
            'volume_comp.h')

        self.add_design_var('inputs_comp.h', lower=1e-2, upper=10.)
        self.add_objective('compliance_comp.compliance')
        self.add_constraint('volume_comp.volume', equals=volume,scaler=10)

import numpy as np

from openmdao.api import Problem, ScipyOptimizeDriver,SqliteRecorder

E = 1.
L = 1.
b = 0.1
volume = 0.01

num_elements = 50

prob = Problem(model=BeamGroup(E=E, L=L, b=b, volume=volume, num_elements=num_elements))

prob.driver = ScipyOptimizeDriver()
prob.driver.options['optimizer'] = 'SLSQP'
prob.driver.options['tol'] = 1e-9
prob.driver.options['disp'] = True
recorder = SqliteRecorder('abc.sql')
prob.driver.add_recorder(recorder)
prob.driver.recording_options['includes'] = []
prob.driver.recording_options['record_inputs'] = True
#        prob.driver.recording_options['record_outputs'] = True
prob.driver.recording_options['record_objectives'] = True
prob.driver.recording_options['record_constraints'] = True
prob.driver.recording_options['record_desvars'] = True
prob.driver.recording_options['record_derivatives'] = True
prob.setup()

prob.run_driver()

print(prob['inputs_comp.h'])
prob.cleanup()


#%%
import re
from openmdao.api import  CaseReader
import matplotlib.pyplot as plt
import numpy as np
import os,json as js    
import matplotlib

cr = CaseReader('abc.sql')
case_keys = cr.list_cases()
obj=[]
for i, case_key in enumerate(case_keys):
    case = cr.get_case(case_key)   
    derivs = cr.get_case(i).jacobian
#        for k in derivs:
#            print(k,derivs[k])

    recorded_objectives  = case.get_objectives()
    recorder_constraints = case.get_constraints()        
    recorder_desvars     = case.get_design_vars()        
    recorder_responses   = case.get_responses()        
    for k in recorder_desvars:
        print(k,recorder_desvars[k])
    for k in recorder_constraints:
        print(k,recorder_constraints[k])  
    for k in recorded_objectives:
        print(k,recorded_objectives[k])  
        obj.append(recorded_objectives[k])

    print('-----------')



#    
#print(obj)
#obj[2]=obj[1]
#print(len(obj))
plt.plot(obj,'*')

1 个答案:

答案 0 :(得分:0)

记录器中的变量确实缩放了两倍。在驱动程序中,这些值已正确缩放。您可以将其与下面的代码进行比较。缩放是在_apply_voi_meta()类的Case中完成的。

#%%

cr = CaseReader('abc.sql')
case_keys = cr.list_cases()
obj=[]
for i, case_key in enumerate(case_keys):
    case = cr.get_case(case_key)
    derivs = cr.get_case(i).jacobian
#        for k in derivs:
#            print(k,derivs[k])

    recorded_objectives  = case.get_objectives()
    recorder_constraints = case.get_constraints()
    recorder_desvars     = case.get_design_vars()
    recorder_responses   = case.get_responses()
    for k in recorder_desvars:
        print(k,recorder_desvars[k])
    for k in recorder_constraints:
        print(k,recorder_constraints[k])
    for k in recorded_objectives:
        print(k,recorded_objectives[k])
        obj.append(recorded_objectives[k])

    print('-----------')

print('\nDRIVER:\n\n')

for k, v in iteritems(driver.get_design_var_values()):
    print(k, v)
for k, v in iteritems(driver.get_constraint_values()):
    print(k, v)
for k, v in iteritems(driver.get_objective_values()):
    print(k, v)

现在很明显,问题的根源是记录器。在您的案例中,您对案例分别调用 get_constraints()get_responses()方法。在这些方法中,可变变量被缩放。因此,当您返回值时,vals中的变量_apply_voi_meta()也将得到缩放。您多次调用任何函数(即缩放相同的变量)时,都会在每次调用函数时缩放这些变量,这是一个副作用。

请参见以下示例:

#%%

cr = CaseReader('abc.sql')
case_keys = cr.list_cases()
obj=[]

scaled = True

for i, case_key in enumerate(case_keys):
    case = cr.get_case(case_key)
    derivs = cr.get_case(i).jacobian
#        for k in derivs:
#            print(k,derivs[k])

    recorded_objectives  = case.get_objectives(scaled=scaled)
    recorder_constraints = case.get_constraints(scaled=scaled)
    recorder_desvars     = case.get_design_vars()
    # recorder_responses   = case.get_responses(scaled=scaled)

    case.get_constraints(scaled=scaled)
    case.get_constraints(scaled=scaled)

    for k in recorder_desvars:
        print(k,recorder_desvars[k])
    for k in recorder_constraints:
        print(k,recorder_constraints[k])
    for k in recorded_objectives:
        print(k,recorded_objectives[k])
        obj.append(recorded_objectives[k])

    print('-----------')

print('\nDRIVER:\n\n')

for k, v in iteritems(driver.get_design_var_values()):
    print(k, v)
for k, v in iteritems(driver.get_constraint_values()):
    print(k, v)
for k, v in iteritems(driver.get_objective_values()):
    print(k, v)

如果缩放器为10,则约束将为未缩放值的1000倍(或10 ^ 3),因为现在我调用了get_constraints()方法3次。