Skip to content

Instantly share code, notes, and snippets.

View jonatanwestholm's full-sized avatar

Jonatan Westholm jonatanwestholm

  • Stockholm, Sweden
View GitHub Profile
fan_size = 1 # m^2
wind_speed = 5 # m/s
co2_concentration = 450e-6 # i.e. ppm
air_density = 1.3e-3 # kg/m^3
s_per_year = 3600 * 24 * 365 # s/y
co2_per_year_per_m2 = fan_size * wind_speed * air_density * \
co2_concentration * s_per_year # kg / y
print("CO2 captured per year per m^2 fan:", co2_per_year_per_m2, "kg")
from sugarrush.solver import SugarRush
"""Constrain
z = v[a]
where a is uintK,
z is uintN,
v is a vector of at most 2**K uintN
"""
@jonatanwestholm
jonatanwestholm / plus.py
Created February 8, 2021 19:55
How to use SugarRush.plus( )
from sugarrush.solver import SugarRush
"""Given three N-bit binary numbers a, b, z
constrain so that
a + b = z % 2**N
(unsigned N-bit integer addition)
"""
from sugarrush.solver import SugarRush
""" Given two intervals [a0, a1], [b0, b1],
constrain that they have no overlap.
"""
solver = SugarRush()
N = 8
a0 = [solver.var() for _ in range(N)]
a1 = [solver.var() for _ in range(N)]
def get_concentrations(r2c, p2k):
"""
Get the concentrations of the reactants at equilibrium
r2c: concentrations per reactant
p2k: equilibrium constants per product
products are tuples ((r0, a0), (r1, a1), ...)
of compounds and their correspondings cardinality, e.g
H20 = (("H", 2), ("O", 1))
def maxflow_lp(G, s, t, capacity):
"""
G is the flow graph
s is source
t is terminus
"""
solver = get_solver("CBC")
flows = {(u, v): solver.NumVar(lb=0, ub=cap)
for u, v, cap in G.edges(data=capacity)}
# The class of 'self' inherits nx.Graph
def apply_SAT(self, board):
while True:
# Let the trivial rules do the heavy lifting,
# for speed.
self.apply_trivial(board)
solver = SugarRush()
known_tiles = set([tile for tile in self
def apply_trivial(self, ground_truth):
while True:
for node in self.nodes:
if self.nodes[node]["adj"] is None:
# Tile is flagged or unopened:
# trivial rules not applicable.
continue
if not self.num_unknown_neigh(node):
# We already know everything about
def cross_switch(self, u0, u1):
G[u0], G[u1] = G[u1], G[u0]
u0 = reverse_cycle(G, u0)
G[u0], G[u1] = G[u1], G[u0]
def reverse_cycle(G, u0):
path = get_path(G, u0)
for i, j in zip(path, [path[-1]] + path[:-1]):
G[i] = j
return path[0]
G[u0], G[u1], G[u2] = G[u1], G[u2], G[u0]