slightlyoff / cassowary.js

Cassowary/JS, better, faster, future-ready
Other
1.69k stars 107 forks source link

Simple Demo Fails #57

Open theladyjaye opened 10 years ago

theladyjaye commented 10 years ago

The deficit below remains at 0 after the balance constraint is applied. I must be doing something incorrectly. Is there something I am missing here?

var spending = new c.Variable({value: 3598});
var revenue = new c.Variable({value: 2328});
var deficit = new c.Variable({value: 0});

var balance = new c.Equation(
    c.minus(spending, revenue),
    deficit,
    c.Strength.required, 0);

console.log('Spending', spending);
console.log('Revenue', revenue);

console.log('Deficit Before', deficit);
solver.addConstraint(balance);
console.log('Deficit After', deficit);

Even copying this test: https://github.com/slightlyoff/cassowary.js/blob/master/tests/End-To-End-test.js#L20-L31

Will fail, running the unit tests do not fail, but using c.js from bin is where this is happening

I should clarify, from the unit test above:

var solver = new c.SimplexSolver();

var x = new c.Variable({ value: 167 });
var y = new c.Variable({ value: 2 });
var eq = new c.Equation(x, new c.Expression(y));

solver.addConstraint(eq);
console.log(x.value, y.value)

x.value will be 0 y.value will be 0

Maybe that is indeed the correct value and I just understand it wrong?

It looks like I need to: solver.addStay(x); and I will get x and y to be 167. I guess that makes sense in an equal situation. One of the 2 sides of the equality needs to take precedence, I suppose the addStay() does that?

If no stay is added why are the variables zero'd?

redcap3000 commented 9 years ago

I'm attempting to run this example without success.... within a meteor serverside method.... (works mostly the same except npm requires are slightly different.) I attempted to following along in this video

https://www.youtube.com/watch?v=72sWgwaAoyk


            c = Meteor.npmRequire("cassowary");
            clientSolver = new c.SimplexSolver();
            spending = new c.Variable({value : 3598});

            revenues = new c.Variable({value : 2328});
            deficit = new c.Variable({});

            balance = new c.Equation(
                c.minus(spending.value,revenues.value),
                deficit,
                c.Strength.required,0)
            ;

            clientSolver.addConstraint(balance);
            clientSolver.addEditVar(spending,c.Strength.high)

            console.log('spending ' + spending);
            console.log('revenues ' + revenues);
            console.log('deficit ' + deficit);

            clientSolver.beginEdit();

            clientSolver.suggestValue(spending,200);

            clientSolver.endEdit();
            // spending becomes '0' 
            console.log('spending ' + spending);
            console.log('revenues ' + revenues);
            console.log('deficit ' + deficit);

* console output *

I20150509-11:57:56.797(-8)? spending [v1:3598]
I20150509-11:57:56.800(-8)? revenues [v2:2328]
I20150509-11:57:56.801(-8)? deficit [v3:1270]
I20150509-11:57:56.801(-8)? spending [v1:0]
I20150509-11:57:56.801(-8)? revenues [v2:2328]
I20150509-11:57:56.801(-8)? deficit [v3:1270]

I feel like I'm missing a basic fundamental step to get this to work.. Apparently the 'spending' variable is never updated to the value entered into the 'suggest()' call.

Using resolve() in various appropriate places has no effect.