Files
FCES-native/examples/simple_optimization.cpp
AI-anonymous 9bbe253810 feat: scaffold FCES-native C++ project with libtorch integration
- CMakeLists.txt with libtorch, GoogleTest, GoogleBenchmark, OpenMP, pybind11
- Header files: config, controller, population, fitness, evolution, spectral, oscillation, telemetry, optimizer
- Source implementations: controller (full micro-MLP forward pass, mutation, crossover), fitness (Welford's algorithm), oscillation (DFT), spectral (SVD rank), optimizer (sign-SGD stub)
- Tests: controller, population, fitness, optimizer (Google Test)
- Benchmarks: evolve throughput, optimizer step (Google Benchmark)
- Examples: simple optimization, PyTorch/libtorch integration
- Python extension: pybind11 bindings with setup.py
- README with architecture diagram and build instructions
2026-05-19 16:05:15 +02:00

37 lines
1.0 KiB
C++

/**
* @file simple_optimization.cpp
* @brief Minimal example: optimize a quadratic function with FCES.
*/
#include <iostream>
#include <torch/torch.h>
#include "fces/optimizer.hpp"
int main() {
// Target: minimize f(x) = ||x - target||^2
auto target = torch::tensor({1.0f, 2.0f, 3.0f, 4.0f, 5.0f});
auto x = torch::randn({5}, torch::requires_grad());
std::vector<torch::Tensor> params = {x};
fces::FCESOptimizer optimizer(params, fces::FCESConfig{}.set_lr(1e-2f));
for (int step = 0; step < 500; ++step) {
optimizer.zero_grad();
auto loss = (x - target).pow(2).sum();
loss.backward();
optimizer.step();
optimizer.update_fitness(loss.item<float>());
if (step % 50 == 0) {
std::cout << "Step " << step
<< " | Loss: " << loss.item<float>()
<< " | x: " << x << std::endl;
}
}
std::cout << "\nFinal x: " << x << std::endl;
std::cout << "Target: " << target << std::endl;
return 0;
}