问题
I am trying to set constraints to perform optimization with the help of mystic package.
I executed the code in Spyder and PyCharm IDE. In both cases kernel crashed. When I have only 2 line string 'simplify'-method works fine. Trying to set greater than 2 lines constraints leads to kernel's death.
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)
回答1:
You are probably just hitting your memory limit, or something like that. I'd suggest to turn off iterating through all solutions, and turn on cycling the variable solved for.
>>> 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]
>>>
You may also want to look at and
and or
, which when used with the join
keyword in generate_constraint
, can be useful for modifying how the individual constraints work together. Also, if you have specific parameters you'd prefer are solved for, you can give them with the target
keyword.
Anyway, the above should generate a constraint, useful within mystic's optimizers.
来源:https://stackoverflow.com/questions/55925571/issues-regarding-constraints-setting-via-mystic-package