From 2a71914ed8bbbb5f30200a66ac7a4e71a5b0f301 Mon Sep 17 00:00:00 2001 From: "John K. Luebs" Date: Thu, 22 Feb 2024 20:58:47 -0600 Subject: [PATCH] ok --- t.lua | 30 ++++++++ tmp/enaml.lua | 204 ++++++++++++++++++++++++++++++++++++++++++++++++++ tmp/ex.lua | 90 ++++++++++++++++++++++ tmp/ex2.lua | 28 +++++++ tmp/ex3.lua | 49 ++++++++++++ tmp/ex4.lua | 37 +++++++++ tmp/ex5.lua | 41 ++++++++++ tmp/t.lua | 26 +++++++ tmp/test.lua | 66 ++++++++++++++++ 9 files changed, 571 insertions(+) create mode 100644 t.lua create mode 100644 tmp/enaml.lua create mode 100644 tmp/ex.lua create mode 100644 tmp/ex2.lua create mode 100644 tmp/ex3.lua create mode 100644 tmp/ex4.lua create mode 100644 tmp/ex5.lua create mode 100644 tmp/t.lua create mode 100644 tmp/test.lua diff --git a/t.lua b/t.lua new file mode 100644 index 0000000..665c382 --- /dev/null +++ b/t.lua @@ -0,0 +1,30 @@ +local kiwi = require("kiwi") + +local c +--debug.getupvalue +do + local v1 = kiwi.Var("v1") + local v2 = kiwi.Var("v2") + local v3 = kiwi.Var("v3") + local v4 = kiwi.Var("v4") + local v5 = kiwi.Var("v5") + local c = (3 * v1 + 4 * v2 + 6 * v3):eq(0) + + local e = c:expression() + print(e) +end + +collectgarbage("collect") +collectgarbage("collect") +-- c = (3 * v1 + 4 * v2 + 6 * v3):eq(0) + +-- local t = c:expression():terms() +-- print(t[2].var) + +-- for k, v in ipairs(t) do +-- print(k, v.var, v.coefficient) +-- end + +-- for k, v in pairs(kiwi.ErrKind) do +-- print(k, v) +-- end diff --git a/tmp/enaml.lua b/tmp/enaml.lua new file mode 100644 index 0000000..0cc1152 --- /dev/null +++ b/tmp/enaml.lua @@ -0,0 +1,204 @@ +local kiwi = require("kiwi") + +local Var = kiwi.Var + +---@param solver kiwi.Solver +local function build_solver(solver) + -- create custom strength + -- + local width = Var("width") + local height = Var("height") + + local mmedium = kiwi.strength.create(0.0, 1.0, 0.0, 1.25) + local smedium = kiwi.strength.create(0.0, 100, 0.0) + local left = Var("left") + local top = Var("top") + local contents_top = Var("contents_top") + local contents_bottom = Var("contents_bottom") + local contents_left = Var("contents_left") + local contents_right = Var("contents_right") + local midline = Var("midline") + local ctleft = Var("ctleft") + local ctheight = Var("ctheight") + local cttop = Var("cttop") + local ctwidth = Var("ctwidth") + local lb1left = Var("lb1left") + local lb1height = Var("lb1height") + local lb1top = Var("lb1top") + local lb1width = Var("lb1width") + local lb2left = Var("lb2left") + local lb2height = Var("lb2height") + local lb2top = Var("lb2top") + local lb2width = Var("lb2width") + local lb3left = Var("lb3left") + local lb3height = Var("lb3height") + local lb3top = Var("lb3top") + local lb3width = Var("lb3width") + local fl1left = Var("fl1left") + local fl1height = Var("fl1height") + local fl1top = Var("fl1top") + local fl1width = Var("fl1width") + local fl2left = Var("fl2left") + local fl2height = Var("fl2height") + local fl2top = Var("fl2top") + local fl2width = Var("fl2width") + local fl3left = Var("fl3left") + local fl3height = Var("fl3height") + local fl3top = Var("fl3top") + local fl3width = Var("fl3width") + + solver:add_edit_var(width, kiwi.strength.STRONG) + solver:add_edit_var(height, kiwi.strength.STRONG) + + local strong, medium, weak = kiwi.strength.STRONG, kiwi.strength.MEDIUM, kiwi.strength.WEAK + local constraints = { + (left + -0):ge(0), + (height + 0):eq(0, medium), + (top + -0):ge(0), + (width + -0):ge(0), + (height + -0):ge(0), + (-top + contents_top + -10):eq(0), + (lb3height + -16):eq(0, strong), + (lb3height + -16):ge(0, strong), + (ctleft + -0):ge(0), + (cttop + -0):ge(0), + (ctwidth + -0):ge(0), + (ctheight + -0):ge(0), + (fl3left + -0):ge(0), + (ctheight + -24):ge(0, smedium), + (ctwidth + -1.67772e+07):le(0, smedium), + (ctheight + -24):le(0, smedium), + (fl3top + -0):ge(0), + (fl3width + -0):ge(0), + (fl3height + -0):ge(0), + (lb1width + -67):eq(0, weak), + (lb2width + -0):ge(0), + (lb2height + -0):ge(0), + (fl2height + -0):ge(0), + (lb3left + -0):ge(0), + (fl2width + -125):ge(0, strong), + (fl2height + -21):eq(0, strong), + (fl2height + -21):ge(0, strong), + (lb3top + -0):ge(0), + (lb3width + -0):ge(0), + (fl1left + -0):ge(0), + (fl1width + -0):ge(0), + (lb1width + -67):ge(0, strong), + (fl2left + -0):ge(0), + (lb2width + -66):eq(0, weak), + (lb2width + -66):ge(0, strong), + (lb2height + -16):eq(0, strong), + (fl1height + -0):ge(0), + (fl1top + -0):ge(0), + (lb2top + -0):ge(0), + (-lb2top + lb3top + -lb2height + -10):eq(0, mmedium), + (-lb3top + -lb3height + fl3top + -10):ge(0), + (-lb3top + -lb3height + fl3top + -10):eq(0, mmedium), + (contents_bottom + -fl3height + -fl3top + -0):eq(0, mmedium), + (fl1top + -contents_top + 0):ge(0), + (fl1top + -contents_top + 0):eq(0, mmedium), + (contents_bottom + -fl3height + -fl3top + -0):ge(0), + (-left + -width + contents_right + 10):eq(0), + (-top + -height + contents_bottom + 10):eq(0), + (-left + contents_left + -10):eq(0), + (lb3left + -contents_left + 0):eq(0, mmedium), + (fl1left + -midline + 0):eq(0, strong), + (fl2left + -midline + 0):eq(0, strong), + (ctleft + -midline + 0):eq(0, strong), + (fl1top + 0.5 * fl1height + -lb1top + -0.5 * lb1height + 0):eq(0, strong), + (lb1left + -contents_left + 0):ge(0), + (lb1left + -contents_left + 0):eq(0, mmedium), + (-lb1left + fl1left + -lb1width + -10):ge(0), + (-lb1left + fl1left + -lb1width + -10):eq(0, mmedium), + (-fl1left + contents_right + -fl1width + -0):ge(0), + (width + 0):eq(0, medium), + (-fl1top + fl2top + -fl1height + -10):ge(0), + (-fl1top + fl2top + -fl1height + -10):eq(0, mmedium), + (cttop + -fl2top + -fl2height + -10):ge(0), + (-ctheight + -cttop + fl3top + -10):ge(0), + (contents_bottom + -fl3height + -fl3top + -0):ge(0), + (cttop + -fl2top + -fl2height + -10):eq(0, mmedium), + (-fl1left + contents_right + -fl1width + -0):eq(0, mmedium), + (-lb2top + -0.5 * lb2height + fl2top + 0.5 * fl2height + 0):eq(0, strong), + (-contents_left + lb2left + 0):ge(0), + (-contents_left + lb2left + 0):eq(0, mmedium), + (fl2left + -lb2width + -lb2left + -10):ge(0), + (-ctheight + -cttop + fl3top + -10):eq(0, mmedium), + (contents_bottom + -fl3height + -fl3top + -0):eq(0, mmedium), + (lb1top + -0):ge(0), + (lb1width + -0):ge(0), + (lb1height + -0):ge(0), + (fl2left + -lb2width + -lb2left + -10):eq(0, mmedium), + (-fl2left + -fl2width + contents_right + -0):eq(0, mmedium), + (-fl2left + -fl2width + contents_right + -0):ge(0), + (lb3left + -contents_left + 0):ge(0), + (lb1left + -0):ge(0), + (0.5 * ctheight + cttop + -lb3top + -0.5 * lb3height + 0):eq(0, strong), + (ctleft + -lb3left + -lb3width + -10):ge(0), + (-ctwidth + -ctleft + contents_right + -0):ge(0), + (ctleft + -lb3left + -lb3width + -10):eq(0, mmedium), + (fl3left + -contents_left + 0):ge(0), + (fl3left + -contents_left + 0):eq(0, mmedium), + (-ctwidth + -ctleft + contents_right + -0):eq(0, mmedium), + (-fl3left + contents_right + -fl3width + -0):eq(0, mmedium), + (-contents_top + lb1top + 0):ge(0), + (-contents_top + lb1top + 0):eq(0, mmedium), + (-fl3left + contents_right + -fl3width + -0):ge(0), + (lb2top + -lb1top + -lb1height + -10):ge(0), + (-lb2top + lb3top + -lb2height + -10):ge(0), + (lb2top + -lb1top + -lb1height + -10):eq(0, mmedium), + (fl1height + -21):eq(0, strong), + (fl1height + -21):ge(0, strong), + (lb2left + -0):ge(0), + (lb2height + -16):ge(0, strong), + (fl2top + -0):ge(0), + (fl2width + -0):ge(0), + (lb1height + -16):ge(0, strong), + (lb1height + -16):eq(0, strong), + (fl3width + -125):ge(0, strong), + (fl3height + -21):eq(0, strong), + (fl3height + -21):ge(0, strong), + (lb3height + -0):ge(0), + (ctwidth + -119):ge(0, smedium), + (lb3width + -24):eq(0, weak), + (lb3width + -24):ge(0, strong), + (fl1width + -125):ge(0, strong), + } + + for _, c in ipairs(constraints) do + print(c) + solver:add_constraint(c) + end + + return width, height +end + +local function main() + local sizes = { + { w = 400, h = 600 }, + { w = 600, h = 400 }, + { w = 800, h = 1200 }, + { w = 1200, h = 800 }, + { w = 400, h = 800 }, + { w = 800, h = 400 }, + } + + for i = 1, 1 do + local solver = kiwi.Solver() + + local width, height = build_solver(solver) + + for _, size in ipairs(sizes) do + solver:suggest_value(width, size.w) + solver:suggest_value(height, size.h) + solver:update_vars() + print(width:value(), height:value()) + end + end +end + +main() +collectgarbage("collect") +collectgarbage("collect") +collectgarbage("collect") +collectgarbage("collect") diff --git a/tmp/ex.lua b/tmp/ex.lua new file mode 100644 index 0000000..1441ce2 --- /dev/null +++ b/tmp/ex.lua @@ -0,0 +1,90 @@ +local kiwi = require("kiwi") +local Var = kiwi.Var + +local Button = setmetatable({}, { + __call = function(_, identifier) + return setmetatable({ + left = Var(identifier .. " left"), + width = Var(identifier .. " width"), + }, { + __tostring = function(self) + return "Button(" .. self.left:value() .. ", " .. self.width:value() .. ")" + end, + }) + end, +}) + +local b1 = Button("b1") +local b2 = Button("b2") + +local left_edge = Var("left") +local right_edge = Var("width") + +local STRONG = kiwi.Strength.STRONG + +local constraints + +for i = 1, 200 do + q = kiwi.Term(left_edge, 2.0) +end + +for i = 1, 50, 1 do +-- stylua: ignore start +constraints = { + left_edge :eq(0.0), + -- two buttons are the same width + b1.width :eq(b2.width), + -- button1 starts 50 for the left margin + (1.00 * b1.left) :eq(left_edge + 50), + -- button2 ends 50 from the right margin + right_edge :eq(b2.left + b2.width + 50), + -- button2 starts at least 100 from the end of button1. This is the "elastic" constraint + b2.left :ge(b1.left + b1.width + 100), + -- button1 has a minimum width of 87 + b1.width :ge(87), + -- button1 has a preferred width of 87 + b1.width :eq(87, STRONG), + -- button2 has minimum width of 113 + b2.width :ge(113), + -- button2 has a preferred width of 113 + b2.width :eq(113, STRONG), +} + -- stylua: ignore end + -- + k = kiwi.Strength.create(0.0, 1.0, 0.0, 1.25) +end +local solver = kiwi.Solver() + +for _, c in ipairs(constraints) do + print(c) + solver:add_constraint(c) +end + +solver:update_vars() + +print(b1) -- Button(50, 113) +print(b2) -- Button(263, 113) +print(left_edge:value()) -- 0 +print(right_edge:value()) -- 426 + +solver:add_edit_var(right_edge, STRONG) +solver:suggest_value(right_edge, 500) +solver:update_vars() +-- solver:dump() +-- print(b1) -- Button(50, 113) +-- print(b2) -- Button(337, 113) +-- print(right_edge:value()) -- 500 + +-- print(solver:dumps()) + +-- solver = kiwi.Solver() + +-- local trailing = Var("trailing") +-- local leading = Var("leading") + +-- solver:add_constraint(kiwi.new_single_constraint(trailing, 86)) +-- solver:add_constraint(kiwi.new_pair_constraint(trailing, leading, 8.0)) +-- print(solver:dumps()) +-- solver:update_vars() +-- print(trailing:value()) -- 86 +-- print(leading:value()) -- 76 diff --git a/tmp/ex2.lua b/tmp/ex2.lua new file mode 100644 index 0000000..f28e79f --- /dev/null +++ b/tmp/ex2.lua @@ -0,0 +1,28 @@ +local kiwi = require("kiwi") + +local Var = kiwi.Var + +local function doit(v, i) + v:set(i) +end + +local v = Var("fuckit") + +local s = kiwi.Solver() + +local e +for i = 1, 1000 do + e = kiwi.Expression(5.0, kiwi.Term(v, 3.0), kiwi.Term(v, 2.0)) +end +--local e = (v + 1) + +s:add_constraint(e:eq(100)) + +print(v:value()) + +s:update_vars() + +v:set_name("fuck") +print(v:value()) + +s:dump() diff --git a/tmp/ex3.lua b/tmp/ex3.lua new file mode 100644 index 0000000..14a0a00 --- /dev/null +++ b/tmp/ex3.lua @@ -0,0 +1,49 @@ +local kiwi = require("kiwi") +local ffi = require("ffi") + +local t +local e +local strformat = string.format +local Strength = kiwi.Strength + +--local bad_str = "-1 v8 + -1 v4 + -1 v2 + -4 v9 + -2 v3 + -1 v6 + -1 v5 + -1 v1 + -2 v7 + -5" +-- local b = "" + +function s(self) + local ops = { + [0] = "<=", + ">=", + "==", + } + local strengths = { + [Strength.REQUIRED] = "required", + [Strength.STRONG] = "strong", + [Strength.MEDIUM] = "medium", + [Strength.WEAK] = "weak", + } + local strength = self:strength() + --local e = self:expression() + local s = strformat("%s %s %s", "aaa", tostring(self:expression()), tonumber(self:op())) + return s +end + +for i = 1, 2000 do + local v1 = kiwi.Var("v1") + local v2 = kiwi.Var("v2") + local v3 = kiwi.Var("v3") + local v4 = kiwi.Var("v4") + + local v5 = kiwi.Var("v5") + local v6 = kiwi.Var("v6") + local v7 = kiwi.Var("v7") + local v8 = kiwi.Var("v8") + local v9 = kiwi.Var("v9") + local v10 = kiwi.Var("v9") + + do + -- + local c = ((-(v1 + v2 + 2 * v3 + v4 + 3)):eq(v5 + v6 + 2 * v7 + v8 + 4 * v9 + 3)) + s(c) + end +end +print(t) diff --git a/tmp/ex4.lua b/tmp/ex4.lua new file mode 100644 index 0000000..cadd0b6 --- /dev/null +++ b/tmp/ex4.lua @@ -0,0 +1,37 @@ +local kiwi = require("kiwi") +local ffi = require("ffi") + +local Var = kiwi.Var + +local v1 = Var("v1") +local v2 = Var("v2") +local v3 = Var("v3") +local v4 = Var("v4") +local v5 = Var("v5") +local v6 = Var("v6") + +local f1 = kiwi.f1 +local f2 = kiwi.f2 + +local single = kiwi.constraints.single + +local function execute_times(f, times) + local begin = os.clock() + for _ = 1, times do + f() + end + local finish = os.clock() + return finish - begin +end + +local t = execute_times(function() + return kiwi.constraints.pair_ratio(v1, 2.0, v2, 3.0) +end, 6000000) + +print(t) + +-- local t = execute_times(function() +-- return pair_ratio2(v1, 2.0, v2, 3.0) +-- end, 2000000) + +-- print(t) diff --git a/tmp/ex5.lua b/tmp/ex5.lua new file mode 100644 index 0000000..5733904 --- /dev/null +++ b/tmp/ex5.lua @@ -0,0 +1,41 @@ +local kiwi = require("kiwi") + +local Var = kiwi.Var + +x1 = Var("x1") +x2 = Var("x2") +xm = Var("xm") + +local constraints = { x1:ge(0), x2:le(100), x2:ge(x1 + 10), xm:eq((x1 + x2) / 2) } + +local solver = kiwi.Solver() + +for _, c in ipairs(constraints) do + solver:add_constraint(c) +end + +local c = kiwi.constraints.single(x1, 40, "EQ", kiwi.Strength.WEAK) +solver:add_constraint(c) + +constraints[#constraints + 1] = c + +solver:add_edit_var(xm, kiwi.Strength.STRONG) + +solver:suggest_value(xm, 60) + +solver:update_vars() + +print(xm:value(), x1:value(), x2:value()) + +for _, c in ipairs(constraints) do + print(c, c:violated()) +end + +solver:suggest_value(xm, 90) +solver:update_vars() + +print(xm:value(), x1:value(), x2:value()) + +for _, c in ipairs(constraints) do + print(c, c:violated()) +end diff --git a/tmp/t.lua b/tmp/t.lua new file mode 100644 index 0000000..8ca4132 --- /dev/null +++ b/tmp/t.lua @@ -0,0 +1,26 @@ +local k = require("kiwi") + +s = k.Solver() + +--s:set_error_mask({ "KiwiErrUnknownEditVariable" }, false) + +local v1 = k.Var("v1") +local v2 = k.Var("v2") +local v3 = k.Var("v3") +local v4 = k.Var("v4") + +print((2 * v1 - 5 * v2)) + +os.exit() +local c1 = k.constraints.pair_ratio(v1, -4, v2, 22, "GE", 1000) +local c2 = k.constraints.pair_ratio(v3, -6, v4, 29, "LE", 1000) + +local vo = c1:expression():terms()[1].var +print(v1 == vo) +-- print(v1:name()) +-- print(v1 * 5) + +local e = k.Expression(6, k.Term(v1, 2), k.Term(v2, 3)) +--print(e) + +--print(k.strength.MEDIUM) diff --git a/tmp/test.lua b/tmp/test.lua new file mode 100644 index 0000000..0ba88e2 --- /dev/null +++ b/tmp/test.lua @@ -0,0 +1,66 @@ +local kiwi = require("kiwi") + +local Var = kiwi.Var +local Strength = kiwi.Strength + +local Button_mt = {} + +---@class Button +---@field left kiwi.Var +---@field width kiwi.Var +---@overload fun(identifier: string): Button +local Button = setmetatable({}, Button_mt) + +function Button_mt.__call(_, identifier) + return setmetatable({ + left = Var("left" .. identifier), + width = Var("width" .. identifier), + }, { + __index = Button, + __tostring = function(self) + return "Button(" .. self.left:value() .. ", " .. self.width:value() .. ")" + end, + }) +end + +local b1 = Button("b1") +local b2 = Button("b2") + +local left_limit = kiwi.Var("left") +local right_limit = kiwi.Var("width") + + +-- stylua: ignore start +local constraints = { + left_limit :eq( 0 ), + b1.width :eq( b2.width ), + b1.left :eq( left_limit + 50), + right_limit:eq( b2.left + b2.width + 50), + b2.left :ge( b1.left + b1.width + 100), + + b1.width :ge( 87 ), + b1.width :eq( 87, Strength.STRONG), + + b2.width :ge( 113 ), + b2.width :eq( 113, Strength.STRONG), +} +-- stylua: ignore end + +local s = kiwi.Solver() + +for _, c in ipairs(constraints) do + s:add_constraint(c) +end + +print(constraints[4]:expression()) +print(constraints[5]:expression()) + +kiwi.Constraint(b1.left + 1, "EQ") + +s:update_vars() + +print(b1) +print(b2) +print(left_limit:value()) +print(right_limit:value()) +print(s:dumps())