Skip to content

Instantly share code, notes, and snippets.

@jangko
Created October 23, 2015 14:40
Show Gist options
  • Save jangko/5f6ee9546c60760b6226 to your computer and use it in GitHub Desktop.
Save jangko/5f6ee9546c60760b6226 to your computer and use it in GitHub Desktop.
example from cassowary-js
#include "kiwi/kiwi.h"
#include <ostream>
int main() {
kiwi::Variable ale = kiwi::Variable("ale");
kiwi::Variable beer = kiwi::Variable("beer");
kiwi::Variable profit = kiwi::Variable("profit");
kiwi::Variable corn = kiwi::Variable("corn");
kiwi::Variable hops = kiwi::Variable("hops");
kiwi::Variable malt = kiwi::Variable("malt");
kiwi::Constraint cns[] = {
13.0 * ale + 23.0 * beer == profit,
(5 * ale) + (15 * beer) <= corn,
(4 * ale) + (4 * beer) <= hops,
(35 * ale) + (20 * beer) <= malt,
ale >= 0.0,
beer >= 0.0,
corn <= 480,
hops <= 160,
malt <= 1190,
malt >= 0,
corn >= 0,
hops >= 0,
};
kiwi::Solver solver;
int len = sizeof(cns) / sizeof(cns[0]);
for(int i = 0; i < len; i++) {
solver.addConstraint(cns[i]);
}
solver.addEditVariable(profit, kiwi::strength::strong);
solver.suggestValue(profit, 1000);
solver.updateVariables();
std::cout << "Profit: " << profit.value() << std::endl;
std::cout << "Ale: " << ale.value() << std::endl;
std::cout << "Beer: " << beer.value() << std::endl;
std::cout << "Corn: " << corn.value() << std::endl;
std::cout << "Hops: " << hops.value() << std::endl;
std::cout << "Malt: " << malt.value() << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment