Advertisement
Guest User

Untitled

a guest
Nov 29th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. //
  2. // g++ g.cpp -ggdb -std=c++11 -lmpfr -lgmp
  3. //
  4. #include <iostream>
  5. #include <cmath>
  6. #include <map>
  7. #include "mpreal.h"
  8. #include <string>
  9. #include <cstdlib>
  10.  
  11. using namespace std;
  12. using namespace mpfr;
  13.  
  14. typedef mpreal scalar;
  15. map<int, scalar> v,g;
  16. const scalar one = 1;
  17.  
  18. class clause3 {
  19. clause3* next;
  20. clause3() {}
  21. public:
  22. int sx, sy, sz, nx, ny, nz;
  23. clause3(int n1, int n2, int n3, int s1, int s2, int s3, clause3* nxt = 0) :
  24. sx(s1), sy(s2), sz(s3), nx(n1), ny(n2), nz(n3), next(nxt) {}
  25. clause3(istream& is, clause3* prev = 0) { read(is, prev); }
  26. bool read(istream& is, clause3* prev = 0) {
  27. try {
  28. string str;
  29. getline(is, str);
  30. while (str[0] == 'c' || str[0] == 'p') { getline(is, str); }
  31.  
  32. sscanf(str.c_str(), "%d %d %d 0", &nx, &ny, &nz);
  33.  
  34. // if (!str.size()) return false;
  35. // v[nx = atoi(str.c_str())] = 0;
  36. if (!nx) return false;
  37. if (nx < 0) {
  38. nx = -nx;
  39. sx = 0;
  40. } else sx = 1;
  41. // getline(is, str,' ');
  42. // v[ny = atoi(str.c_str())] = 0;
  43. if (ny < 0) {
  44. ny = -ny;
  45. sy = 0;
  46. } else sy = 1;
  47. // getline(is, str,' ');
  48. // v[nz = atoi(str.c_str())] = 0;
  49. if (nz < 0) {
  50. nz = -nz;
  51. sz = 0;
  52. } else sz = 1;
  53. v[nx]=v[ny]=v[nz]=0;
  54. if (is.good()) {
  55. next = new clause3;
  56. // is.ignore(numeric_limits<std::streamsize>::max(), '\n');
  57. // getline(is, str);
  58. if (!next->read(is, this)) {
  59. delete next;
  60. next = 0;
  61. return true;
  62. }
  63. }
  64. else next = 0;
  65. return true;
  66. }
  67. catch (...) {
  68. return false;
  69. }
  70. }
  71. scalar operator()(bool print = false) const { // eval
  72. scalar x = v[nx], y = v[ny], z = v[nz];
  73. scalar r = one - (sx ? one - x : x) * (sy ? one - y : y) * (sz ? one - z : z);
  74. if (next) r *= (*next)(print);
  75. if (print) cout<<r<<' ';
  76. return r;
  77. }
  78. scalar operator()(int dv) const { // diff
  79. scalar r = 0;
  80. if (dv == nx)
  81. r = (sx?one:-one) * (sy ? one - v[ny] : v[ny]) * (sz ? one - v[nz] : v[nz]);
  82. else if (dv == ny)
  83. r = (sy?one:-one) * (sx ? one - v[nx] : v[nx]) * (sz ? one - v[nz] : v[nz]);
  84. else if (dv == nz)
  85. r = (sz?one:-one) * (sx ? one - v[nx] : v[nx]) * (sy ? one - v[ny] : v[ny]);
  86. if (!next) return r;
  87. if (r) return r * (*next)() + (*next)(dv) * (*this)();
  88. return (*next)(dv) * (*this)();
  89. }
  90. scalar operator()(int d1, int d2) const { // 2nd diff
  91. scalar r = 0;
  92. // if (d1 == nx && d2 == nx) return
  93. return r;
  94. }
  95. void print() const {
  96. cout<<'(';
  97. if (!sx)cout<<'!'; cout<<nx<<'|';
  98. if (!sy)cout<<'!'; cout<<ny<<'|';
  99. if (!sz)cout<<'!'; cout<<nz<<')';
  100. if (next) { cout<<" & "<<endl; next->print(); }
  101. else cout<<endl;
  102. }
  103. };
  104.  
  105. int main(int,char**){
  106. mpreal::set_default_prec(512);
  107. const clause3& c = *new clause3(cin);
  108. //#define DOESNT_CONVERGE
  109. //c.print();
  110. c(true);
  111. cout<<endl;
  112. scalar d;
  113. for (auto x : v) v[x.first] = .5;
  114. for (uint n = 0; n < 3; n++) {
  115.  
  116. #ifdef DOESNT_CONVERGE
  117. for (auto x : v)
  118. cout<<'d'<<x.first<<' '<<(g[x.first] = (c()-1)*c(x.first))<<'\t'; //
  119. for (auto x : v) v[x.first] -= g[x.first];
  120. #else
  121. for (auto x : v) if (rand()%3) {
  122. cout<<'d'<<x.first<<' '<<(d = (c()-1)*c(x.first))<<','; // to detect unsat we minimize (c-1)^2 so grad is prop to (c-1)dc
  123. v[x.first] -= d;
  124. cout<<v[x.first]<<'\t';
  125. }
  126. #endif
  127.  
  128. cout<<endl<<c();
  129. cout<<"======="<<endl;
  130. }
  131.  
  132. return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement