j7s-optimization/src/main.cpp

44 lines
1.3 KiB
C++

// Copyright 2022 James Pace
// All Rights Reserved.
//
// For a license to this software contact
// James Pace at jpace121@gmail.com.
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#include <iostream>
#include "j7s-optimization/CostFunction.hpp"
#include "j7s-optimization/SimplexSolver.hpp"
#include "j7s-optimization/common.hpp"
int main(int, char **)
{
const j7s::CostFunction cost(2.0, 3.0, 4.0);
const std::vector<double> init_simplex = {-10, 0, 10};
j7s::SimplexSolver solver(cost, init_simplex);
j7s::IterationState state = j7s::IterationState::OK;
for (int cnt = 0; cnt < 1000; cnt++)
{
state = solver.update();
if (state == j7s::IterationState::CONVERGED)
{
break;
}
}
if (state == j7s::IterationState::CONVERGED)
{
const auto best = solver.bestCoord();
std::cout << "Converged! Best Input: " << best.input << " Cost: " << best.cost << std::endl;
std::cout << "Actual Best: " << cost.actualBest()
<< " Cost: " << cost.eval(cost.actualBest()) << std::endl;
}
else
{
std::cout << "Did not converge." << std::endl;
}
return 0;
}