Giter Club home page Giter Club logo

kiwi's Introduction

LUME Kiwi

LUME Kiwi is a fast TypeScript implementation of the Cassowary constraint solving algorithm, based on the seminal Cassowary paper. Originally created by Chris Colbert, it was redesigned from the ground up to be lightweight, fast and easy to maintain. View the benchmarks to see how it compares to Cassowary.js.

Soon it will be compiled to WebAssembly with AssemblyScript (TypeScript to WebAssembly compiler).

Index

Demo

Live demo on CodePen.

Install

Local Install

Install using NPM:

npm install @lume/kiwi

If you have a plain web app with no build, or a non-browser JS runtime that also supports import maps like Deno, you'll need to add @lume/kiwi to your importmap script so that the browser or JS runtime knows where to import kiwi from. F.e. something like this:

<script type="importmap">
  {
    "imports": {
      "@lume/kiwi": "./node_modules/@lume/kiwi/dist/kiwi.js"
    }
  }
</script>

CDN Install

Note, if using importmaps and native ES Modules in a browser, or in a JS runtime like Deno, you can get Kiwi directly from the UNPKG CDN without installing it locally (just as the live CodePen demo does):

<script type="importmap">
  {
    "imports": {
      "@lume/kiwi": "https://unpkg.com/@lume/[email protected]/dist/kiwi.js"
    }
  }
</script>

Usage

After installing, import kiwi into your project:

import * as kiwi from '@lume/kiwi'

console.log(kiwi)

// ...use kiwi...

The following example creates a solver which automatically calculates a width based on some constraints:

// Create a solver
var solver = new kiwi.Solver()

// Create edit variables
var left = new kiwi.Variable()
var width = new kiwi.Variable()
solver.addEditVariable(left, kiwi.Strength.strong)
solver.addEditVariable(width, kiwi.Strength.strong)
solver.suggestValue(left, 100)
solver.suggestValue(width, 400)

// Create and add a constraint
var right = new kiwi.Variable()
solver.addConstraint(new kiwi.Constraint(new kiwi.Expression([-1, right], left, width), kiwi.Operator.Eq))

// Solve the constraints
solver.updateVariables()

console.assert(right.value() === 500)

// later, update the constraints and re-calculate
setTimeout(() => {
  solver.suggestValue(left, 200)
  solver.suggestValue(width, 600)

  solver.updateVariables() // update

  console.assert(right.value() === 800)
}, 2000)

Documentation

Benchmarks

To run the benchmark in the browser, just visit this page.

To run the benchmark locally using nodejs, clone or download this repository and execute the following steps:

npm install
npm run bench

Statically serve the project, f.e. npx five-server . which opens a new browser tab, then visit /bench/index.html to verify that the benchmark also runs in a browser.

Sample result output:

----- Running creation benchmark...
Cassowary.js x 2,597 ops/sec ±1.56% (93 runs sampled)
kiwi x 26,243 ops/sec ±1.34% (91 runs sampled)
kiwi new API x 20,840 ops/sec ±7.19% (80 runs sampled)
Fastest is kiwi (± 10.11x faster)
----- Running solving benchmark...
Cassowary.js x 260,002 ops/sec ±2.62% (89 runs sampled)
kiwi x 595,455 ops/sec ±1.74% (89 runs sampled)
Fastest is kiwi (± 2.29x faster)

Tests

To run the tests in the browser, just visit this page.

To run the tests locally using nodejs, clone or download this repository and execute the following steps:

npm install
npm run build && npm run test

Start a static server, f.e. npx five-server . which opens a new browser tab, and visit /test/index.html to verify that tests also pass in a browser.

Contribute

If you like this project and want to support it, show some love and give it a star, try it and report any bugs, write new feature ideas, or even open a pull request!

License

© 2013 Nucleic Development Team © 2021 Joseph Orbegoso Pea (http://github.com/trusktr) © 2021 Lume

License

Status

Build Status

kiwi's People

Contributors

adamhaile avatar cacaodev avatar henrikh avatar ijzerenhein avatar joshuahhh avatar jwiggins avatar kretz avatar orentrutner avatar sccolbert avatar thesherwood avatar trusktr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kiwi's Issues

How to get an objective function? I want to maximize/minimize stuff.

I am trying to model a home battery solution, where I want to optimize profit from buying/selling energy.
The result should charge the battery on cheap hours, and sell it on expensive hours, given the constraints.

The battery has a minimum and maximum capacity (SOC: 0 ... 5 kWh)
The charging / discharging speed has a minimum and a maximum (load: -2 ... 2 kW)
The energy prices vary per hour, and are known 24 hours in advance. (price[1] .... price[24])

What I am able to do is model the SOC and LOAD constraints. And I can model the profit.hour[n] = load.hour[n] x price.hour[n].
The total profit (objective function) would be profit.hour[1] + profit.hour[2] + ..... + profit.hour[24]

But now the big issue: How do I optimize to get maximum profit? (or alternatively minimize for cost)? I would like to see for each hour what the optimum load should be.

[Feature Request] incremental update notification

Is there any easy way to see which variables have changed during a call to solver.updateVariables? The api doesn't seem to give me a convenient way to incrementally update anything depending on the kiwi solver's state (such as UI), without checking each variable individually.

2 possible approaches for the API that spring to mind are:

  1. Having the variable constructor take a callback
type VariableConstructor = (name?: string, callback: (variable: Kiwi.Variable, new_value: any) => void) => Kiwi.Variable;
  1. Returning a list of tuples of dirty variables and their new values from solver.updateVariables
type VariableValue = any;
type SolverUpdateVariables = (this: Kiwi.Solver) => [Kiwi.Variable, VariableValue][];

Thoughts?

[QUESTION] Timetable constrain solver

Cheers Community,

I would like to build a lesson planner and am looking for a constrain solver in NodeJS. Currently I have found OptaPlanner as a Java variant and your kiwi.js npm package.

Does anyone have experience with constrain solvers in NodeJs environment (kiwi.js) and maybe with timetables?

Current "hard" constrains are:

  • classes
  • rooms
  • subjects
  • Teachers
  • timeslots

The idea behind the constraint solver should be an automatic assignment of subjects/teachers/classes within given timeslots.

Thanks for your help!

Missing types

Hey, it looks like the new version doesn't ship the types.

Could not find a declaration file for module '@lume/kiwi'. '...../node_modules/@lume/kiwi/dist/kiwi.js' implicitly has an 'any' type.
Try `npm i --save-dev @types/lume__kiwi` if it exists or add a new declaration (.d.ts) file containing `declare module '@lume/kiwi';`
"main": "dist/kiwi.js",
"types": "dist/kiwi.d.ts",

image

Distance constraint?

As far as I can tell, this library doesn't support squares or square roots. As a result, I'm not sure if the library even supports adding constraints that are distance-based. For example, a point (x1, y1) and point (x2, y2) with a constant distance as a constraint. Am I missing something?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.