简体   繁体   English

如何在没有已知目标函数(比如一些随机函数)和已知变量和约束的情况下使用 gekko 优化器?

[英]how can I use gekko optimizer without a known objective function(let say some random function) and with known variables and constraints?

I a trying to minimize and maximize a random 'test function' using gekko which is connected to A. A consists of 4 parameters between the range (0-100) and sum of A < 100.However I keep getting weird results,because the minimum of the test function is supposed to be 2500 and the max 10000. My code is below.Can anyone tell me where the problem is?我试图使用连接到 A 的 Gekko 来最小化和最大化随机“测试函数”。 A 由范围 (0-100) 和 A < 100 的总和之间的 4 个参数组成。但是我一直得到奇怪的结果,因为测试函数的最小值应该是 2500,最大值是 10000。我的代码在下面。谁能告诉我问题出在哪里? Thanks in advance提前致谢


    import numpy as np
    from gekko import GEKKO 


    def test_function(x):
        return np.dot(x, x)


    A = m.Array(m.Var, (4))
    # initial guess
    ig = [1, 5, 5, 1]
    # lower bounds
    i = 0
    for Ai in A:
        Ai.value = ig[i]
        Ai.lower = 0
        Ai.upper = 100
        i += 1
    m.Equation(np.sum(A) < 100)
    m.Obj(test_function(A))
    m.solve()
    print(test_function(A))
    print (A)

results结果

Solver         :  IPOPT (v3.12)
 Solution time  :   1.379999999971915E-002 sec
 Objective      :   4.141037033873033E-007
 Successful solution
 ---------------------------------------------------

(((((v1)*(v1))+((v2)*(v2)))+((v3)*(v3)))+((v4)*(v4)))
[[0.00042734466188] [0.00015629584657] [0.00015629584657]
 [0.00042734466188]]

Process finished with exit code 0

The way that you have your lower bounds and objective function defined, gekko is choosing decimal that is just barely above 0. A decimal just barely above zero satisfies all of the bounds that you have given it and it is why the objective is so low.定义下界和目标函数的方式,gekko 选择略高于 0 的小数。略高于零的小数满足你给它的所有界限,这就是目标如此低的原因。 Also if you make the objective function negative it will maximize it to 5000. I'm not sure where you got the min and max that you are expecting.此外,如果您将目标函数设为负值,它会将其最大化到 5000。我不确定您在哪里获得了您期望的最小值和最大值。

I modified the problem to match your statement.我修改了问题以匹配您的陈述。 There is also a list comprehension that simplifies the process of defining new variables with upper and lower bounds.还有一个列表推导式,可以简化定义具有上限和下限的新变量的过程。 The following script shows two ways of accessing test_function(x) with (1) Gekko variables that are symbolic expressions or (2) with numeric values to evaluate that constraint and objective function.以下脚本显示了使用 (1) 作为符号表达式的 Gekko 变量或 (2) 使用数值访问test_function(x)两种方法,以评估该约束和目标函数。

import numpy as np
from gekko import GEKKO 
m = GEKKO(remote=False)
def test_function(x):
    return np.dot(x, x)
ig = [1, 5, 5, 1] # initial guess
A = np.array([m.Var(value=ig[i],lb=0,ub=10000,name='a'+str(i)) \
              for i in range(4)])
m.Equation(test_function(A)>2500)
m.Equation(test_function(A)<10000)
m.Minimize(test_function(A))
m.solve()
# extract values to get a numerical solution of test_function
A_sol = [A[i].value[0] for i in range(4)]
print(test_function(A_sol))
# get the objective function value from the solver
print(m.options.OBJFCNVAL)
# print variables
print (A)

The results of the script are shown below.脚本的结果如下所示。 If you use print(test_function(A_sol)) , it prints the symbolic expression that Gekko uses to find the solution.如果您使用print(test_function(A_sol)) ,它会打印 Gekko 用来查找解决方案的符号表达式。 In your case, you may be interested in the numeric solution, not the symbolic form.在您的情况下,您可能对数字解决方案感兴趣,而不是符号形式。

# results
2499.999999993099
2500.0
[[14.90599615] [32.059495922] [32.059495922] [14.90599615]]

Both m.options.OBJFCNVAL and evaluating the expression give the same result but are slightly different because of machine precision. m.options.OBJFCNVAL和计算表达式都给出相同的结果,但由于机器精度而略有不同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用带有变量的随机函数? - How can i use the random function with variables? 从 PuLP 到 GEKKO:约束、目标函数的语法映射 - From PuLP to GEKKO: Syntax Mapping for Constraints, Objective Function 当只知道目标函数时,如何在Gurobi中设置启动解决方案? - How to set an start solution in Gurobi, when only objective function is known? 在已知目标 function 上实现梯度下降 - Implementing gradient descent on with known objective function 我可以在 Gekko 的设计优化过程中使用隐式目标 function。 这个隐式 function 是一个子程序,它创建数字 model - Can I use implicit objective function in a design optimization process in Gekko. this implicit function is a subroutine that creates numerical model 如何在Gekko获得目标函数的价值 - How to get value for objective function in Gekko 如何在 Python Gekko 中最大化目标 function? - How to maximize an objective function in Python Gekko? 如果我的目标函数是非线性(也是指数解释)函数,我应该使用什么求解器? Python GEKKO - What solver should I use if my objective function is an nonlinear (also exponential explanation) function? Python GEKKO 如何指定 Z3 的优化器从目标 function 的下限开始搜索? - How can I specify to the Z3's optimizer to start the search from the lower bound of the objective function? 我怎么能猜出已知字符串哈希中缺少的字符? - How can i guess some missing character in known strings hash?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM