Skip to content
The Chat Archive
gpt-3.5-turbo-0613· OpenAI WildChat-4.8M (AI2)

import numpy as np from scipy.optimize import minimize def objective(S, I, T): return…

  1. User

    import numpy as np from scipy.optimize import minimize

    def objective(S, I, T): return np.linalg.norm((I - np.matmul(T, S)),ord=2)

    def constraint(S): return S.shape == (9, 1)

    I is an nx1 vector

    T is an nxm matrix

    S is an mx1 vector

    T = np.genfromtxt("T_4 8.csv", delimiter=",") S1 = np.arange(9).reshape((9, 1)) I = np.dot(T, S1)

    initial guess for S

    S0 = np.zeros((9, 1))

    bounds for S

    bounds = [(0, None)] * 9

    constraint for S

    cons = [{'type': 'eq', 'fun': constraint}]

    result = minimize(objective, S0, args=(I, T), method='SLSQP', bounds=bounds, constraints=cons)

    result = minimize(objective, S0, args=(I, T), method='SLSQP', bounds=bounds) print(result)我该怎么单独提取出目标函数的结果

  2. Assistant

    要提取出目标函数的结果,可以使用result.x。这将返回得到的优化变量的最优值。

    optimal_S = result.x
    print(optimal_S)
    

    你也可以使用result.fun来获取目标函数的最优值。

    optimal_objective = result.fun
    print(optimal_objective)