本文整理汇总了C++中Solver::addConstraint方法的典型用法代码示例。如果您正苦于以下问题:C++ Solver::addConstraint方法的具体用法?C++ Solver::addConstraint怎么用?C++ Solver::addConstraint使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Solver
的用法示例。
在下文中一共展示了Solver::addConstraint方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: expression
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);
}
示例2: constant
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));
}
示例3: main
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;
}