本文整理汇总了C++中DataTable::addSample方法的典型用法代码示例。如果您正苦于以下问题:C++ DataTable::addSample方法的具体用法?C++ DataTable::addSample怎么用?C++ DataTable::addSample使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataTable
的用法示例。
在下文中一共展示了DataTable::addSample方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Exception
DataTable operator+(const DataTable &lhs, const DataTable &rhs)
{
if(lhs.getNumVariables() != rhs.getNumVariables()) {
throw Exception("operator+(DataTable, DataTable): trying to add two DataTable's of different dimensions!");
}
DataTable result;
for(auto it = lhs.cbegin(); it != lhs.cend(); it++) {
result.addSample(*it);
}
for(auto it = rhs.cbegin(); it != rhs.cend(); it++) {
result.addSample(*it);
}
return result;
}
示例2: runProblem
void Michalewicz::runProblem()
{
double pi = atan(1)*4;
std::vector<VariablePtr> vars = {
std::make_shared<Variable>(0, 0, pi),
std::make_shared<Variable>(0, 0, pi),
std::make_shared<Variable>(1)
};
// Set starting points
vars.at(0)->setValue(1.0);
vars.at(1)->setValue(1.0);
DataTable data;
double dx = 0.05;
for (double x1 = 0; x1 <= pi; x1+=dx)
{
for (double x2 = 0; x2 <= pi; x2+=dx)
{
std::vector<double> x = {x1, x2};
DenseVector xd(2); xd << x1, x2;
DenseVector yd = michalewiczFunction(xd);
data.addSample(x,yd(0));
}
}
// Test accuracy of B-spline
// DenseVector (*foo)(DenseVector);
// foo = &michalewiczFunction;
// BSpline* bs = new BSpline(*data, 3);
// bool testpassed = bs->testBspline(foo);
// if (testpassed)
// {
// cout << "B-spline is very accurate:)" << endl;
// }
// else
// {
// cout << "B-spline is NOT very accurate:(" << endl;
// }
BSpline bs(data, BSplineType::CUBIC);
auto constraint = std::make_shared<ConstraintBSpline>(vars, bs, false);
//SolverIpopt solver(constraint);
BB::BranchAndBound solver(constraint);
// Optimize
SolverResult result = solver.optimize();
cout << result << endl;
fopt_found = result.objectiveValue;
zopt_found = result.primalVariables;
cout << zopt_found << endl;
}
示例3: runProblem
void Michalewicz::runProblem()
{
double pi = atan(1)*4;
std::vector<VariablePtr> vars = {
std::make_shared<Variable>(0, 0, pi),
std::make_shared<Variable>(0, 0, pi),
std::make_shared<Variable>(1)
};
// Set starting points
vars.at(0)->setValue(1.0);
vars.at(1)->setValue(1.0);
DataTable data;
unsigned int nums = 10; // 60x60 yields is sufficient to model function around optimum
auto x1 = linspace(0, 4, nums);
auto x2 = linspace(0, 4, nums);
for (auto x1i : x1)
{
for (auto x2i : x2)
{
DenseVector xd(2); xd << x1i, x2i;
double yd = michalewiczFunction(xd);
data.addSample(xd, yd);
}
}
// Test accuracy of B-spline
// DenseVector (*foo)(DenseVector);
// foo = &michalewiczFunction;
// BSpline* bs = new BSpline(*data, 3);
// bool testpassed = bs->testBspline(foo);
// if (testpassed)
// {
// cout << "B-spline is very accurate:)" << endl;
// }
// else
// {
// cout << "B-spline is NOT very accurate:(" << endl;
// }
BSpline bs = BSpline::Builder(data).degree(3).build();
auto constraint = std::make_shared<ConstraintBSpline>(vars, bs, false);
//SolverIpopt solver(constraint);
BB::BranchAndBound solver(constraint);
// Optimize
SolverResult result = solver.optimize();
cout << result << endl;
fopt_found = result.objectiveValue;
zopt_found = result.primalVariables;
cout << zopt_found << endl;
}
示例4: runProblem
void BilinearRelaxationTest::runProblem()
{
std::vector<double> cost, lb, ub, z0;
cost.push_back(0);
cost.push_back(0);
cost.push_back(1);
lb.push_back(-1.);
lb.push_back(-1.);
lb.push_back(-INF);
ub.push_back(1);
ub.push_back(2.5);
ub.push_back(INF);
z0.push_back(1);
z0.push_back(-0.5);
z0.push_back(-0.5);
std::vector<VariablePtr> vars;
for (unsigned int i = 0; i < 3; i++)
{
auto var = std::make_shared<Variable>(cost.at(i), lb.at(i), ub.at(i));
var->setValue(z0.at(i));
vars.push_back(var);
}
// Constraints
ConstraintSetPtr constraints = std::make_shared<ConstraintSet>();
ConstraintPtr myBilinearConstraint = std::make_shared<ConstraintBilinear>(vars,1,0,0);
constraints->add(myBilinearConstraint);
DataTable data;
double dx = 0.5;
for (double x1 = lb.at(0); x1 <= ub.at(0); x1+=dx)
{
for (double x2 = lb.at(1); x2 <= ub.at(1); x2+=dx)
{
std::vector<double> x;
x.push_back(x1);
x.push_back(x2);
DenseVector xd; xd.setZero(2);
xd(0) = x1;
xd(1) = x2;
DenseVector yd = bilinearFunction(xd);
data.addSample(x,yd(0));
}
}
ConstraintSetPtr constraints2 = std::make_shared<ConstraintSet>();
BSpline bs = BSpline::Builder(data).degree(3).build();
ConstraintPtr cbspline = std::make_shared<ConstraintBSpline>(vars, bs, true);
constraints2->add(cbspline);
// Test accuracy of B-spline
// DenseVector (*foo)(DenseVector);
// foo = &bilinearFunction;
// BSpline* bs = new BSpline(*data, 3);
// bool testpassed = bs->testBspline(foo);
// if (testpassed)
// {
// cout << "B-spline is very accurate:)" << endl;
// }
// else
// {
// cout << "B-spline is NOT very accurate:(" << endl;
// }
BB::BranchAndBound solver(constraints);
//SolverIpopt solver(constraints);
SolverResult res = solver.optimize();
cout << res << endl;
zopt_found = res.primalVariables;
fopt_found = res.objectiveValue;
}