def calLinePassPoint(A, B): # version 2 result = set() lineAB_Equ = calLineEqut(A, B) x, y, z = z3.Reals("x y z") cal = z3.Solver() cal.add(lineAB_Equ[0] == x, lineAB_Equ[1] == y, lineAB_Equ[2] == z) cal.push() minX, maxX = sorted([A[0], B[0]]) minY, maxY = sorted([A[1], B[1]]) minZ, maxZ = sorted([A[2], B[2]]) for a1 in range(minX, maxX + 1): for b1 in range(minY, maxY + 1): for c1 in range(minZ, maxZ + 1): cal.pop() cal.push() cal.add(x >= a1, x < a1 + 1) cal.add(y >= b1, y < b1 + 1) cal.add(z >= c1, z < c1 + 1) if cal.check() != z3.sat: continue m = cal.model() tmp_point = [ floatSymbolToInt(m[x]), floatSymbolToInt(m[y]), floatSymbolToInt(m[z]) ] result.add(tuple(tmp_point)) return result
def test_1(): x1, x2, x3 = z3.Reals('x1 x2 x3') x1_f = z3.Bool('x1_f') expr = z3.And((70 / 17 * x1 + 2 * x2) < 10, x1_f, (x1 > 10)) print(expr) vocab = Du.Vocab() tree = Du.ast_to_node(expr, vocab) print(tree) vocab.save("test_1_vocab.json")
def checkDeterminant(det_D_1): print(det_D_1) """ z3 symbol declaration. """ p1, p2, p3, p4 = z3.Reals('p1 p2 p3 p4') q1, q2, q3, q4 = z3.Reals('q1 q2 q3 q4') """ solve with refutation. f >= 0 ? If no, then we get f<0. """ s = z3.Solver() # s.add(p1>=0,p1<1) # s.add(p2>=0,p2<1) s.add(p3 > 0, p3 <= 1) # s.add(p4>0,p4<=1) s.add(q1 > 0, q1 < 1) s.add(q2 > 0, q2 < 1) s.add(q3 > 0, q3 < 1) s.add(q4 > 0, q4 < 1) f = eval(det_D_1) s.add(f >= 0) result = s.check() print(result)
def tangent_to_circle_and_line( fillet_radius: float, circle_radius: Point2, circle_center: Point2, line_point, line_angle, prefered_fillet_center_direction=None, ) -> typing.List[Point2]: fillet_center = z3.Reals("x y") solver = z3.Solver() solver.add( assert_tangent_to_circle(fillet_center, fillet_radius, circle_radius, circle_center)) solver.add( assert_tangent_to_line(fillet_center, fillet_radius, line_point, line_angle)) solutions = all_solutions_point2(solver, fillet_center) if prefered_fillet_center_direction is None: return solutions else: return choose_solution_in_direction(solutions, prefered_fillet_center_direction)
""" Theorem 6. Every pure strategy response to Mischief strategy receives the same average payoff. We write all values in Extrema.xlsx to F, and solve it with z3. """ import z3 q1, q2, q3, q4 = z3.Reals('q1 q2 q3 q4') R, T, S, P = z3.Reals('R T S P') F0 = (-P*q2 + P + T*q4)/(-q2 + q4 + 1) F1 = (-P*q2 + P - R*q2*q4 + R*q4 + S*q2*q4 - S*q2 - S*q4 + S + T*q1*q4 - T*q3*q4 + T*q3)/(q1*q4 - 2*q2 - q3*q4 + q3 + 2) F2 = (-P*q1*q2 + P*q2*q3 - P*q3 + P + R*q2*q4 - S*q2*q4 + S*q4 + T*q4)/(-q1*q2 + q2*q3 - q3 + 2*q4 + 1) F3 = (-P*q1*q2 + P*q2*q3 - P*q3 + P + R*q2*q3 - R*q3*q4 + R*q4 - S*q1*q2 + S*q1*q4 - S*q4 + S + T*q1*q4 - T*q3*q4 + T*q3)/(2*(-q1*q2 + q1*q4 + q2*q3 - q3*q4 + 1)) F4 = (-P*q2 + P + T*q4)/(-q2 + q4 + 1) F5 = (-P*q2*q3 + P*q3 - R*q2*q3 + R*q3 + S*q2*q4 - S*q2 - S*q4 + S + T*q1*q3)/(q1*q3 - 2*q2*q3 + q2*q4 - q2 + 2*q3 - q4 + 1) F6 = (-P*q1*q3 + P*q3 + R*q3*q4 - S*q2*q4 + S*q4 + T*q3*q4)/(-q1*q3 - q2*q4 + 2*q3*q4 + q3 + q4) F7 = (-P*q1*q3 + P*q3 + R*q3 - S*q1*q2 + S*q1*q4 - S*q4 + S + T*q1*q3)/(-q1*q2 + q1*q4 + 2*q3 - q4 + 1) F8 = (P*q2 - P - T*q4)/(q2 - q4 - 1) F9 = (P*q1*q2 - P*q1 - P*q2 + P - R*q2*q4 + R*q4 + S*q1*q2 - S*q1 - S*q2 + S - T*q1*q3 + T*q3)/(2*q1*q2 - q1*q3 - 2*q1 - q2*q4 - 2*q2 + q3 + q4 + 2) F10 = (P*q1*q3 - P*q1 - P*q3 + P + R*q2*q4 - S*q1*q4 + S*q4 - T*q1*q4 + T*q4)/(q1*q3 - 2*q1*q4 - q1 + q2*q4 - q3 + 2*q4 + 1) F11 = (P*q1*q3 - P*q1 - P*q3 + P + R*q2*q3 - R*q3*q4 + R*q4 - S*q1 + S - T*q1*q3 + T*q3)/(-2*q1 + q2*q3 - q3*q4 + q4 + 2) F12 = -1 F13 = (-R*q3 + S*q1 - S)/(q1 - q3 - 1) F14 = (R*q3 - S*q1 + S)/(-q1 + q3 + 1) F15 = (R*q3 - S*q1 + S)/(-q1 + q3 + 1) F = [F0,F1,F2,F3,F5,F6,F7,F9,F10,F11,F15]
def execute(trial=False): print("Starting new_station algorithm.") startTime = datetime.datetime.now() trial_size = 25 num_clusters = 50 max_dist = 0.04 min_num_close_stations = 2 # num_results = 5 # Set up the database connection. client = dml.pymongo.MongoClient() repo = client.repo repo.authenticate('kgarber', 'kgarber') repo.dropCollection("bluebikes.new_station") repo.createCollection("bluebikes.new_station") # read from mongodb print("Reading stations from DB.") stations = [st for st in repo['kgarber.bluebikes.stations'].find()] if trial: print("Running in trial mode.") stations = np.random.choice(stations, size=trial_size) # [[longitude, latitude], ...] stations = [[ st["location"]["geometry"]["coordinates"][0], st["location"]["geometry"]["coordinates"][1] ] for st in stations] # filter to make sure coordinates are correct (we have some bad data) stations = [ st for st in stations if -75 < st[0] < -70 and 40 < st[1] < 45 ] if trial: clusters = stations else: # get 100 clusters of stations, # any more than that is too hard for the optimizer. # also, clustering helps us remove weight from dense station areas. print("Running k-means.") clusters, _ = cluster.vq.kmeans(stations, num_clusters) num_pts = len(clusters) # objective variables z3.set_option(precision=6) o_lon, o_lat = z3.Reals('o_lon o_lat') # distances from new station to all other points distances = [ linear_distance(o_lon, o_lat, cl[0], cl[1]) for cl in clusters ] dist_sum = sum(distances) # restrict how far the station gets placed from a few other stations # prevents bad network branching num_close_enough = sum([z3.If(d < max_dist, 1, 0) for d in distances]) has_close_enough = num_close_enough > min_num_close_stations # set up the optimization solver opt = z3.Optimize() # make sure we don't place the station too far away from a few other stations opt.add(has_close_enough) # keep the station out of the atlantic ocean opt.add(o_lon < -70.032) # maximize the distance to other stations print("Running optimization...") maximized = opt.maximize(dist_sum) if (opt.check() == z3.sat): m = opt.model() print("Longitude:", m[o_lon].as_decimal(5)) print("Latitude:", m[o_lat].as_decimal(5)) repo['kgarber.bluebikes.new_station'].insert({ "longitude": m[o_lon].numerator_as_long() / m[o_lon].denominator_as_long(), "latitude": m[o_lat].numerator_as_long() / m[o_lat].denominator_as_long() }) # indicate that the collection is complete repo['kgarber.bluebikes.new_station'].metadata({'complete': True}) else: print("No result for optimization...") repo.logout() endTime = datetime.datetime.now() print("Finished new_station algorithm.") return {"start": startTime, "end": endTime}
import z3 if __name__ == '__main__': x, y = z3.Reals('x y') s = z3.Solver() s.add(x > 1) s.add(y > 1) s.add(z3.Or(x + y > 3, x - y < 2)) print('Printing constrains...') for c in s.assertions(): print(c) # print(s.statistics()) print(s.check()) m = s.model() for d in m.decls(): print(f'{d.name()} := {m[d]}')
""" You are given 5 positive real numbers, a<b<c<d<e, with the property that the total sum of their squares is equal to the total sum of their pairwise products. - Prove that we can choose an ordered triplet of numbers from this set (x,y,z) such that x<y<z, and such that it is not possible to form a triangle with these side lengths. - Prove that the number of such ordered triplets is at least 6. Z3 FAILED This is another one that Z3 was unable to solve -- due to the quadratic (not decidable) real number arithmetic constraints. """ import z3 s = z3.Optimize() a, b, c, d, e = z3.Reals("a b c d e") # Hard constraints s.add(0 < a, a < b, b < c, c < d, d < e) sum_squares = a*a + b*b + c*c + d*d + e*e sum_pairs = a*b + a*c + a*d + a*e + b*c + b*d + b*e + c*d + c*e + d*e s.add(sum_squares == sum_pairs) # Soft constraints # Each one states that the triangle inequality holds for a particular triple s.add_soft(a + b < c) s.add_soft(a + b < d) s.add_soft(a + b < e) s.add_soft(a + c < d) s.add_soft(a + c < e) s.add_soft(a + d < e) s.add_soft(b + c < d) s.add_soft(b + c < e)
import polya import z3 import timeit import polya.main.messages as messages import sys import fractions Forall, And, Implies = z3.ForAll, z3.And, z3.Implies #################################################################################################### # # These are the examples discussed in section 6 of the paper. # #################################################################################################### a, b, c, d, e, i, K, m, n = z3.Reals('a b c d e i K m n') r, s, t, u, v, w, x, y, z = z3.Reals('r s t u v w x y z') eps = z3.Real('eps') f = z3.Function('f', z3.RealSort(), z3.RealSort()) h = z3.Function('h', z3.RealSort(), z3.RealSort(), z3.RealSort()) g = z3.Function('g', z3.RealSort(), z3.RealSort(), z3.RealSort(), z3.RealSort()) log = z3.Function('log', z3.RealSort(), z3.RealSort()) exp = z3.Function('exp', z3.RealSort(), z3.RealSort()) ceil = z3.Function('ceil', z3.RealSort(), z3.RealSort()) abs = z3.Function('abs', z3.RealSort(), z3.RealSort()) # #mins = [z3.Function('min2', z3.RealSort(), z3.RealSort(), z3.RealSort()), # z3.Function('min2', z3.RealSort(), z3.RealSort(), z3.RealSort(), z3.RealSort()), # z3.Function('min2', z3.RealSort(), z3.RealSort(), z3.RealSort(), z3.RealSort(),