Quick Tutorial
In this quick tutorial, we define two algorithms and show that they are oracle-equivalent.
The first algorithm,
Import linnaeus
and Algorithm
class,
import linnaeus as lin
from linnaeus import Algorithm
Define the first algorithm as algo1
with name Algoritm 1
,
algo1 = Algorithm("Algorithm 1")
Add variables x1
, x2
, and x3
to algo1
,
x1, x2, x3 = algo1.add_var("x1", "x2", "x3")
Add oracle gradf
, gradient of f
to algo1
,
gradf = algo1.add_oracle("gradf")
Add update equations:
# x3 <- 2x1 - x2
algo1.add_update(x3, 2*x1 - x2)
# x2 <- x1
algo1.add_update(x2, x1)
# x1 <- x3 - 1/10*gradf(x3)
algo1.add_update(x1, x3 - 1/10*gradf(x3))
Parse algo1
:
algo1.parse()
System parses algo1
and returns the update equations,
The second algorithm,
Define the second algorithm as algo2
and parse it:
algo2 = Algorithm("Algorithm 2")
xi1, xi2, xi3 = algo2.add_var("xi1", "xi2", "xi3")
gradf = algo2.add_oracle("gradf")
# xi3 <- xi1
algo2.add_update(xi3, xi1)
# xi1 <- xi1 - xi2 - 1/5*gradf(xi1)
algo2.add_update(xi1, xi1 - xi2 - 1/5*gradf(xi3))
# xi2 <- xi2 + 1/10*gradf(xi3)
algo2.add_update(xi2, xi2 + 1/10*gradf(xi3))
algo2.parse()
System parses algo2
and returns the update equations,
Check oracle equivalence :
lin.is_equivalent(algo1, algo2)
System returns
True
which shows that algo1
and algo2
are oracle-equivalent.