Advertisement
iskhakovt

Untitled

Mar 3rd, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. typedef long double ld;
  5.  
  6. struct Point
  7. {
  8.     ld x, y;
  9.  
  10.     Point() {}
  11.     Point(Point const &p): x(p.x), y(p.y) {}
  12.     Point(ld x, ld y): x(x), y(y) {}
  13.  
  14.     Point operator=(Point const &p)
  15.     {
  16.         this->x = p.x;
  17.         this->y = p.y;
  18.         return *this;
  19.     }
  20.  
  21.     Point operator-(Point const &p)
  22.     {
  23.         return Point(this->x - p.x, this->y - p.y);
  24.     }
  25.  
  26.     Point operator+(Point const &p)
  27.     {
  28.         return Point(p.x + this->x, p.y + this->y);
  29.     }
  30.  
  31.     Point operator*(ld mult)
  32.     {
  33.         return Point(this->x * mult, this->y * mult);
  34.     }
  35.  
  36.     static Point middle(Point const &a, Point const &b)
  37.     {
  38.         return Point((a.x + b.x) / 2.L, (a.y + b.y) / 2.L);
  39.     }
  40. };
  41.  
  42. class Simplex
  43. {
  44. public:
  45.  
  46.     Point best, good, worst;
  47.  
  48.     ld (*f)(Point);
  49.  
  50.     Simplex(int seed, ld (*func)(Point))
  51.     {
  52.         srand(seed);
  53.  
  54.         best = Point(0.0, 0.0);
  55.         good = Point(0.0, 1.0);
  56.         worst = Point(1.0, 0.1);
  57.  
  58.         f = func;
  59.  
  60.         relax();
  61.     }
  62.  
  63.     void proceed()
  64.     {
  65.         Point m = Point::middle(best, good);
  66.         Point r = worst + (best - worst) + (good - worst);
  67.         Point e = worst + ((best - worst) + (good - worst)) * (3.L / 2.L);
  68.  
  69.         if (f(r) < f(good))
  70.         {
  71.             if (f(r) < f(best) && f(e) < f(r))
  72.                 worst = e;
  73.             else
  74.                 worst = r;
  75.         }
  76.         else
  77.         {
  78.             if (f(r) < f(worst))
  79.                 worst = r;
  80.  
  81.             Point c = Point::middle(worst, m);
  82.  
  83.             if (f(c) < f(worst))
  84.                 worst = c;
  85.             else
  86.             {
  87.                 worst = Point::middle(best, worst);
  88.                 good = m;
  89.             }
  90.         }
  91.     }
  92.  
  93.     void relax()
  94.     {
  95.         if (f(worst) < f(good))
  96.             std::swap(worst, good);
  97.         if (f(good) < f(best))
  98.             std::swap(good, best);
  99.         if (f(worst) < f(good))
  100.             std::swap(worst, good);
  101.     }
  102. };
  103.  
  104. struct Func
  105. {
  106.     static ld a, b, c;
  107.  
  108.     static ld func(Point p)
  109.     {
  110.         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;
  111.     }
  112. };
  113.  
  114. ld Func::a, Func::b, Func::c;
  115.  
  116. ld sqsum(Point p)
  117. {
  118.     return p.x * p.x + p.y * p.y;
  119. }
  120.  
  121. std::ostream & operator<<(std::ostream &out, Simplex const &s)
  122. {
  123.     out.precision(20);
  124.     out << std::fixed;
  125.  
  126.     out << (s.best.x + s.good.x + s.worst.x) / 3.l << " " << (s.best.y + s.good.y + s.worst.y) / 3.l << "\n";\
  127.  
  128.     return out;
  129. }
  130.  
  131. int main()
  132. {
  133.     std::cin >> Func::a >> Func::b >> Func::c;
  134.     Simplex g(218, Func::func);
  135.  
  136.     for (unsigned long i = 0; i != 100000; ++i)
  137.     {
  138.         g.proceed();
  139.         g.relax();
  140.     }
  141.  
  142.     std::cout << g;
  143.  
  144.     return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement