Ejemplo n.º 1
0
TEST(Solver, AddConstraint)
{
    Constant c1("c1", 3.0);
    Variable v1("v1");
    
    Expression expression(-v1, c1);
    Constraint constraint(expression, Constraint::OP_EQ);
    
    Solver solver;
    solver.addConstraint(constraint);
    EXPECT_THROW(solver.addConstraint(constraint), DuplicateConstraint);
}
Ejemplo n.º 2
0
TEST(Solver, UpdateConstants)
{
    Constant c1("c1");
    Constant c2("c2");
    c1.setValue(6.28);
    c2.setValue(0.5);
    
    Variable v1("v1");
    
    Expression expression(-v1, SimpleTerm(c1, SimpleTerm::OP_MULTIPLY, c2));
    Constraint constraint(expression, Constraint::OP_EQ);
    
    Solver solver;
    
    solver.addConstraint(constraint);
    solver.updateVariables();
    EXPECT_EQ(v1.value(), 3.14);
    
    c1.setValue(50.0);
    solver.reevaluateConstants();
    solver.updateVariables();
    EXPECT_EQ(v1.value(), 25);
    
    c2.setValue(1.0);
    solver.reevaluateConstants();
    solver.updateVariables();
    EXPECT_EQ(v1.value(), 50.0);
}
Ejemplo n.º 3
0
TEST(Solver, Basics)
{
    Constant constant("x");
    constant.setValue(6.28);
    
    Variable leftSide("v");
    SimpleTerm rightSide(constant, SimpleTerm::OP_MULTIPLY, -0.5);
    Expression expression(leftSide, rightSide);
    Constraint constraint(expression, Constraint::OP_EQ);
    
    Solver solver;
    
    EXPECT_TRUE(!solver.hasConstraint(constraint));
    
    solver.addConstraint(constraint);
    
    EXPECT_TRUE(solver.hasConstraint(constraint));
    EXPECT_TRUE(solver.hasConstraint(constraint));
    
    solver.updateVariables();
    EXPECT_EQ(leftSide.value(), 3.14);
    
    solver.removeConstraint(constraint);
    EXPECT_TRUE(!solver.hasConstraint(constraint));
}
Ejemplo n.º 4
0
int main( int argc, const char* argv[] )
{
    // 0|-------------| x1 ----- xm ----- x2 |-----------------------|100

    /*
    x1 = kiwi.Variable('x1')
    x2 = kiwi.Variable('x2')
    xm = kiwi.Variable('xm')
    */

    Variable x1("x1");
    Variable x2("x2");
    Variable xm("xm");

    /*
    constraints = [
        x1 >= 0,
        x2 <= 100,
        x2 >= x1 + 10,
        xm == (x1 + x2) / 2,
    ]  # these all have strength 'required'
    */
    Constraint constraints[] = {
        Constraint {x1 >= 0},
        Constraint {x2 <= 100},
        Constraint {x2 >= x1 + 10},
        Constraint {xm == (x1 + x2) / 2}
    };

 // solver = kiwi.Solver()
    Solver solver;

    /*
    for cn in constraints:
        solver.addConstraint(cn)
    */
    for(auto& constraint : constraints)
    {
        solver.addConstraint(constraint);
    }

 // solver.addEditVariable(xm, 'strong')
    solver.addEditVariable(xm, strength::strong);

    /*
        for val in (-20, 0, 20, 50, 80, 100, 140):
            solver.suggestValue(xm, val)
            solver.updateVariables()
            print 'x1:', x1.value()
            print 'x2:', x2.value()
            print 'xm:', xm.value()
            print 'suggested xm:', val
            print

    */
    int values[] = {-20, 0, 20, 50, 80, 100, 140};
    for(auto value : values)
    {
        solver.suggestValue(xm, value);
        solver.updateVariables();
        std::cout << "x1:" << x1.value() << "\n";
        std::cout << "x2:" << x2.value() << "\n";
        std::cout << "xm:" << xm.value() << "\n";
        std::cout << "suggested xm:" << value << "\n";
        std::cout << "\n";
    }

    return 0;
}