Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maybe wolfe #6

Merged
merged 10 commits into from
May 10, 2024
Prev Previous commit
Next Next commit
Still getting different ouputs for different executions of arbitrage.mpc
  • Loading branch information
Mikerah committed Apr 29, 2024
commit b6a116a53d7b163071fcd1f3839a616781bb0f91
3 changes: 3 additions & 0 deletions arbitrage.mpc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ sfix.set_precision(f=16, k=40)
program.use_edabit(True)
program.use_split(4)

program.use_trunc_pr = False

# Create pools
#reserves_eq = sfix.Array(2)
reserves_eq = Matrix(2, 1, sfix)
Expand Down Expand Up @@ -49,6 +51,7 @@ net_trade = router.netflows()
price_vector_tmp = Matrix(1, 2, sfix)
price_vector_tmp.assign(router.v)
print_ln("%s", router.v.reveal())
net_trade.print_reveal_nested()
print_ln("Profit: %s", price_vector_tmp.dot(net_trade).reveal())
#print_ln_to(0, "Net trade: ")
#net_trade.print_reveal_nested()
Expand Down
3 changes: 2 additions & 1 deletion cfmms.mpc
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
"""

from Compiler import mpc_math

"""
sfix.round_nearest = True
cfix.set_precision(f=16, k=40)
sfix.set_precision(f=16, k=40)

program.use_edabit(True)
program.use_split(4)
"""

class CFMM:
"""
Expand Down
27 changes: 9 additions & 18 deletions optimizers.mpc
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from Compiler import mpc_math, ml

sfix.round_nearest = True
"""
program.use_edabit(True)
program.use_split(4)
"""


sfix.round_nearest = True
cfix.set_precision(f=16, k=40)
sfix.set_precision(f=16, k=40)

"""
program.use_edabit(True)
"""

class Optimizer:

Expand All @@ -34,7 +35,7 @@ class SGDOptimizer(Optimizer):

def optimize(self, initial_guess, learning_rate=sfix(0.01), n_iter=10):
guess = sfix(initial_guess)
@for_range(n_iter)
@for_range_opt(n_iter)
def _(i):
guess.update(guess - learning_rate * self.function.grad(guess))

Expand Down Expand Up @@ -85,15 +86,13 @@ class L_BFGS_BOptimizer(Optimizer):
def _():
break_loop()

def optimize(self, initial_guess, fn=None, g=None, memory_size=17, threshold=cfix(0.0001), alpha=MemValue(cfix(0.5)), rho=cfix(0.5), armijo_c=cfix(0.0001), c_2=cfix(0.9), n_iter=10):
def optimize(self, initial_guess, fn=None, g=None, threshold=cfix(0.0001), alpha=MemValue(cfix(0.5)), rho=cfix(0.5), armijo_c=cfix(0.0001), c_2=cfix(0.9), n_iter=10):
k = regint(0)

guess_k = Matrix(len(initial_guess), 1, sfix)
#guess_k = Array.create_from(initial_guess)
guess_k.assign(initial_guess)
#print("from optimizers.mpc")
#print(type(initial_guess))
grad_f_k = g(guess_k)
grad_f_k = Matrix(len(initial_guess), 1, sfix)
grad_f_k.assign(g(guess_k))
prev_guess_k = Matrix(len(initial_guess), 1, sfix)
prev_grad_k = Matrix(len(initial_guess), 1, sfix)

Expand All @@ -103,8 +102,6 @@ class L_BFGS_BOptimizer(Optimizer):

@for_range_opt(n_iter)
def _(i):
#print_ln("%s", self.function.grad(guess_k).reveal())
#print_ln("%s", threshold)
@if_e((self.function.norm(g(guess_k)) > threshold).reveal())
def _():
#print_ln("%s", (self.function.norm(self.function.grad(guess_k)) > threshold).reveal())
Expand All @@ -117,12 +114,6 @@ class L_BFGS_BOptimizer(Optimizer):
tmp_grad_f_k = Matrix(len(grad_f_k), 1, sfix)
tmp_grad_f_k.assign(grad_f_k)
p_vec_k.assign(invH.dot(tmp_grad_f_k))
#print_ln("%s", invH.dot(grad_f_k).reveal())
print_ln("%s", p_vec_k.reveal())
#invH.dot(grad_f_k).print_reveal_nested()

# Compute line search using backtracking
# It's okay to reveal here since we aren't revealing guess_k itself

self.update_alpha(guess_k, p_vec_k, rho, alpha, armijo_c, c_2, n_iter=10, fn=fn, g=g)

Expand Down
8 changes: 5 additions & 3 deletions router.mpc
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
exec(open("../cfmms.mpc").read())
exec(open("../optimizers.mpc").read())

"""
sfix.round_nearest = True
cfix.set_precision(f=16, k=40)
sfix.set_precision(f=16, k=40)

program.use_edabit(True)
program.use_split(4)
"""

class ArbitrageRouter:

Expand Down Expand Up @@ -51,7 +53,7 @@ class ArbitrageRouter:
#NB: Maybe there might be a way to leverage this to make the computation faster. To be explored later.
def fn(v):
tmp = Matrix(len(self.v), 1, sfix)
@for_range(len(self.v))
@for_range_opt(len(self.v))
def _(i):
tmp[i].assign(((v[i][0] <= self.v[i][0]).if_else(0, 1)))

Expand Down Expand Up @@ -80,7 +82,7 @@ class ArbitrageRouter:

tmp = Matrix(len(self.v), 1, sfix)
#tmp = []
@for_range(len(self.v))
@for_range_opt(len(self.v))
def _(i):
tmp[i].assign(((v[i][0] <= self.v[i][0]).if_else(0, 1)))

Expand Down Expand Up @@ -111,7 +113,7 @@ class ArbitrageRouter:


self.find_arb(self.v)
new_price_vector = optimizer.optimize(self.v, fn, g, threshold=cfix(1))[0]
new_price_vector = optimizer.optimize(self.v, fn, g)[0]
#print_ln("router.v: %s", new_price_vector.reveal())
self.v.assign(new_price_vector)
#print("from router.route")
Expand Down