将整数约束置于神秘之中 [英] Putting integer constraints in mystic

查看:82
本文介绍了将整数约束置于神秘之中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是神秘主义者,正在研究优化问题.我的神秘代码如下:

I am new to mystic and working on an optimization problem.My mystic code looks like this:

def find_loss(e,lmd,q,k):  
    edge_pmf=find_final_dist(e,lmd,q) 
    l_e=sum(edge_pmf[k+1:])
    return l_e   

def objective(x):
    s=0
    for i in range(len(x)):
        s+=find_loss(edge_enum[i],lamd,q,x[i]) 
    return s 

added=lambda x: [i for i in x]        
cons=lambda x: my.constraints.impose_sum(total_cap,added(x)) 

@integers() 
def round(x): 
    return x 

bounds=[(0,None)]*a    
if __name__=='_main_':
   result=diffev2(objective,x0=bounds,bounds=bounds,constraints=round,npop=20,gtol=50,disp=True,full_output=True) 
   print(result[0])

我保证我的objective()定义正确(它包含一些此代码中未提及的词典和函数).但是我的约束cons只能与x一起用作整数值,还是为此需要在my.constraints之上添加一些@integers()约束,如下所示?而且,我的优化结果什么也没显示.我的错在哪里?

I assure my objective() is defined right(it contains a few dictionaries and functions not mentioned in this code). But does my constraint cons work only with x only as integer values, or I need to add some constraints like @integers()above my.constraints for that,as done below? Also,my optimization result shows nothing.Where's my fault?

推荐答案

如果您想一次应用两个约束,mystic可以使用耦合器来帮助实现这一点.如果将两个约束修饰符应用于单个函数,则它将成为约束"OR",并且人们通常需要"AND".因此,在mystic.constraints中,存在用于构建复合约束的"AND","OR"和"NOT",以及用于其他类型的函数耦合的mystic.coupler.

If you want to apply both constraints at once, mystic has couplers to help do that. If you apply two constraints decorators to a single function, it will be a constraints "OR", and people generally want an "AND". So, within mystic.constraints, there's "AND", "OR", and "NOT", to build compound constraints, as well as mystic.coupler for other types of function coupling.

以下是使用"AND"的示例:

Here's an example of using "AND":

>>> import mystic as my
>>> total_cap = 10
>>> added=lambda x: [i for i in x] 
>>> cons=lambda x: my.constraints.impose_sum(total_cap,added(x))
>>> import numpy as np
>>> round = np.round
>>> c = my.constraints.and_(cons, round)
>>> c([1.1, 2.3, 4.5, 6.7])
[1.0, 1.0, 3.0, 5.0]
>>> 

可以在此处找到使用带有整数约束的耦合器的完整示例:

A full example of using a coupler with integer constraints can be found here: https://github.com/uqfoundation/mystic/blob/master/examples2/eq10.py

这篇关于将整数约束置于神秘之中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆