我正在尝试设置约束以借助神秘软件包进行优化。
我在Spyder和PyCharm IDE中执行了代码。在这两种情况下,内核都崩溃了。 当我只有2行字符串时,“简化”方法可以正常工作。尝试设置多于2行的约束会导致内核死亡。
import mystic.symbolic as ms
equation = """
x0*7.2 + x1*1.9 + x2*35.7 + x3*4.1 + x4*23.0 + x5*19.2 + x6*0.0 + x7*0.0 + x8*0.0 + x9*0.0 + x10*10.1 + x11*10.8 + x12*11.3 + x13*14.8 + x14*78.6 + x15*5.8 + x16*3.4 + x17*1.8 + x18*3.4 + x19*1.0 + x20*2.9 + x21*0.0 + x22*0.0 + x23*0.0 + x24*0.0 + x25*0.0 + x26*0.0 + x27*0.0 + x28*0.0 + x29*0.0 >= 27.0
x0*7.2 + x1*1.9 + x2*35.9 + x3*4.1 + x4*23.0 + x5*19.2 + x6*0.0 + x7*0.0 + x8*0.0 + x9*0.0 + x10*9.8 + x11*10.9 + x12*27.1 + x13*15.0 + x14*78.7 + x15*5.8 + x16*3.4 + x17*1.8 + x18*3.4 + x19*1.0 + x20*2.9 + x21*0.0 + x22*0.0 + x23*0.0 + x24*0.0 + x25*0.0 + x26*0.0 + x27*0.0 + x28*0.0 + x29*0.0 >= 29.0
x0*7.2 + x1*1.9 + x2*35.9 + x3*4.1 + x4*23.0 + x5*19.2 + x6*0.0 + x7*0.0 + x8*0.0 + x9*0.0 + x10*9.8 + x11*10.9 + x12*27.1 + x13*15.0 + x14*78.7 + x15*5.8 + x16*3.4 + x17*1.8 + x18*3.4 + x19*1.0 + x20*2.9 + x21*0.0 + x22*0.0 + x23*0.0 + x24*0.0 + x25*0.0 + x26*0.0 + x27*0.0 + x28*0.0 + x29*0.0 >= 29.0
x0*0.0 + x1*0.0 + x2*0.0 + x3*0.0 + x4*0.0 + x5*0.0 + x6*27.9 + x7*73.0 + x8*230.9 + x9*107.3 + x10*0.0 + x11*0.0 + x12*0.0 + x13*0.0 + x14*0.0 + x15*0.0 + x16*0.0 + x17*0.0 + x18*0.0 + x19*0.0 + x20*0.0 + x21*13.7 + x22*63.7 + x23*29.5 + x24*7.2 + x25*24.3 + x26*9.6 + x27*142.1 + x28*10.5 + x29*41.0 >= 420.0
x0*0.0 + x1*0.0 + x2*0.0 + x3*0.0 + x4*0.0 + x5*0.0 + x6*27.2 + x7*72.9 + x8*88.5 + x9*107.3 + x10*0.0 + x11*0.0 + x12*0.0 + x13*0.0 + x14*0.0 + x15*0.0 + x16*0.0 + x17*0.0 + x18*0.0 + x19*0.0 + x20*0.0 + x21*13.7 + x22*63.7 + x23*29.5 + x24*7.2 + x25*23.6 + x26*9.6 + x27*142.1 + x28*10.3 + x29*37.0 >= 420.0
"""
eqn = ms.simplify(equation)
答案 0 :(得分:0)
您可能刚刚达到内存限制或类似的限制。我建议关闭所有解决方案的迭代,并打开循环解决的变量。
>>> import mystic.symbolic as ms
>>> equation = """
... x0*7.2 + x1*1.9 + x2*35.7 + x3*4.1 + x4*23.0 + x5*19.2 + x6*0.0 + x7*0.0 + x8*0.0 + x9*0.0 + x10*10.1 + x11*10.8 + x12*11.3 + x13*14.8 + x14*78.6 + x15*5.8 + x16*3.4 + x17*1.8 + x18*3.4 + x19*1.0 + x20*2.9 + x21*0.0 + x22*0.0 + x23*0.0 + x24*0.0 + x25*0.0 + x26*0.0 + x27*0.0 + x28*0.0 + x29*0.0 >= 27.0
... x0*7.2 + x1*1.9 + x2*35.9 + x3*4.1 + x4*23.0 + x5*19.2 + x6*0.0 + x7*0.0 + x8*0.0 + x9*0.0 + x10*9.8 + x11*10.9 + x12*27.1 + x13*15.0 + x14*78.7 + x15*5.8 + x16*3.4 + x17*1.8 + x18*3.4 + x19*1.0 + x20*2.9 + x21*0.0 + x22*0.0 + x23*0.0 + x24*0.0 + x25*0.0 + x26*0.0 + x27*0.0 + x28*0.0 + x29*0.0 >= 29.0
... x0*7.2 + x1*1.9 + x2*35.9 + x3*4.1 + x4*23.0 + x5*19.2 + x6*0.0 + x7*0.0 + x8*0.0 + x9*0.0 + x10*9.8 + x11*10.9 + x12*27.1 + x13*15.0 + x14*78.7 + x15*5.8 + x16*3.4 + x17*1.8 + x18*3.4 + x19*1.0 + x20*2.9 + x21*0.0 + x22*0.0 + x23*0.0 + x24*0.0 + x25*0.0 + x26*0.0 + x27*0.0 + x28*0.0 + x29*0.0 >= 29.0
... x0*0.0 + x1*0.0 + x2*0.0 + x3*0.0 + x4*0.0 + x5*0.0 + x6*27.9 + x7*73.0 + x8*230.9 + x9*107.3 + x10*0.0 + x11*0.0 + x12*0.0 + x13*0.0 + x14*0.0 + x15*0.0 + x16*0.0 + x17*0.0 + x18*0.0 + x19*0.0 + x20*0.0 + x21*13.7 + x22*63.7 + x23*29.5 + x24*7.2 + x25*24.3 + x26*9.6 + x27*142.1 + x28*10.5 + x29*41.0 >= 420.0
... x0*0.0 + x1*0.0 + x2*0.0 + x3*0.0 + x4*0.0 + x5*0.0 + x6*27.2 + x7*72.9 + x8*88.5 + x9*107.3 + x10*0.0 + x11*0.0 + x12*0.0 + x13*0.0 + x14*0.0 + x15*0.0 + x16*0.0 + x17*0.0 + x18*0.0 + x19*0.0 + x20*0.0 + x21*13.7 + x22*63.7 + x23*29.5 + x24*7.2 + x25*23.6 + x26*9.6 + x27*142.1 + x28*10.3 + x29*37.0 >= 420.0
... """
>>> eqns = ms.simplify(equation, all=False, cycle=True)
>>> print(eqns)
x4 >= -0.31304347826087*x0 - 0.0826086956521739*x1 - 0.426086956521739*x10 - 0.473913043478261*x11 - 1.17826086956522*x12 - 0.652173913043478*x13 - 3.42173913043478*x14 - 0.252173913043478*x15 - 0.147826086956522*x16 - 0.0782608695652174*x17 - 0.147826086956522*x18 - 0.0434782608695652*x19 - 1.56086956521739*x2 - 0.126086956521739*x20 - 0.178260869565217*x3 - 0.834782608695652*x5 + 1.26086956521739
x7 >= -0.187928669410151*x21 - 0.873799725651577*x22 - 0.404663923182442*x23 - 0.0987654320987654*x24 - 0.323731138545953*x25 - 0.131687242798354*x26 - 1.94924554183813*x27 - 0.141289437585734*x28 - 0.507544581618656*x29 - 0.373113854595336*x6 - 1.21399176954733*x8 - 1.4718792866941*x9 + 5.76131687242798
x0 >= -0.263888888888889*x1 - 1.40277777777778*x10 - 1.5*x11 - 1.56944444444444*x12 - 2.05555555555556*x13 - 10.9166666666667*x14 - 0.805555555555556*x15 - 0.472222222222222*x16 - 0.25*x17 - 0.472222222222222*x18 - 0.138888888888889*x19 - 4.95833333333333*x2 - 0.402777777777778*x20 - 0.569444444444444*x3 - 3.19444444444444*x4 - 2.66666666666667*x5 + 3.75
x6 >= -0.491039426523298*x21 - 2.2831541218638*x22 - 1.0573476702509*x23 - 0.258064516129032*x24 - 0.870967741935484*x25 - 0.344086021505376*x26 - 5.09318996415771*x27 - 0.376344086021505*x28 - 1.46953405017921*x29 - 2.61648745519713*x7 - 8.27598566308244*x8 - 3.84587813620072*x9 + 15.0537634408602
x3 >= -1.75609756097561*x0 - 0.463414634146341*x1 - 2.39024390243902*x10 - 2.65853658536585*x11 - 6.60975609756098*x12 - 3.65853658536585*x13 - 19.1951219512195*x14 - 1.41463414634146*x15 - 0.829268292682927*x16 - 0.439024390243902*x17 - 0.829268292682927*x18 - 0.24390243902439*x19 - 8.75609756097561*x2 - 0.707317073170732*x20 - 5.60975609756098*x4 - 4.68292682926829*x5 + 7.07317073170732
>>>
>>> constrain = ms.generate_constraint(ms.generate_solvers(eqns))
>>>
>>> constrain(list(range(-29,1)))
[-29, -28, -27, -26, 187.46956521739128, -24, 84.56076719225553, 85.54595336076824, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0]
>>>
您可能还想看看and
和or
,当它们与join
中的generate_constraint
关键字一起使用时,可用于修改各个约束的工作方式一起。另外,如果您有想要解决的特定参数,则可以给它们加上target
关键字。
无论如何,以上内容应该会产生一个约束,这在神秘主义者的优化器中很有用。