@@ -813,8 +813,6 @@ def _find_solution_unrounded(
813813 approx = p .get_omnipool_approx (tkn )
814814 # if approx == "none" and epsilon_tkn[tkn] <= 1e-6 and tkn != p.tkn_profit:
815815 # approx = "linear"
816- # elif approx == "none" and epsilon_tkn[tkn] <= 1e-3:
817- # approx = "quadratic"
818816 if tkn == p .tkn_profit :
819817 approx = "none"
820818 if approx == "linear" : # linearize the AMM constraint
@@ -858,14 +856,6 @@ def _find_solution_unrounded(
858856 # A4i = np.vstack([A4i, A4i_bounds])
859857 # b4i = np.append(b4i, b4i_bounds)
860858 # cones4.append(cb.NonnegativeConeT(4))
861- elif approx == "quadratic" : # quadratic approximation to in-given-out function
862- A4i = np .zeros ((3 , k ))
863- A4i [1 ,i ] = - omnipool_lrna_coefs [tkn ]
864- A4i [1 ,n + i ] = - omnipool_asset_coefs [tkn ]
865- A4i [2 ,n + i ] = - omnipool_asset_coefs [tkn ]
866- b4i = np .array ([1 , 0 , 0 ])
867- cones4 .append (cb .PowerConeT (0.5 ))
868- cone_sizes4 .append (3 )
869859 else : # full AMM constraint
870860 A4i = np .zeros ((3 , k ))
871861 b4i = np .ones (3 )
@@ -991,11 +981,8 @@ def _find_good_solution(
991981 n , m , r = p .n , p .m , p .r
992982 force_omnipool_approx = {tkn : "linear" for tkn in p .omnipool .asset_list }
993983 amm_approx_list = []
994- for amm in p .amm_list :
995- if isinstance (amm , StableSwapPoolState ):
996- amm_approx_list .append (["linear" for _ in range (len (amm .asset_list ) + 1 )])
997- elif isinstance (amm , ConstantProductPoolState ):
998- amm_approx_list .append ("linear" )
984+ for amm_constraints in p .amm_constraints :
985+ amm_approx_list .append (amm_constraints .get_linear_approx ())
999986 p .set_up_problem (clear_I = False , force_omnipool_approx = force_omnipool_approx , amm_approx_list = amm_approx_list )
1000987 omnipool_deltas , intent_deltas , x , obj , dual_obj , status , amm_deltas = _find_solution_unrounded (p , allow_loss = allow_loss )
1001988 # if partial trade size is much higher than executed trade, lower trade max
@@ -1021,40 +1008,19 @@ def _find_good_solution(
10211008 # stableswap_pcts.append(pcts)
10221009
10231010 # force_omnipool_approx = None
1024- approx_adjusted_ct = 0
1011+ approx_adjusted = False
10251012 if approx_amm_eqs and status not in ['PrimalInfeasible' , 'DualInfeasible' ]: # update approximations if necessary
10261013 omnipool_pcts = {tkn : abs (omnipool_deltas [tkn ]) / p .omnipool .liquidity [tkn ] for tkn in p .omnipool .asset_list }
10271014 for tkn in p .omnipool .asset_list :
10281015 if force_omnipool_approx [tkn ] == "linear" and omnipool_pcts [tkn ] > 1e-6 :
1029- force_omnipool_approx [tkn ] = "quadratic" # don't actually want to force linear approximation
1030- approx_adjusted_ct += 1
1031- if force_omnipool_approx [tkn ] == "quadratic" and omnipool_pcts [tkn ] > 1e-3 :
1032- force_omnipool_approx [tkn ] = "full" # don't actually want to force quadratic approximation
1033- approx_adjusted_ct += 1
1034-
1035- amm_pcts = []
1036- for _i , amm in enumerate (p .amm_list ):
1037- pcts = [abs (amm_deltas [_i ][0 ]) / amm .shares ] # first shares size constraint, delta_s / s_0 <= epsilon
1038- if isinstance (amm , StableSwapPoolState ):
1039- sum_delta_x = sum ([abs (amm_deltas [_i ][j + 1 ]) for j in range (len (amm .asset_list ))])
1040- pcts .append (sum_delta_x / amm .d )
1041- pcts .extend ([abs (amm_deltas [_i ][j + 1 ]) / amm .liquidity [tkn ] for j , tkn in enumerate (amm .asset_list )])
1042- amm_pcts .append (pcts )
1043- for s , amm in enumerate (p .amm_list ):
1044- if isinstance (amm , StableSwapPoolState ):
1045- if amm_approx_list [s ][0 ] == "linear" and max (amm_pcts [s ][0 ], amm_pcts [s ][1 ]) > 1e-5 :
1046- amm_approx_list [s ][0 ] = "full"
1047- approx_adjusted_ct += 1
1048- for j in range (len (amm .asset_list )): # evaluate each asset constraint
1049- if amm_approx_list [s ][j + 1 ] == "linear" and amm_pcts [s ][j + 2 ] > 1e-5 :
1050- amm_approx_list [s ][j + 1 ] = "full"
1051- approx_adjusted_ct += 1
1052- elif isinstance (amm , ConstantProductPoolState ):
1053- if amm_approx_list [s ][0 ] == "linear" and max (amm_pcts [s ]) > 1e-5 :
1054- amm_approx_list [s ][0 ] = "full"
1055- approx_adjusted_ct += 1
1056- else :
1057- raise AssertionError ("Unrecognized AMM type" )
1016+ force_omnipool_approx [tkn ] = "full" # don't actually want to force linear approximation
1017+ approx_adjusted = True
1018+
1019+ for _i , amm_constraints in enumerate (p .amm_constraints ):
1020+ old_approx = amm_approx_list [_i ]
1021+ amm_approx_list [_i ] = amm_constraints .upgrade_approx (amm_deltas [_i ], old_approx )
1022+ if old_approx != amm_approx_list [_i ]:
1023+ approx_adjusted = True
10581024
10591025 for i in range (100 ):
10601026 # lower maxes for intents
@@ -1066,7 +1032,7 @@ def _find_good_solution(
10661032 # so we should be looking at trade_pcts where max is nonzero
10671033 trade_pcts_nonzero_max = [max (- intent_deltas [i ],0 ) / m for i , m in enumerate (p .partial_sell_maxs ) if m > 0 ]
10681034
1069- if (len (trade_pcts_nonzero_max ) == 0 or min (trade_pcts_nonzero_max ) >= 0.1 ) and approx_adjusted_ct == 0 :
1035+ if (len (trade_pcts_nonzero_max ) == 0 or min (trade_pcts_nonzero_max ) >= 0.1 ) and ( not approx_adjusted ) :
10701036 break # no changes to problem were made
10711037 if len (trade_pcts_nonzero_max ) > 0 and min (trade_pcts_nonzero_max ) < 0.1 :
10721038 new_maxes , zero_ct = scale_down_partial_intents (p , trade_pcts , 10 )
@@ -1086,50 +1052,19 @@ def _find_good_solution(
10861052 if scale_trade_max : # update trade_pcts
10871053 trade_pcts = [- intent_deltas [i ] / m if m > 0 else 0 for i , m in enumerate (p .partial_sell_maxs )]
10881054 # trade_pcts + [1 for _ in range(r)]
1055+ approx_adjusted = False
10891056 if approx_amm_eqs and status not in ['PrimalInfeasible' , 'DualInfeasible' ]: # update approximations if necessary
10901057 omnipool_pcts = {tkn : abs (omnipool_deltas [tkn ]) / p .omnipool .liquidity [tkn ] for tkn in p .omnipool .asset_list }
1091- approx_adjusted_ct = 0
10921058 for tkn in p .omnipool .asset_list :
10931059 if force_omnipool_approx [tkn ] == "linear" and omnipool_pcts [tkn ] > 1e-6 :
1094- force_omnipool_approx [tkn ] = "quadratic" # don't actually want to force linear approximation
1095- approx_adjusted_ct += 1
1096- if force_omnipool_approx [tkn ] == "quadratic" and omnipool_pcts [tkn ] > 1e-3 :
1097- force_omnipool_approx [tkn ] = "full" # don't actually want to force quadratic approximation
1098- approx_adjusted_ct += 1
1099- # elif omnipool_pcts[tkn] <= 1e-6: # force linear
1100- # force_omnipool_approx[tkn] = "linear"
1101- # approx_adjusted_ct += 1
1102- # else:
1103- # if omnipool_pcts[tkn] <= 1e-6: # force linear
1104- # force_omnipool_approx[tkn] = "linear"
1105- # approx_adjusted_ct += 1
1106- # elif omnipool_pcts[tkn] <= 1e-3: # force quadratic
1107- # force_omnipool_approx[tkn] = "quadratic"
1108- # approx_adjusted_ct += 1
1109-
1110- amm_pcts = []
1111- for _i , amm in enumerate (p .amm_list ):
1112- pcts = [abs (amm_deltas [_i ][0 ]) / amm .shares ] # first shares size constraint, delta_s / s_0 <= epsilon
1113- if isinstance (amm , StableSwapPoolState ):
1114- sum_delta_x = sum ([abs (amm_deltas [_i ][j + 1 ]) for j in range (len (amm .asset_list ))])
1115- pcts .append (sum_delta_x / amm .d )
1116- pcts .extend ([abs (amm_deltas [_i ][j + 1 ]) / amm .liquidity [tkn ] for j , tkn in enumerate (amm .asset_list )])
1117- amm_pcts .append (pcts )
1118- for s , amm in enumerate (p .amm_list ):
1119- if isinstance (amm , StableSwapPoolState ):
1120- if amm_approx_list [s ][0 ] == "linear" and max (amm_pcts [s ][0 ], amm_pcts [s ][1 ]) > 1e-5 :
1121- amm_approx_list [s ][0 ] = "full"
1122- approx_adjusted_ct += 1
1123- for j in range (len (amm .asset_list )): # evaluate each asset constraint
1124- if amm_approx_list [s ][j + 1 ] == "linear" and amm_pcts [s ][j + 2 ] > 1e-5 :
1125- amm_approx_list [s ][j + 1 ] = "full"
1126- approx_adjusted_ct += 1
1127- elif isinstance (amm , ConstantProductPoolState ):
1128- if amm_approx_list [s ][0 ] == "linear" and max (amm_pcts [s ]) > 1e-5 :
1129- amm_approx_list [s ][0 ] = "full"
1130- approx_adjusted_ct += 1
1131- else :
1132- raise AssertionError ("Unrecognized AMM type" )
1060+ force_omnipool_approx [tkn ] = "full" # don't actually want to force linear approximation
1061+ approx_adjusted = True
1062+
1063+ for _i , amm_constraints in enumerate (p .amm_constraints ):
1064+ old_approx = amm_approx_list [_i ]
1065+ amm_approx_list [_i ] = amm_constraints .upgrade_approx (amm_deltas [_i ], old_approx )
1066+ if old_approx != amm_approx_list [_i ]:
1067+ approx_adjusted = True
11331068
11341069 # once solution is found, re-run with directional flags
11351070 if do_directional_run :
0 commit comments