2024-04-27 22:42:42 -07:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
|
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
2025-05-27 07:24:15 -07:00
|
|
|
#include <sleipnir/optimization/problem.hpp>
|
2024-04-27 22:42:42 -07:00
|
|
|
|
|
|
|
|
TEST(SleipnirTest, Quartic) {
|
2025-05-27 07:24:15 -07:00
|
|
|
slp::Problem problem;
|
2024-04-27 22:42:42 -07:00
|
|
|
|
2025-05-27 07:24:15 -07:00
|
|
|
auto x = problem.decision_variable();
|
|
|
|
|
x.set_value(20.0);
|
2024-04-27 22:42:42 -07:00
|
|
|
|
2025-05-27 07:24:15 -07:00
|
|
|
problem.minimize(slp::pow(x, 4));
|
2024-04-27 22:42:42 -07:00
|
|
|
|
2025-05-27 07:24:15 -07:00
|
|
|
problem.subject_to(x >= 1);
|
2024-04-27 22:42:42 -07:00
|
|
|
|
2025-05-27 07:24:15 -07:00
|
|
|
EXPECT_EQ(problem.cost_function_type(), slp::ExpressionType::NONLINEAR);
|
|
|
|
|
EXPECT_EQ(problem.equality_constraint_type(), slp::ExpressionType::NONE);
|
|
|
|
|
EXPECT_EQ(problem.inequality_constraint_type(), slp::ExpressionType::LINEAR);
|
2024-04-27 22:42:42 -07:00
|
|
|
|
2025-05-27 07:24:15 -07:00
|
|
|
auto status = problem.solve({.diagnostics = true});
|
2024-04-27 22:42:42 -07:00
|
|
|
|
2025-05-27 07:24:15 -07:00
|
|
|
EXPECT_EQ(status, slp::ExitStatus::SUCCESS);
|
|
|
|
|
|
|
|
|
|
EXPECT_NEAR(x.value(), 1.0, 1e-6);
|
2024-04-27 22:42:42 -07:00
|
|
|
}
|