Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- typedef long double ld;
- struct Point
- {
- ld x, y;
- Point() {}
- Point(Point const &p): x(p.x), y(p.y) {}
- Point(ld x, ld y): x(x), y(y) {}
- Point operator=(Point const &p)
- {
- this->x = p.x;
- this->y = p.y;
- return *this;
- }
- Point operator-(Point const &p)
- {
- return Point(this->x - p.x, this->y - p.y);
- }
- Point operator+(Point const &p)
- {
- return Point(p.x + this->x, p.y + this->y);
- }
- Point operator*(ld mult)
- {
- return Point(this->x * mult, this->y * mult);
- }
- static Point middle(Point const &a, Point const &b)
- {
- return Point((a.x + b.x) / 2.L, (a.y + b.y) / 2.L);
- }
- };
- class Simplex
- {
- public:
- Point best, good, worst;
- ld (*f)(Point);
- Simplex(int seed, ld (*func)(Point))
- {
- srand(seed);
- best = Point(0.0, 0.0);
- good = Point(0.0, 1.0);
- worst = Point(1.0, 0.1);
- f = func;
- relax();
- }
- void proceed()
- {
- Point m = Point::middle(best, good);
- Point r = worst + (best - worst) + (good - worst);
- Point e = worst + ((best - worst) + (good - worst)) * (3.L / 2.L);
- if (f(r) < f(good))
- {
- if (f(r) < f(best) && f(e) < f(r))
- worst = e;
- else
- worst = r;
- }
- else
- {
- if (f(r) < f(worst))
- worst = r;
- Point c = Point::middle(worst, m);
- if (f(c) < f(worst))
- worst = c;
- else
- {
- worst = Point::middle(best, worst);
- good = m;
- }
- }
- }
- void relax()
- {
- if (f(worst) < f(good))
- std::swap(worst, good);
- if (f(good) < f(best))
- std::swap(good, best);
- if (f(worst) < f(good))
- std::swap(worst, good);
- }
- };
- struct Func
- {
- static ld a, b, c;
- static ld func(Point p)
- {
- return a * (p.y - p.x * p.x * p.x * p.x) * (p.y - p.x * p.x * p.x * p.x) + b * (1.L - p.x) * (1.L - p.x) + c * p.y;
- }
- };
- ld Func::a, Func::b, Func::c;
- ld sqsum(Point p)
- {
- return p.x * p.x + p.y * p.y;
- }
- std::ostream & operator<<(std::ostream &out, Simplex const &s)
- {
- out.precision(20);
- out << std::fixed;
- out << (s.best.x + s.good.x + s.worst.x) / 3.l << " " << (s.best.y + s.good.y + s.worst.y) / 3.l << "\n";\
- return out;
- }
- int main()
- {
- std::cin >> Func::a >> Func::b >> Func::c;
- Simplex g(218, Func::func);
- for (unsigned long i = 0; i != 100000; ++i)
- {
- g.proceed();
- g.relax();
- }
- std::cout << g;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement