微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

APOT求解器每次都找到不同的解

如何解决APOT求解器每次都找到不同的解

我正在使用 gekko 来解决 MINLP 问题。我正在使用 APOT 求解器,因为它是唯一可以提供整数解的求解器,这在我的情况下是严格需要的。

我遇到的问题是,每次运行求解器时,我都会有不同的解决方案,即使对于非常小的情况,我也无法确定解决方案的最优性。从一些解决方案到其他解决方案,客观最终值存在重要差异。

我注意到只发生了 1 次迭代,不知道是否应该是这样。此外,当它可以运行更长时间并找到更好的解决方案时,它的运行时间不到 1 秒。

在其中一个运行的输出下方:

 --------- APM Model Size ------------
 Each time step contains
   Objects      :           86
   Constants    :            0
   Variables    :          606
   Intermediates:            0
   Connections  :          806
   Equations    :         1046
   Residuals    :         1046
 
 Number of state variables:            606
 Number of total equations: -          172
 Number of slack variables: -           40
 ---------------------------------------
 degrees of freedom       :            394
 
 ----------------------------------------------
 Steady State Optimization with APOPT Solver
 ----------------------------------------------
Iter:     1 I:  0 Tm:      0.38 NLPi:   28 Dpth:    0 Lvs:    0 Obj:  1.27E+09 Gap:  0.00E+00
 Successful solution
 
 ---------------------------------------------------
 Solver         :  APOPT (v1.0)
 Solution time  :   0.38800000000628643      sec
 Objective      :    1271480486.0000000     
 Successful solution
 ---------------------------------------------------

对于如何调整求解器的任何建议,我将不胜感激,这样我就可以获得一致的解决方案。提前致谢

编辑

我正在添加代码(精简版)。基本上我想做的是根据m个城市的需求找到n个仓库的最佳位置,用h3库中相应的六边形表示。

from gekko import GEKKO
from random import randint
import pandas as pd



if __name__ == '__main__':
    
    n_warehouses = 6
    df_aggreg = pd.DataFrame.from_dict({'hex_id_basic': {32: 0,12: 1,2: 2,3: 3,22: 4,24: 5,38: 6,8: 7,19: 8,27: 9,21: 10,25: 11,28: 12,26: 13,29: 14,30: 15,31: 16,1: 17,20: 18,18: 19,17: 20,33: 21,35: 22,14: 23,13: 24,11: 25,10: 26,36: 27,6: 28,5: 29,4: 30,16: 31,34: 32,0: 33,23: 34,15: 35,9: 36,37: 37,7: 38,39: 39},'value': {32: 1808,12: 847,2: 847,3: 847,22: 847,24: 847,38: 847,8: 847,19: 847,27: 847,21: 452,25: 452,28: 452,26: 452,29: 452,30: 452,31: 452,1: 452,20: 452,18: 452,17: 452,33: 452,35: 452,14: 452,13: 452,11: 452,10: 452,36: 452,6: 452,5: 452,4: 452,16: 452,34: 169,0: 169,23: 84,15: 84,9: 84,37: 84,7: 84,39: 84}})
    distance_matrix = [[0,278320,302712,117682,283287,225645,303065,258900,252165,453768,389125,305694,377415,445329,176671,16098,95378,272352,153948,247011,153063,175620,184734,253592,235204,275271,204377,140151,270207,254950,364642,92383,239928,300635,394936,291140,293377,205417,253778,313127],[278320,144398,187013,244571,107924,257813,279533,334530,195669,205157,84028,109937,187707,427560,265135,188779,292672,307462,280094,408319,281660,131476,440829,46693,204251,233509,147273,263519,267077,185835,310005,254863,323378,119817,407585,280337,171576,280105,261055],[302712,185183,377007,234161,392715,170354,237346,307577,347861,228110,138929,301200,476655,286696,244493,179154,254571,176769,387398,378208,238178,372115,163476,79788,152001,222356,146928,158337,62584,289416,375428,203381,217686,310387,155900,289533,173468,397435],[117682,291864,179791,311714,170740,192926,379238,347869,240050,272172,370968,293195,101616,87586,185977,129253,162328,222594,233040,150108,262697,153805,162481,109099,101869,173375,162759,246974,123207,265470,218641,306739,255114,197339,195959,167223,320151],[283287,142846,20212,455483,484791,287216,161617,183085,340463,280631,326676,282534,212575,470828,409823,449527,434084,135211,148574,531347,213849,411617,394742,190552,450777,445332,427673,367985,53066,504293,288485,544788,473256,96874,453151,29897],[225645,158910,325969,365598,232092,169623,84652,214438,223750,344437,216987,130954,340949,308549,322092,374352,178834,41429,440518,71333,272261,268233,88085,317511,314784,285789,286639,147154,374185,190689,432588,338741,64095,324564,164244],[303065,474649,504627,286841,156847,192075,350549,280649,341260,302511,232768,489976,430017,468858,453490,151205,167295,551499,229266,429328,414077,210234,469552,464393,442147,388101,68990,523448,294341,564878,491892,116277,472394,10359],[258900,68930,467349,478557,358082,308273,460170,428178,244879,258325,15394,131661,13671,271974,403214,307634,213649,272011,90917,61801,266220,26455,12593,202481,195575,434164,48812,379747,140257,39308,358628,6107,482200],[252165,526826,526392,408559,373685,519349,407413,240840,276462,66186,101574,60620,226811,414422,341075,147167,320118,158889,101274,294543,95127,79800,271409,171262,456912,68984,441013,74111,100302,388712,64684,513007],[453768,136244,148461,178102,8441,573471,443171,358499,479086,502816,469837,595669,392502,273266,636429,225844,383011,426871,313638,447989,454756,317090,500323,328239,507228,90088,600610,461075,278672,468809,282694],[389125,121715,255074,131581,475633,382466,297470,492617,477097,477118,540843,284992,206940,609805,206849,408806,425937,256909,465228,466469,379467,455895,209773,524589,174173,596319,483539,187375,478271,151054],[305694,158796,140049,428839,295540,210329,371886,368057,357279,450043,256851,125251,502181,89028,287097,307444,165570,343975,345858,265337,357240,207855,403529,110982,479964,361969,139306,358064,193009],[377415,173125,534732,362967,293628,317765,374520,313604,493385,391108,241276,501197,156178,218494,280742,254733,285601,295994,139047,393079,358593,342270,90310,447455,294687,278510,310960,351759],[445329,565355,434730,350062,472002,494725,462533,587263,384786,264901,628392,217517,376220,419266,305200,440993,447576,311937,491975,321105,500318,84177,593082,454296,270683,461573,276700],[176671,192659,241685,440309,306114,415366,213722,192267,305547,353506,381057,451860,378110,280381,442537,425977,539074,236503,273614,465255,533298,426186,464854,295413,422602,351173],[16098,86037,258560,144482,233219,158401,180937,176621,250690,222751,259395,189468,130132,255508,240568,348587,87437,241145,287389,382525,282778,278823,201018,239867,312431],[95378,273554,197249,249773,243741,146171,90840,321549,143532,241689,196668,44890,260330,250266,306721,160762,180583,306135,302150,332965,284225,121772,254773,241969],[272352,140627,25344,279225,418280,322922,213131,286282,99366,77144,281612,32238,26277,207287,206362,449552,33473,391005,133626,34477,373979,19000,497504],[153948,117996,141824,323726,276426,134201,280163,191685,105340,226127,152834,133897,305377,69716,375475,161033,424673,138867,170911,317471,125583,439203],[247011,258348,393955,302253,202640,270296,98356,54784,259602,38118,20016,211973,182205,426898,56395,382945,133870,52974,352657,7588,476592],[153063,314904,334581,140210,370743,331489,244983,286515,294300,275249,442428,100328,387881,294243,528076,223497,311245,358451,265867,463708],[175620,150414,428871,237099,386147,341933,156045,406310,395685,438153,267684,82364,450314,367218,462586,430241,117601,399353,161423],[184734,406344,86738,264273,247556,50786,302265,297224,294689,249321,137664,356394,227861,405170,324686,53055,305544,174613],[253592,414364,298374,221122,355576,240067,222654,413797,163640,491733,212409,556786,103608,247427,443322,208464,561173],[235204,208101,219280,101025,259858,260053,214484,274518,217001,318654,159780,391112,279361,131548,271564,233971],[275271,86592,233845,67147,79218,115829,239530,400051,124610,293938,231168,78076,317319,94401,435547],[204377,204896,66374,53769,200202,155140,372506,110258,342408,174094,90128,297872,58728,421827],[140151,263729,256622,282929,198993,168664,314976,258111,356656,286974,94168,263557,218490],[270207,19120,176392,213505,432376,59886,359568,165204,23979,354255,31668,476705],[254950,192090,195450,424902,59414,367175,152072,38895,348532,15183,471835],[364642,346396,430496,224040,228276,340846,177161,344487,206946,445989],[92383,328145,229377,429805,200778,233973,281930,189695,397850],[239928,482764,317893,512855,455638,86038,431229,79283],[300635,418433,121305,47807,407440,51547,530978],[394936,515064,371904,249637,381509,293301],[291140,164624,450245,136922,573716],[293377,376916,45412,498907],[205417,356278,124356],[253778,480029],[313127,261055,397435,320151,29897,164244,10359,482200,513007,282694,151054,193009,351759,276700,351173,312431,241969,497504,439203,476592,463708,161423,174613,561173,233971,435547,421827,218490,476705,471835,445989,397850,79283,530978,293301,573716,498907,124356,480029,0]]
    
    # Initialize Model
    m = GEKKO(remote=False)
    m.options.soLVER = 1
    m.options.IMODE = 3

    # VARIABLES
    print("Creating variables...")

    # warehouse locations
    warehouses_to_hexagon_vars = []
    for dks_id in range(n_warehouses):
        warehouses_to_hexagon_vars.append([m.Var(value=0,lb=0,ub=1,integer=True,name=f"dk_{dks_id}_{hex_id}")
                                           for hex_id in df_aggreg["hex_id_basic"].unique()])
    # hexagon assigned to warehouse
    hexagon_to_warehouse_vars = []
    for hex_id in df_aggreg["hex_id_basic"].unique():
        hexagon_to_warehouse_vars.append([m.Var(value=0,name=f"hx_{hex_id}_{dks_id}")
                                          for dks_id in range(n_warehouses)])
    
    # CONSTRAINTS

    # A warehouse located only in one hexagon
    print("Creating constraints...")
    for dks_vars in warehouses_to_hexagon_vars:
        m.Equation(m.sum(dks_vars) == 1)

    # One hexagon contains at most one warehouse
    for hex_id in df_aggreg["hex_id_basic"].unique():
        m.Equation(m.sum([dks_vars[hex_id] for dks_vars in warehouses_to_hexagon_vars]) <= 1)

    # One hexagon assigned only to one warehouse
    for hex_vars in hexagon_to_warehouse_vars:
        m.Equation(m.sum(hex_vars) == 1)

    # WARM START
    for dks_id in range(n_warehouses):
        warehouses_to_hexagon_vars[dks_id][randint(0,len(warehouses_to_hexagon_vars[dks_id]) - 1)].value = 1

    for hex_id in range(len(hexagon_to_warehouse_vars)):
        hexagon_to_warehouse_vars[hex_id][randint(0,n_warehouses - 1)].value = 1
    
    # Set objective function
    print("Creating objective function...")
    for wh_id in range(n_warehouses):
        for hex_id_1 in df_aggreg["hex_id_basic"].unique():
            for hex_id_2 in df_aggreg["hex_id_basic"].unique():
                distance_hexagon_to_warehouse = int(distance_matrix[hex_id_1][hex_id_2])
                demand_hexagon = df_aggreg.loc[df_aggreg["hex_id_basic"] == hex_id_2,"value"].values[0]
                m.Obj(warehouses_to_hexagon_vars[wh_id][hex_id_1] * 
                      hexagon_to_warehouse_vars[hex_id_2][wh_id] * 
                      distance_hexagon_to_warehouse *
                      demand_hexagon)
    
    # Solve simulation
    m.solve()

解决方法

我已经实现了一个并行解决方案,其中我运行了 n 次相同的模型,但每次都有不同的初始解决方案。一旦所有模型都解决了,我就采用目标值最低的解决方案。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。