Solver.cc 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611
  1. /***************************************************************************************[Solver.cc]
  2. MiniSat -- Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
  3. Copyright (c) 2007-2010, Niklas Sorensson
  4. Chanseok Oh's MiniSat Patch Series -- Copyright (c) 2015, Chanseok Oh
  5. Maple_LCM, Based on MapleCOMSPS_DRUP -- Copyright (c) 2017, Mao Luo, Chu-Min LI, Fan Xiao: implementing a learnt clause minimisation approach
  6. Reference: M. Luo, C.-M. Li, F. Xiao, F. Manya, and Z. L. , “An effective learnt clause minimization approach for cdcl sat solvers,” in IJCAI-2017, 2017, pp. to–appear.
  7. Maple_LCM_Dist, Based on Maple_LCM -- Copyright (c) 2017, Fan Xiao, Chu-Min LI, Mao Luo: using a new branching heuristic called Distance at the beginning of search
  8. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  9. associated documentation files (the "Software"), to deal in the Software without restriction,
  10. including without limitation the rights to use, copy, modify, merge, publish, distribute,
  11. sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in all copies or
  14. substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  16. NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
  19. OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. **************************************************************************************************/
  21. #include <math.h>
  22. #include <signal.h>
  23. #include <unistd.h>
  24. #include "mtl/Sort.h"
  25. #include "core/Solver.h"
  26. using namespace Minisat;
  27. #ifdef BIN_DRUP
  28. int Solver::buf_len = 0;
  29. unsigned char Solver::drup_buf[2 * 1024 * 1024];
  30. unsigned char* Solver::buf_ptr = drup_buf;
  31. #endif
  32. //=================================================================================================
  33. // Options:
  34. static const char* _cat = "CORE";
  35. static DoubleOption opt_step_size (_cat, "step-size", "Initial step size", 0.40, DoubleRange(0, false, 1, false));
  36. static DoubleOption opt_step_size_dec (_cat, "step-size-dec","Step size decrement", 0.000001, DoubleRange(0, false, 1, false));
  37. static DoubleOption opt_min_step_size (_cat, "min-step-size","Minimal step size", 0.06, DoubleRange(0, false, 1, false));
  38. static DoubleOption opt_var_decay (_cat, "var-decay", "The variable activity decay factor", 0.80, DoubleRange(0, false, 1, false));
  39. static DoubleOption opt_clause_decay (_cat, "cla-decay", "The clause activity decay factor", 0.999, DoubleRange(0, false, 1, false));
  40. static DoubleOption opt_random_var_freq (_cat, "rnd-freq", "The frequency with which the decision heuristic tries to choose a random variable", 0, DoubleRange(0, true, 1, true));
  41. static DoubleOption opt_random_seed (_cat, "rnd-seed", "Used by the random variable selection", 91648253, DoubleRange(0, false, HUGE_VAL, false));
  42. static IntOption opt_ccmin_mode (_cat, "ccmin-mode", "Controls conflict clause minimization (0=none, 1=basic, 2=deep)", 2, IntRange(0, 2));
  43. static IntOption opt_phase_saving (_cat, "phase-saving", "Controls the level of phase saving (0=none, 1=limited, 2=full)", 2, IntRange(0, 2));
  44. static BoolOption opt_rnd_init_act (_cat, "rnd-init", "Randomize the initial activity", false);
  45. static IntOption opt_restart_first (_cat, "rfirst", "The base restart interval", 100, IntRange(1, INT32_MAX));
  46. static DoubleOption opt_restart_inc (_cat, "rinc", "Restart interval increase factor", 2, DoubleRange(1, false, HUGE_VAL, false));
  47. static DoubleOption opt_garbage_frac (_cat, "gc-frac", "The fraction of wasted memory allowed before a garbage collection is triggered", 0.20, DoubleRange(0, false, HUGE_VAL, false));
  48. //------------
  49. static DoubleOption opt_conflRatio (_cat, "conflRatio","True assgins above this,construct a model", 0.9, DoubleRange(0, true, 1, true));
  50. static DoubleOption opt_areaRatio (_cat, "areaRatio","Make sure two false model not too nearly in search space", 1.01, DoubleRange(1, true, 3, true));
  51. static DoubleOption opt_maxflipRatio(_cat,"maxFlipRatio","max steps for cnnr", 600, DoubleRange(1, true, 50000, true));
  52. static DoubleOption opt_lastflipRatio(_cat,"initFlipRatio","initial & now steps", 100, DoubleRange(1, true, 50000, true));
  53. static DoubleOption opt_timeRatio (_cat,"timeRatio","cnnrTime / totalTime", 0.35, DoubleRange(0, true, 1.5, true));
  54. static DoubleOption opt_raiseFlipRatio(_cat,"raiseFlipRatio","lastflipRatio*=raiseFlipRatio every time call cnnr", 1.02, DoubleRange(1, true, 5, true));
  55. static DoubleOption opt_switchMode(_cat,"switchMode","time to switch to VSIDS", 2500, DoubleRange(0, true, 6000, true));
  56. //==============
  57. //=================================================================================================
  58. // Constructor/Destructor:
  59. Solver::Solver() :
  60. // Parameters (user settable):
  61. //
  62. drup_file (NULL)
  63. , verbosity (0)
  64. , step_size (opt_step_size)
  65. , step_size_dec (opt_step_size_dec)
  66. , min_step_size (opt_min_step_size)
  67. , timer (5000)
  68. , var_decay (opt_var_decay)
  69. , clause_decay (opt_clause_decay)
  70. , random_var_freq (opt_random_var_freq)
  71. , random_seed (opt_random_seed)
  72. , VSIDS (false)
  73. , ccmin_mode (opt_ccmin_mode)
  74. , phase_saving (opt_phase_saving)
  75. , rnd_pol (false)
  76. , rnd_init_act (opt_rnd_init_act)
  77. , garbage_frac (opt_garbage_frac)
  78. , restart_first (opt_restart_first)
  79. , restart_inc (opt_restart_inc)
  80. // Parameters (the rest):
  81. //
  82. , learntsize_factor((double)1/(double)3), learntsize_inc(1.1)
  83. // Parameters (experimental):
  84. //
  85. , learntsize_adjust_start_confl (100)
  86. , learntsize_adjust_inc (1.5)
  87. // Statistics: (formerly in 'SolverStats')
  88. //
  89. , solves(0), starts(0), decisions(0), rnd_decisions(0), propagations(0), conflicts(0), conflicts_VSIDS(0)
  90. , dec_vars(0), clauses_literals(0), learnts_literals(0), max_literals(0), tot_literals(0)
  91. , ok (true)
  92. , cla_inc (1)
  93. , var_inc (1)
  94. , watches_bin (WatcherDeleted(ca))
  95. , watches (WatcherDeleted(ca))
  96. , qhead (0)
  97. , simpDB_assigns (-1)
  98. , simpDB_props (0)
  99. , order_heap_CHB (VarOrderLt(activity_CHB))
  100. , order_heap_VSIDS (VarOrderLt(activity_VSIDS))
  101. , progress_estimate (0)
  102. , remove_satisfied (true)
  103. , core_lbd_cut (3)
  104. , global_lbd_sum (0)
  105. , lbd_queue (50)
  106. , next_T2_reduce (10000)
  107. , next_L_reduce (15000)
  108. , counter (0)
  109. // Resource constraints:
  110. //
  111. , conflict_budget (-1)
  112. , propagation_budget (-1)
  113. , asynch_interrupt (false)
  114. // simplfiy
  115. , nbSimplifyAll(0)
  116. , s_propagations(0)
  117. // simplifyAll adjust occasion
  118. , curSimplify(1)
  119. , nbconfbeforesimplify(1000)
  120. , incSimplify(1000)
  121. , my_var_decay (0.6)
  122. , DISTANCE (true)
  123. , var_iLevel_inc (1)
  124. , order_heap_distance(VarOrderLt(activity_distance))
  125. //------------------------------
  126. , areaRatio (opt_areaRatio)
  127. , conflRatio (opt_conflRatio)
  128. , maxflipRatio (opt_maxflipRatio)
  129. , lastflipRatio (opt_lastflipRatio)
  130. , raiseFlipRatio (opt_raiseFlipRatio)
  131. , timeRatio (opt_timeRatio)
  132. , maxstep (20000000)
  133. , UnComTime (0.0)
  134. , callNum (0)
  135. , solvedByUncom (false)
  136. , forceRestart (false)
  137. , lastLearntNum (0)
  138. //=============================
  139. {
  140. //--------------------------------
  141. //adjust
  142. //ifstream fin("para");
  143. //fin>>conflRatio>>areaRatio>>maxstep>>maxflipRatio>>lastflipRatio>>raiseFlipRatio>>timeLimit>>timeRatio;
  144. //printf("%f %f %ld %f %f %f\n",conflRatio,areaRatio,maxstep,maxflipRatio,lastflipRatio,raiseFlipRatio);
  145. //fin.close();
  146. //===============================
  147. }
  148. Solver::~Solver()
  149. {
  150. }
  151. // simplify All
  152. //
  153. CRef Solver::simplePropagate()
  154. {
  155. CRef confl = CRef_Undef;
  156. int num_props = 0;
  157. watches.cleanAll();
  158. watches_bin.cleanAll();
  159. while (qhead < trail.size())
  160. {
  161. Lit p = trail[qhead++]; // 'p' is enqueued fact to propagate.
  162. vec<Watcher>& ws = watches[p];
  163. Watcher *i, *j, *end;
  164. num_props++;
  165. // First, Propagate binary clauses
  166. vec<Watcher>& wbin = watches_bin[p];
  167. for (int k = 0; k<wbin.size(); k++)
  168. {
  169. Lit imp = wbin[k].blocker;
  170. if (value(imp) == l_False)
  171. {
  172. return wbin[k].cref;
  173. }
  174. if (value(imp) == l_Undef)
  175. {
  176. simpleUncheckEnqueue(imp, wbin[k].cref);
  177. }
  178. }
  179. for (i = j = (Watcher*)ws, end = i + ws.size(); i != end;)
  180. {
  181. // Try to avoid inspecting the clause:
  182. Lit blocker = i->blocker;
  183. if (value(blocker) == l_True)
  184. {
  185. *j++ = *i++; continue;
  186. }
  187. // Make sure the false literal is data[1]:
  188. CRef cr = i->cref;
  189. Clause& c = ca[cr];
  190. Lit false_lit = ~p;
  191. if (c[0] == false_lit)
  192. c[0] = c[1], c[1] = false_lit;
  193. assert(c[1] == false_lit);
  194. // i++;
  195. // If 0th watch is true, then clause is already satisfied.
  196. // However, 0th watch is not the blocker, make it blocker using a new watcher w
  197. // why not simply do i->blocker=first in this case?
  198. Lit first = c[0];
  199. // Watcher w = Watcher(cr, first);
  200. if (first != blocker && value(first) == l_True)
  201. {
  202. i->blocker = first;
  203. *j++ = *i++; continue;
  204. }
  205. // Look for new watch:
  206. //if (incremental)
  207. //{ // ----------------- INCREMENTAL MODE
  208. // int choosenPos = -1;
  209. // for (int k = 2; k < c.size(); k++)
  210. // {
  211. // if (value(c[k]) != l_False)
  212. // {
  213. // if (decisionLevel()>assumptions.size())
  214. // {
  215. // choosenPos = k;
  216. // break;
  217. // }
  218. // else
  219. // {
  220. // choosenPos = k;
  221. // if (value(c[k]) == l_True || !isSelector(var(c[k]))) {
  222. // break;
  223. // }
  224. // }
  225. // }
  226. // }
  227. // if (choosenPos != -1)
  228. // {
  229. // // watcher i is abandonned using i++, because cr watches now ~c[k] instead of p
  230. // // the blocker is first in the watcher. However,
  231. // // the blocker in the corresponding watcher in ~first is not c[1]
  232. // Watcher w = Watcher(cr, first); i++;
  233. // c[1] = c[choosenPos]; c[choosenPos] = false_lit;
  234. // watches[~c[1]].push(w);
  235. // goto NextClause;
  236. // }
  237. //}
  238. else
  239. { // ----------------- DEFAULT MODE (NOT INCREMENTAL)
  240. for (int k = 2; k < c.size(); k++)
  241. {
  242. if (value(c[k]) != l_False)
  243. {
  244. // watcher i is abandonned using i++, because cr watches now ~c[k] instead of p
  245. // the blocker is first in the watcher. However,
  246. // the blocker in the corresponding watcher in ~first is not c[1]
  247. Watcher w = Watcher(cr, first); i++;
  248. c[1] = c[k]; c[k] = false_lit;
  249. watches[~c[1]].push(w);
  250. goto NextClause;
  251. }
  252. }
  253. }
  254. // Did not find watch -- clause is unit under assignment:
  255. i->blocker = first;
  256. *j++ = *i++;
  257. if (value(first) == l_False)
  258. {
  259. confl = cr;
  260. qhead = trail.size();
  261. // Copy the remaining watches:
  262. while (i < end)
  263. *j++ = *i++;
  264. }
  265. else
  266. {
  267. simpleUncheckEnqueue(first, cr);
  268. }
  269. NextClause:;
  270. }
  271. ws.shrink(i - j);
  272. }
  273. s_propagations += num_props;
  274. return confl;
  275. }
  276. void Solver::simpleUncheckEnqueue(Lit p, CRef from){
  277. assert(value(p) == l_Undef);
  278. assigns[var(p)] = lbool(!sign(p)); // this makes a lbool object whose value is sign(p)
  279. vardata[var(p)].reason = from;
  280. trail.push_(p);
  281. }
  282. void Solver::cancelUntilTrailRecord()
  283. {
  284. for (int c = trail.size() - 1; c >= trailRecord; c--)
  285. {
  286. Var x = var(trail[c]);
  287. assigns[x] = l_Undef;
  288. }
  289. qhead = trailRecord;
  290. trail.shrink(trail.size() - trailRecord);
  291. }
  292. void Solver::litsEnqueue(int cutP, Clause& c)
  293. {
  294. for (int i = cutP; i < c.size(); i++)
  295. {
  296. simpleUncheckEnqueue(~c[i]);
  297. }
  298. }
  299. bool Solver::removed(CRef cr) {
  300. return ca[cr].mark() == 1;
  301. }
  302. void Solver::simpleAnalyze(CRef confl, vec<Lit>& out_learnt, vec<CRef>& reason_clause, bool True_confl)
  303. {
  304. int pathC = 0;
  305. Lit p = lit_Undef;
  306. int index = trail.size() - 1;
  307. do{
  308. if (confl != CRef_Undef){
  309. reason_clause.push(confl);
  310. Clause& c = ca[confl];
  311. // Special case for binary clauses
  312. // The first one has to be SAT
  313. if (p != lit_Undef && c.size() == 2 && value(c[0]) == l_False) {
  314. assert(value(c[1]) == l_True);
  315. Lit tmp = c[0];
  316. c[0] = c[1], c[1] = tmp;
  317. }
  318. // if True_confl==true, then choose p begin with the 1th index of c;
  319. for (int j = (p == lit_Undef && True_confl == false) ? 0 : 1; j < c.size(); j++){
  320. Lit q = c[j];
  321. if (!seen[var(q)]){
  322. seen[var(q)] = 1;
  323. pathC++;
  324. }
  325. }
  326. }
  327. else if (confl == CRef_Undef){
  328. out_learnt.push(~p);
  329. }
  330. // if not break, while() will come to the index of trail blow 0, and fatal error occur;
  331. if (pathC == 0) break;
  332. // Select next clause to look at:
  333. while (!seen[var(trail[index--])]);
  334. // if the reason cr from the 0-level assigned var, we must break avoid move forth further;
  335. // but attention that maybe seen[x]=1 and never be clear. However makes no matter;
  336. if (trailRecord > index + 1) break;
  337. p = trail[index + 1];
  338. confl = reason(var(p));
  339. seen[var(p)] = 0;
  340. pathC--;
  341. } while (pathC >= 0);
  342. }
  343. void Solver::simplifyLearnt(Clause& c)
  344. {
  345. ////
  346. original_length_record += c.size();
  347. trailRecord = trail.size();// record the start pointer
  348. vec<Lit> falseLit;
  349. falseLit.clear();
  350. //sort(&c[0], c.size(), VarOrderLevelLt(vardata));
  351. bool True_confl = false;
  352. int beforeSize, afterSize;
  353. beforeSize = c.size();
  354. int i, j;
  355. CRef confl;
  356. for (i = 0, j = 0; i < c.size(); i++){
  357. if (value(c[i]) == l_Undef){
  358. //printf("///@@@ uncheckedEnqueue:index = %d. l_Undef\n", i);
  359. simpleUncheckEnqueue(~c[i]);
  360. c[j++] = c[i];
  361. confl = simplePropagate();
  362. if (confl != CRef_Undef){
  363. break;
  364. }
  365. }
  366. else{
  367. if (value(c[i]) == l_True){
  368. //printf("///@@@ uncheckedEnqueue:index = %d. l_True\n", i);
  369. c[j++] = c[i];
  370. True_confl = true;
  371. confl = reason(var(c[i]));
  372. break;
  373. }
  374. else{
  375. //printf("///@@@ uncheckedEnqueue:index = %d. l_False\n", i);
  376. falseLit.push(c[i]);
  377. }
  378. }
  379. }
  380. c.shrink(c.size() - j);
  381. afterSize = c.size();
  382. //printf("\nbefore : %d, after : %d ", beforeSize, afterSize);
  383. if (confl != CRef_Undef || True_confl == true){
  384. simp_learnt_clause.clear();
  385. simp_reason_clause.clear();
  386. if (True_confl == true){
  387. simp_learnt_clause.push(c.last());
  388. }
  389. simpleAnalyze(confl, simp_learnt_clause, simp_reason_clause, True_confl);
  390. if (simp_learnt_clause.size() < c.size()){
  391. for (i = 0; i < simp_learnt_clause.size(); i++){
  392. c[i] = simp_learnt_clause[i];
  393. }
  394. c.shrink(c.size() - i);
  395. }
  396. }
  397. cancelUntilTrailRecord();
  398. ////
  399. simplified_length_record += c.size();
  400. }
  401. bool Solver::simplifyLearnt_x(vec<CRef>& learnts_x)
  402. {
  403. int beforeSize, afterSize;
  404. int learnts_x_size_before = learnts_x.size();
  405. int ci, cj, li, lj;
  406. bool sat, false_lit;
  407. unsigned int nblevels;
  408. ////
  409. //printf("learnts_x size : %d\n", learnts_x.size());
  410. ////
  411. int nbSimplified = 0;
  412. int nbSimplifing = 0;
  413. for (ci = 0, cj = 0; ci < learnts_x.size(); ci++){
  414. CRef cr = learnts_x[ci];
  415. Clause& c = ca[cr];
  416. if (removed(cr)) continue;
  417. else if (c.simplified()){
  418. learnts_x[cj++] = learnts_x[ci];
  419. ////
  420. nbSimplified++;
  421. }
  422. else{
  423. ////
  424. nbSimplifing++;
  425. sat = false_lit = false;
  426. for (int i = 0; i < c.size(); i++){
  427. if (value(c[i]) == l_True){
  428. sat = true;
  429. break;
  430. }
  431. else if (value(c[i]) == l_False){
  432. false_lit = true;
  433. }
  434. }
  435. if (sat){
  436. removeClause(cr);
  437. }
  438. else{
  439. detachClause(cr, true);
  440. if (false_lit){
  441. for (li = lj = 0; li < c.size(); li++){
  442. if (value(c[li]) != l_False){
  443. c[lj++] = c[li];
  444. }
  445. }
  446. c.shrink(li - lj);
  447. }
  448. beforeSize = c.size();
  449. assert(c.size() > 1);
  450. // simplify a learnt clause c
  451. simplifyLearnt(c);
  452. assert(c.size() > 0);
  453. afterSize = c.size();
  454. //printf("beforeSize: %2d, afterSize: %2d\n", beforeSize, afterSize);
  455. if (c.size() == 1){
  456. // when unit clause occur, enqueue and propagate
  457. uncheckedEnqueue(c[0]);
  458. if (propagate() != CRef_Undef){
  459. ok = false;
  460. return false;
  461. }
  462. // delete the clause memory in logic
  463. c.mark(1);
  464. ca.free(cr);
  465. }
  466. else{
  467. attachClause(cr);
  468. learnts_x[cj++] = learnts_x[ci];
  469. nblevels = computeLBD(c);
  470. if (nblevels < c.lbd()){
  471. //printf("lbd-before: %d, lbd-after: %d\n", c.lbd(), nblevels);
  472. c.set_lbd(nblevels);
  473. }
  474. if (c.mark() != CORE){
  475. if (c.lbd() <= core_lbd_cut){
  476. //if (c.mark() == LOCAL) local_learnts_dirty = true;
  477. //else tier2_learnts_dirty = true;
  478. cj--;
  479. learnts_core.push(cr);
  480. c.mark(CORE);
  481. }
  482. else if (c.mark() == LOCAL && c.lbd() <= 6){
  483. //local_learnts_dirty = true;
  484. cj--;
  485. learnts_tier2.push(cr);
  486. c.mark(TIER2);
  487. }
  488. }
  489. c.setSimplified(true);
  490. }
  491. }
  492. }
  493. }
  494. learnts_x.shrink(ci - cj);
  495. // printf("c nbLearnts_x %d / %d, nbSimplified: %d, nbSimplifing: %d\n",
  496. // learnts_x_size_before, learnts_x.size(), nbSimplified, nbSimplifing);
  497. return true;
  498. }
  499. bool Solver::simplifyLearnt_core()
  500. {
  501. int beforeSize, afterSize;
  502. int learnts_core_size_before = learnts_core.size();
  503. int ci, cj, li, lj;
  504. bool sat, false_lit;
  505. unsigned int nblevels;
  506. ////
  507. //printf("learnts_x size : %d\n", learnts_x.size());
  508. ////
  509. int nbSimplified = 0;
  510. int nbSimplifing = 0;
  511. for (ci = 0, cj = 0; ci < learnts_core.size(); ci++){
  512. CRef cr = learnts_core[ci];
  513. Clause& c = ca[cr];
  514. if (removed(cr)) continue;
  515. else if (c.simplified()){
  516. learnts_core[cj++] = learnts_core[ci];
  517. ////
  518. nbSimplified++;
  519. }
  520. else{
  521. int saved_size=c.size();
  522. // if (drup_file){
  523. // add_oc.clear();
  524. // for (int i = 0; i < c.size(); i++) add_oc.push(c[i]); }
  525. ////
  526. nbSimplifing++;
  527. sat = false_lit = false;
  528. for (int i = 0; i < c.size(); i++){
  529. if (value(c[i]) == l_True){
  530. sat = true;
  531. break;
  532. }
  533. else if (value(c[i]) == l_False){
  534. false_lit = true;
  535. }
  536. }
  537. if (sat){
  538. removeClause(cr);
  539. }
  540. else{
  541. detachClause(cr, true);
  542. if (false_lit){
  543. for (li = lj = 0; li < c.size(); li++){
  544. if (value(c[li]) != l_False){
  545. c[lj++] = c[li];
  546. }
  547. }
  548. c.shrink(li - lj);
  549. }
  550. beforeSize = c.size();
  551. assert(c.size() > 1);
  552. // simplify a learnt clause c
  553. simplifyLearnt(c);
  554. assert(c.size() > 0);
  555. afterSize = c.size();
  556. if(saved_size !=c.size()){
  557. #ifdef BIN_DRUP
  558. binDRUP('a', c , drup_file);
  559. // binDRUP('d', add_oc, drup_file);
  560. #else
  561. for (int i = 0; i < c.size(); i++)
  562. fprintf(drup_file, "%i ", (var(c[i]) + 1) * (-2 * sign(c[i]) + 1));
  563. fprintf(drup_file, "0\n");
  564. // fprintf(drup_file, "d ");
  565. // for (int i = 0; i < add_oc.size(); i++)
  566. // fprintf(drup_file, "%i ", (var(add_oc[i]) + 1) * (-2 * sign(add_oc[i]) + 1));
  567. // fprintf(drup_file, "0\n");
  568. #endif
  569. }
  570. //printf("beforeSize: %2d, afterSize: %2d\n", beforeSize, afterSize);
  571. if (c.size() == 1){
  572. // when unit clause occur, enqueue and propagate
  573. uncheckedEnqueue(c[0]);
  574. if (propagate() != CRef_Undef){
  575. ok = false;
  576. return false;
  577. }
  578. // delete the clause memory in logic
  579. c.mark(1);
  580. ca.free(cr);
  581. //#ifdef BIN_DRUP
  582. // binDRUP('d', c, drup_file);
  583. //#else
  584. // fprintf(drup_file, "d ");
  585. // for (int i = 0; i < c.size(); i++)
  586. // fprintf(drup_file, "%i ", (var(c[i]) + 1) * (-2 * sign(c[i]) + 1));
  587. // fprintf(drup_file, "0\n");
  588. //#endif
  589. }
  590. else{
  591. attachClause(cr);
  592. learnts_core[cj++] = learnts_core[ci];
  593. nblevels = computeLBD(c);
  594. if (nblevels < c.lbd()){
  595. //printf("lbd-before: %d, lbd-after: %d\n", c.lbd(), nblevels);
  596. c.set_lbd(nblevels);
  597. }
  598. c.setSimplified(true);
  599. }
  600. }
  601. }
  602. }
  603. learnts_core.shrink(ci - cj);
  604. // printf("c nbLearnts_core %d / %d, nbSimplified: %d, nbSimplifing: %d\n",
  605. // learnts_core_size_before, learnts_core.size(), nbSimplified, nbSimplifing);
  606. return true;
  607. }
  608. bool Solver::simplifyLearnt_tier2()
  609. {
  610. int beforeSize, afterSize;
  611. int learnts_tier2_size_before = learnts_tier2.size();
  612. int ci, cj, li, lj;
  613. bool sat, false_lit;
  614. unsigned int nblevels;
  615. ////
  616. //printf("learnts_x size : %d\n", learnts_x.size());
  617. ////
  618. int nbSimplified = 0;
  619. int nbSimplifing = 0;
  620. for (ci = 0, cj = 0; ci < learnts_tier2.size(); ci++){
  621. CRef cr = learnts_tier2[ci];
  622. Clause& c = ca[cr];
  623. if (removed(cr)) continue;
  624. else if (c.simplified()){
  625. learnts_tier2[cj++] = learnts_tier2[ci];
  626. ////
  627. nbSimplified++;
  628. }
  629. else{
  630. int saved_size=c.size();
  631. // if (drup_file){
  632. // add_oc.clear();
  633. // for (int i = 0; i < c.size(); i++) add_oc.push(c[i]); }
  634. ////
  635. nbSimplifing++;
  636. sat = false_lit = false;
  637. for (int i = 0; i < c.size(); i++){
  638. if (value(c[i]) == l_True){
  639. sat = true;
  640. break;
  641. }
  642. else if (value(c[i]) == l_False){
  643. false_lit = true;
  644. }
  645. }
  646. if (sat){
  647. removeClause(cr);
  648. }
  649. else{
  650. detachClause(cr, true);
  651. if (false_lit){
  652. for (li = lj = 0; li < c.size(); li++){
  653. if (value(c[li]) != l_False){
  654. c[lj++] = c[li];
  655. }
  656. }
  657. c.shrink(li - lj);
  658. }
  659. beforeSize = c.size();
  660. assert(c.size() > 1);
  661. // simplify a learnt clause c
  662. simplifyLearnt(c);
  663. assert(c.size() > 0);
  664. afterSize = c.size();
  665. if(saved_size!=c.size()){
  666. #ifdef BIN_DRUP
  667. binDRUP('a', c , drup_file);
  668. // binDRUP('d', add_oc, drup_file);
  669. #else
  670. for (int i = 0; i < c.size(); i++)
  671. fprintf(drup_file, "%i ", (var(c[i]) + 1) * (-2 * sign(c[i]) + 1));
  672. fprintf(drup_file, "0\n");
  673. // fprintf(drup_file, "d ");
  674. // for (int i = 0; i < add_oc.size(); i++)
  675. // fprintf(drup_file, "%i ", (var(add_oc[i]) + 1) * (-2 * sign(add_oc[i]) + 1));
  676. // fprintf(drup_file, "0\n");
  677. #endif
  678. }
  679. //printf("beforeSize: %2d, afterSize: %2d\n", beforeSize, afterSize);
  680. if (c.size() == 1){
  681. // when unit clause occur, enqueue and propagate
  682. uncheckedEnqueue(c[0]);
  683. if (propagate() != CRef_Undef){
  684. ok = false;
  685. return false;
  686. }
  687. // delete the clause memory in logic
  688. c.mark(1);
  689. ca.free(cr);
  690. //#ifdef BIN_DRUP
  691. // binDRUP('d', c, drup_file);
  692. //#else
  693. // fprintf(drup_file, "d ");
  694. // for (int i = 0; i < c.size(); i++)
  695. // fprintf(drup_file, "%i ", (var(c[i]) + 1) * (-2 * sign(c[i]) + 1));
  696. // fprintf(drup_file, "0\n");
  697. //#endif
  698. }
  699. else{
  700. attachClause(cr);
  701. learnts_tier2[cj++] = learnts_tier2[ci];
  702. nblevels = computeLBD(c);
  703. if (nblevels < c.lbd()){
  704. //printf("lbd-before: %d, lbd-after: %d\n", c.lbd(), nblevels);
  705. c.set_lbd(nblevels);
  706. }
  707. if (c.lbd() <= core_lbd_cut){
  708. cj--;
  709. learnts_core.push(cr);
  710. c.mark(CORE);
  711. }
  712. c.setSimplified(true);
  713. }
  714. }
  715. }
  716. }
  717. learnts_tier2.shrink(ci - cj);
  718. // printf("c nbLearnts_tier2 %d / %d, nbSimplified: %d, nbSimplifing: %d\n",
  719. // learnts_tier2_size_before, learnts_tier2.size(), nbSimplified, nbSimplifing);
  720. return true;
  721. }
  722. bool Solver::simplifyAll()
  723. {
  724. ////
  725. simplified_length_record = original_length_record = 0;
  726. if (!ok || propagate() != CRef_Undef)
  727. return ok = false;
  728. //// cleanLearnts(also can delete these code), here just for analyzing
  729. //if (local_learnts_dirty) cleanLearnts(learnts_local, LOCAL);
  730. //if (tier2_learnts_dirty) cleanLearnts(learnts_tier2, TIER2);
  731. //local_learnts_dirty = tier2_learnts_dirty = false;
  732. if (!simplifyLearnt_core()) return ok = false;
  733. if (!simplifyLearnt_tier2()) return ok = false;
  734. //if (!simplifyLearnt_x(learnts_local)) return ok = false;
  735. checkGarbage();
  736. ////
  737. // printf("c size_reduce_ratio : %4.2f%%\n",
  738. // original_length_record == 0 ? 0 : (original_length_record - simplified_length_record) * 100 / (double)original_length_record);
  739. //-----------------------------
  740. if(lastLearntNum > nLearnts()){
  741. lastLearntNum = nLearnts();
  742. }
  743. //=============================
  744. return true;
  745. }
  746. //=================================================================================================
  747. // Minor methods:
  748. // Creates a new SAT variable in the solver. If 'decision' is cleared, variable will not be
  749. // used as a decision variable (NOTE! This has effects on the meaning of a SATISFIABLE result).
  750. //
  751. Var Solver::newVar(bool sign, bool dvar)
  752. {
  753. int v = nVars();
  754. watches_bin.init(mkLit(v, false));
  755. watches_bin.init(mkLit(v, true ));
  756. watches .init(mkLit(v, false));
  757. watches .init(mkLit(v, true ));
  758. assigns .push(l_Undef);
  759. vardata .push(mkVarData(CRef_Undef, 0));
  760. activity_CHB .push(0);
  761. activity_VSIDS.push(rnd_init_act ? drand(random_seed) * 0.00001 : 0);
  762. picked.push(0);
  763. conflicted.push(0);
  764. almost_conflicted.push(0);
  765. #ifdef ANTI_EXPLORATION
  766. canceled.push(0);
  767. #endif
  768. seen .push(0);
  769. seen2 .push(0);
  770. polarity .push(sign);
  771. decision .push();
  772. trail .capacity(v+1);
  773. setDecisionVar(v, dvar);
  774. activity_distance.push(0);
  775. var_iLevel.push(0);
  776. var_iLevel_tmp.push(0);
  777. pathCs.push(0);
  778. return v;
  779. }
  780. bool Solver::addClause_(vec<Lit>& ps)
  781. {
  782. assert(decisionLevel() == 0);
  783. if (!ok) return false;
  784. // Check if clause is satisfied and remove false/duplicate literals:
  785. sort(ps);
  786. Lit p; int i, j;
  787. if (drup_file){
  788. add_oc.clear();
  789. for (int i = 0; i < ps.size(); i++) add_oc.push(ps[i]); }
  790. for (i = j = 0, p = lit_Undef; i < ps.size(); i++)
  791. if (value(ps[i]) == l_True || ps[i] == ~p)
  792. return true;
  793. else if (value(ps[i]) != l_False && ps[i] != p)
  794. ps[j++] = p = ps[i];
  795. ps.shrink(i - j);
  796. if (drup_file && i != j){
  797. #ifdef BIN_DRUP
  798. binDRUP('a', ps, drup_file);
  799. binDRUP('d', add_oc, drup_file);
  800. #else
  801. for (int i = 0; i < ps.size(); i++)
  802. fprintf(drup_file, "%i ", (var(ps[i]) + 1) * (-2 * sign(ps[i]) + 1));
  803. fprintf(drup_file, "0\n");
  804. fprintf(drup_file, "d ");
  805. for (int i = 0; i < add_oc.size(); i++)
  806. fprintf(drup_file, "%i ", (var(add_oc[i]) + 1) * (-2 * sign(add_oc[i]) + 1));
  807. fprintf(drup_file, "0\n");
  808. #endif
  809. }
  810. if (ps.size() == 0)
  811. return ok = false;
  812. else if (ps.size() == 1){
  813. uncheckedEnqueue(ps[0]);
  814. return ok = (propagate() == CRef_Undef);
  815. }else{
  816. CRef cr = ca.alloc(ps, false);
  817. clauses.push(cr);
  818. attachClause(cr);
  819. }
  820. return true;
  821. }
  822. void Solver::attachClause(CRef cr) {
  823. const Clause& c = ca[cr];
  824. assert(c.size() > 1);
  825. OccLists<Lit, vec<Watcher>, WatcherDeleted>& ws = c.size() == 2 ? watches_bin : watches;
  826. ws[~c[0]].push(Watcher(cr, c[1]));
  827. ws[~c[1]].push(Watcher(cr, c[0]));
  828. if (c.learnt()) learnts_literals += c.size();
  829. else clauses_literals += c.size(); }
  830. void Solver::detachClause(CRef cr, bool strict) {
  831. const Clause& c = ca[cr];
  832. assert(c.size() > 1);
  833. OccLists<Lit, vec<Watcher>, WatcherDeleted>& ws = c.size() == 2 ? watches_bin : watches;
  834. if (strict){
  835. remove(ws[~c[0]], Watcher(cr, c[1]));
  836. remove(ws[~c[1]], Watcher(cr, c[0]));
  837. }else{
  838. // Lazy detaching: (NOTE! Must clean all watcher lists before garbage collecting this clause)
  839. ws.smudge(~c[0]);
  840. ws.smudge(~c[1]);
  841. }
  842. if (c.learnt()) learnts_literals -= c.size();
  843. else clauses_literals -= c.size(); }
  844. void Solver::removeClause(CRef cr) {
  845. Clause& c = ca[cr];
  846. if (drup_file){
  847. if (c.mark() != 1){
  848. #ifdef BIN_DRUP
  849. binDRUP('d', c, drup_file);
  850. #else
  851. fprintf(drup_file, "d ");
  852. for (int i = 0; i < c.size(); i++)
  853. fprintf(drup_file, "%i ", (var(c[i]) + 1) * (-2 * sign(c[i]) + 1));
  854. fprintf(drup_file, "0\n");
  855. #endif
  856. }else
  857. printf("c Bug. I don't expect this to happen.\n");
  858. }
  859. detachClause(cr);
  860. // Don't leave pointers to free'd memory!
  861. if (locked(c)){
  862. Lit implied = c.size() != 2 ? c[0] : (value(c[0]) == l_True ? c[0] : c[1]);
  863. vardata[var(implied)].reason = CRef_Undef; }
  864. c.mark(1);
  865. ca.free(cr);
  866. }
  867. bool Solver::satisfied(const Clause& c) const {
  868. for (int i = 0; i < c.size(); i++)
  869. if (value(c[i]) == l_True)
  870. return true;
  871. return false; }
  872. // Revert to the state at given level (keeping all assignment at 'level' but not beyond).
  873. //
  874. void Solver::cancelUntil(int level) {
  875. if (decisionLevel() > level){
  876. for (int c = trail.size()-1; c >= trail_lim[level]; c--){
  877. Var x = var(trail[c]);
  878. if (!VSIDS){
  879. uint32_t age = conflicts - picked[x];
  880. if (age > 0){
  881. double adjusted_reward = ((double) (conflicted[x] + almost_conflicted[x])) / ((double) age);
  882. double old_activity = activity_CHB[x];
  883. activity_CHB[x] = step_size * adjusted_reward + ((1 - step_size) * old_activity);
  884. if (order_heap_CHB.inHeap(x)){
  885. if (activity_CHB[x] > old_activity)
  886. order_heap_CHB.decrease(x);
  887. else
  888. order_heap_CHB.increase(x);
  889. }
  890. }
  891. #ifdef ANTI_EXPLORATION
  892. canceled[x] = conflicts;
  893. #endif
  894. }
  895. assigns [x] = l_Undef;
  896. if (phase_saving > 1 || (phase_saving == 1) && c > trail_lim.last())
  897. polarity[x] = sign(trail[c]);
  898. insertVarOrder(x); }
  899. qhead = trail_lim[level];
  900. trail.shrink(trail.size() - trail_lim[level]);
  901. trail_lim.shrink(trail_lim.size() - level);
  902. } }
  903. //=================================================================================================
  904. // Major methods:
  905. Lit Solver::pickBranchLit()
  906. {
  907. Var next = var_Undef;
  908. // Heap<VarOrderLt>& order_heap = VSIDS ? order_heap_VSIDS : order_heap_CHB;
  909. Heap<VarOrderLt>& order_heap = DISTANCE ? order_heap_distance : ((!VSIDS)? order_heap_CHB:order_heap_VSIDS);
  910. // Random decision:
  911. /*if (drand(random_seed) < random_var_freq && !order_heap.empty()){
  912. next = order_heap[irand(random_seed,order_heap.size())];
  913. if (value(next) == l_Undef && decision[next])
  914. rnd_decisions++; }*/
  915. // Activity based decision:
  916. while (next == var_Undef || value(next) != l_Undef || !decision[next])
  917. if (order_heap.empty())
  918. return lit_Undef;
  919. else{
  920. #ifdef ANTI_EXPLORATION
  921. if (!VSIDS){
  922. Var v = order_heap_CHB[0];
  923. uint32_t age = conflicts - canceled[v];
  924. while (age > 0){
  925. double decay = pow(0.95, age);
  926. activity_CHB[v] *= decay;
  927. if (order_heap_CHB.inHeap(v))
  928. order_heap_CHB.increase(v);
  929. canceled[v] = conflicts;
  930. v = order_heap_CHB[0];
  931. age = conflicts - canceled[v];
  932. }
  933. }
  934. #endif
  935. next = order_heap.removeMin();
  936. }
  937. return mkLit(next, polarity[next]);
  938. }
  939. /*_________________________________________________________________________________________________
  940. |
  941. | analyze : (confl : Clause*) (out_learnt : vec<Lit>&) (out_btlevel : int&) -> [void]
  942. |
  943. | Description:
  944. | Analyze conflict and produce a reason clause.
  945. |
  946. | Pre-conditions:
  947. | * 'out_learnt' is assumed to be cleared.
  948. | * Current decision level must be greater than root level.
  949. |
  950. | Post-conditions:
  951. | * 'out_learnt[0]' is the asserting literal at level 'out_btlevel'.
  952. | * If out_learnt.size() > 1 then 'out_learnt[1]' has the greatest decision level of the
  953. | rest of literals. There may be others from the same level though.
  954. |
  955. |________________________________________________________________________________________________@*/
  956. void Solver::analyze(CRef confl, vec<Lit>& out_learnt, int& out_btlevel, int& out_lbd)
  957. {
  958. int pathC = 0;
  959. Lit p = lit_Undef;
  960. // Generate conflict clause:
  961. //
  962. out_learnt.push(); // (leave room for the asserting literal)
  963. int index = trail.size() - 1;
  964. do{
  965. assert(confl != CRef_Undef); // (otherwise should be UIP)
  966. Clause& c = ca[confl];
  967. // For binary clauses, we don't rearrange literals in propagate(), so check and make sure the first is an implied lit.
  968. if (p != lit_Undef && c.size() == 2 && value(c[0]) == l_False){
  969. assert(value(c[1]) == l_True);
  970. Lit tmp = c[0];
  971. c[0] = c[1], c[1] = tmp; }
  972. // Update LBD if improved.
  973. if (c.learnt() && c.mark() != CORE){
  974. int lbd = computeLBD(c);
  975. if (lbd < c.lbd()){
  976. if (c.lbd() <= 30) c.removable(false); // Protect once from reduction.
  977. c.set_lbd(lbd);
  978. if (lbd <= core_lbd_cut){
  979. learnts_core.push(confl);
  980. c.mark(CORE);
  981. }else if (lbd <= 6 && c.mark() == LOCAL){
  982. // Bug: 'cr' may already be in 'learnts_tier2', e.g., if 'cr' was demoted from TIER2
  983. // to LOCAL previously and if that 'cr' is not cleaned from 'learnts_tier2' yet.
  984. learnts_tier2.push(confl);
  985. c.mark(TIER2); }
  986. }
  987. if (c.mark() == TIER2)
  988. c.touched() = conflicts;
  989. else if (c.mark() == LOCAL)
  990. claBumpActivity(c);
  991. }
  992. for (int j = (p == lit_Undef) ? 0 : 1; j < c.size(); j++){
  993. Lit q = c[j];
  994. if (!seen[var(q)] && level(var(q)) > 0){
  995. if (VSIDS){
  996. varBumpActivity(var(q), .5);
  997. add_tmp.push(q);
  998. }else
  999. conflicted[var(q)]++;
  1000. seen[var(q)] = 1;
  1001. if (level(var(q)) >= decisionLevel()){
  1002. pathC++;
  1003. }else
  1004. out_learnt.push(q);
  1005. }
  1006. }
  1007. // Select next clause to look at:
  1008. while (!seen[var(trail[index--])]);
  1009. p = trail[index+1];
  1010. confl = reason(var(p));
  1011. seen[var(p)] = 0;
  1012. pathC--;
  1013. }while (pathC > 0);
  1014. out_learnt[0] = ~p;
  1015. // Simplify conflict clause:
  1016. //
  1017. int i, j;
  1018. out_learnt.copyTo(analyze_toclear);
  1019. if (ccmin_mode == 2){
  1020. uint32_t abstract_level = 0;
  1021. for (i = 1; i < out_learnt.size(); i++)
  1022. abstract_level |= abstractLevel(var(out_learnt[i])); // (maintain an abstraction of levels involved in conflict)
  1023. for (i = j = 1; i < out_learnt.size(); i++)
  1024. if (reason(var(out_learnt[i])) == CRef_Undef || !litRedundant(out_learnt[i], abstract_level))
  1025. out_learnt[j++] = out_learnt[i];
  1026. }else if (ccmin_mode == 1){
  1027. for (i = j = 1; i < out_learnt.size(); i++){
  1028. Var x = var(out_learnt[i]);
  1029. if (reason(x) == CRef_Undef)
  1030. out_learnt[j++] = out_learnt[i];
  1031. else{
  1032. Clause& c = ca[reason(var(out_learnt[i]))];
  1033. for (int k = c.size() == 2 ? 0 : 1; k < c.size(); k++)
  1034. if (!seen[var(c[k])] && level(var(c[k])) > 0){
  1035. out_learnt[j++] = out_learnt[i];
  1036. break; }
  1037. }
  1038. }
  1039. }else
  1040. i = j = out_learnt.size();
  1041. max_literals += out_learnt.size();
  1042. out_learnt.shrink(i - j);
  1043. tot_literals += out_learnt.size();
  1044. out_lbd = computeLBD(out_learnt);
  1045. if (out_lbd <= 6 && out_learnt.size() <= 30) // Try further minimization?
  1046. if (binResMinimize(out_learnt))
  1047. out_lbd = computeLBD(out_learnt); // Recompute LBD if minimized.
  1048. // Find correct backtrack level:
  1049. //
  1050. if (out_learnt.size() == 1)
  1051. out_btlevel = 0;
  1052. else{
  1053. int max_i = 1;
  1054. // Find the first literal assigned at the next-highest level:
  1055. for (int i = 2; i < out_learnt.size(); i++)
  1056. if (level(var(out_learnt[i])) > level(var(out_learnt[max_i])))
  1057. max_i = i;
  1058. // Swap-in this literal at index 1:
  1059. Lit p = out_learnt[max_i];
  1060. out_learnt[max_i] = out_learnt[1];
  1061. out_learnt[1] = p;
  1062. out_btlevel = level(var(p));
  1063. }
  1064. if (VSIDS){
  1065. for (int i = 0; i < add_tmp.size(); i++){
  1066. Var v = var(add_tmp[i]);
  1067. if (level(v) >= out_btlevel - 1)
  1068. varBumpActivity(v, 1);
  1069. }
  1070. add_tmp.clear();
  1071. }else{
  1072. seen[var(p)] = true;
  1073. for(int i = out_learnt.size() - 1; i >= 0; i--){
  1074. Var v = var(out_learnt[i]);
  1075. CRef rea = reason(v);
  1076. if (rea != CRef_Undef){
  1077. const Clause& reaC = ca[rea];
  1078. for (int i = 0; i < reaC.size(); i++){
  1079. Lit l = reaC[i];
  1080. if (!seen[var(l)]){
  1081. seen[var(l)] = true;
  1082. almost_conflicted[var(l)]++;
  1083. analyze_toclear.push(l); } } } } }
  1084. for (int j = 0; j < analyze_toclear.size(); j++) seen[var(analyze_toclear[j])] = 0; // ('seen[]' is now cleared)
  1085. }
  1086. // Try further learnt clause minimization by means of binary clause resolution.
  1087. bool Solver::binResMinimize(vec<Lit>& out_learnt)
  1088. {
  1089. // Preparation: remember which false variables we have in 'out_learnt'.
  1090. counter++;
  1091. for (int i = 1; i < out_learnt.size(); i++)
  1092. seen2[var(out_learnt[i])] = counter;
  1093. // Get the list of binary clauses containing 'out_learnt[0]'.
  1094. const vec<Watcher>& ws = watches_bin[~out_learnt[0]];
  1095. int to_remove = 0;
  1096. for (int i = 0; i < ws.size(); i++){
  1097. Lit the_other = ws[i].blocker;
  1098. // Does 'the_other' appear negatively in 'out_learnt'?
  1099. if (seen2[var(the_other)] == counter && value(the_other) == l_True){
  1100. to_remove++;
  1101. seen2[var(the_other)] = counter - 1; // Remember to remove this variable.
  1102. }
  1103. }
  1104. // Shrink.
  1105. if (to_remove > 0){
  1106. int last = out_learnt.size() - 1;
  1107. for (int i = 1; i < out_learnt.size() - to_remove; i++)
  1108. if (seen2[var(out_learnt[i])] != counter)
  1109. out_learnt[i--] = out_learnt[last--];
  1110. out_learnt.shrink(to_remove);
  1111. }
  1112. return to_remove != 0;
  1113. }
  1114. // Check if 'p' can be removed. 'abstract_levels' is used to abort early if the algorithm is
  1115. // visiting literals at levels that cannot be removed later.
  1116. bool Solver::litRedundant(Lit p, uint32_t abstract_levels)
  1117. {
  1118. analyze_stack.clear(); analyze_stack.push(p);
  1119. int top = analyze_toclear.size();
  1120. while (analyze_stack.size() > 0){
  1121. assert(reason(var(analyze_stack.last())) != CRef_Undef);
  1122. Clause& c = ca[reason(var(analyze_stack.last()))]; analyze_stack.pop();
  1123. // Special handling for binary clauses like in 'analyze()'.
  1124. if (c.size() == 2 && value(c[0]) == l_False){
  1125. assert(value(c[1]) == l_True);
  1126. Lit tmp = c[0];
  1127. c[0] = c[1], c[1] = tmp; }
  1128. for (int i = 1; i < c.size(); i++){
  1129. Lit p = c[i];
  1130. if (!seen[var(p)] && level(var(p)) > 0){
  1131. if (reason(var(p)) != CRef_Undef && (abstractLevel(var(p)) & abstract_levels) != 0){
  1132. seen[var(p)] = 1;
  1133. analyze_stack.push(p);
  1134. analyze_toclear.push(p);
  1135. }else{
  1136. for (int j = top; j < analyze_toclear.size(); j++)
  1137. seen[var(analyze_toclear[j])] = 0;
  1138. analyze_toclear.shrink(analyze_toclear.size() - top);
  1139. return false;
  1140. }
  1141. }
  1142. }
  1143. }
  1144. return true;
  1145. }
  1146. /*_________________________________________________________________________________________________
  1147. |
  1148. | analyzeFinal : (p : Lit) -> [void]
  1149. |
  1150. | Description:
  1151. | Specialized analysis procedure to express the final conflict in terms of assumptions.
  1152. | Calculates the (possibly empty) set of assumptions that led to the assignment of 'p', and
  1153. | stores the result in 'out_conflict'.
  1154. |________________________________________________________________________________________________@*/
  1155. void Solver::analyzeFinal(Lit p, vec<Lit>& out_conflict)
  1156. {
  1157. out_conflict.clear();
  1158. out_conflict.push(p);
  1159. if (decisionLevel() == 0)
  1160. return;
  1161. seen[var(p)] = 1;
  1162. for (int i = trail.size()-1; i >= trail_lim[0]; i--){
  1163. Var x = var(trail[i]);
  1164. if (seen[x]){
  1165. if (reason(x) == CRef_Undef){
  1166. assert(level(x) > 0);
  1167. out_conflict.push(~trail[i]);
  1168. }else{
  1169. Clause& c = ca[reason(x)];
  1170. for (int j = c.size() == 2 ? 0 : 1; j < c.size(); j++)
  1171. if (level(var(c[j])) > 0)
  1172. seen[var(c[j])] = 1;
  1173. }
  1174. seen[x] = 0;
  1175. }
  1176. }
  1177. seen[var(p)] = 0;
  1178. }
  1179. void Solver::uncheckedEnqueue(Lit p, CRef from)
  1180. {
  1181. assert(value(p) == l_Undef);
  1182. Var x = var(p);
  1183. if (!VSIDS){
  1184. picked[x] = conflicts;
  1185. conflicted[x] = 0;
  1186. almost_conflicted[x] = 0;
  1187. #ifdef ANTI_EXPLORATION
  1188. uint32_t age = conflicts - canceled[var(p)];
  1189. if (age > 0){
  1190. double decay = pow(0.95, age);
  1191. activity_CHB[var(p)] *= decay;
  1192. if (order_heap_CHB.inHeap(var(p)))
  1193. order_heap_CHB.increase(var(p));
  1194. }
  1195. #endif
  1196. }
  1197. assigns[x] = lbool(!sign(p));
  1198. vardata[x] = mkVarData(from, decisionLevel());
  1199. trail.push_(p);
  1200. }
  1201. /*_________________________________________________________________________________________________
  1202. |
  1203. | propagate : [void] -> [Clause*]
  1204. |
  1205. | Description:
  1206. | Propagates all enqueued facts. If a conflict arises, the conflicting clause is returned,
  1207. | otherwise CRef_Undef.
  1208. |
  1209. | Post-conditions:
  1210. | * the propagation queue is empty, even if there was a conflict.
  1211. |________________________________________________________________________________________________@*/
  1212. CRef Solver::propagate()
  1213. {
  1214. CRef confl = CRef_Undef;
  1215. int num_props = 0;
  1216. watches.cleanAll();
  1217. watches_bin.cleanAll();
  1218. while (qhead < trail.size()){
  1219. Lit p = trail[qhead++]; // 'p' is enqueued fact to propagate.
  1220. vec<Watcher>& ws = watches[p];
  1221. Watcher *i, *j, *end;
  1222. num_props++;
  1223. vec<Watcher>& ws_bin = watches_bin[p]; // Propagate binary clauses first.
  1224. for (int k = 0; k < ws_bin.size(); k++){
  1225. Lit the_other = ws_bin[k].blocker;
  1226. if (value(the_other) == l_False){
  1227. confl = ws_bin[k].cref;
  1228. #ifdef LOOSE_PROP_STAT
  1229. return confl;
  1230. #else
  1231. goto ExitProp;
  1232. #endif
  1233. }else if(value(the_other) == l_Undef)
  1234. uncheckedEnqueue(the_other, ws_bin[k].cref);
  1235. }
  1236. for (i = j = (Watcher*)ws, end = i + ws.size(); i != end;){
  1237. // Try to avoid inspecting the clause:
  1238. Lit blocker = i->blocker;
  1239. if (value(blocker) == l_True){
  1240. *j++ = *i++; continue; }
  1241. // Make sure the false literal is data[1]:
  1242. CRef cr = i->cref;
  1243. Clause& c = ca[cr];
  1244. Lit false_lit = ~p;
  1245. if (c[0] == false_lit)
  1246. c[0] = c[1], c[1] = false_lit;
  1247. assert(c[1] == false_lit);
  1248. i++;
  1249. // If 0th watch is true, then clause is already satisfied.
  1250. Lit first = c[0];
  1251. Watcher w = Watcher(cr, first);
  1252. if (first != blocker && value(first) == l_True){
  1253. *j++ = w; continue; }
  1254. // Look for new watch:
  1255. for (int k = 2; k < c.size(); k++)
  1256. if (value(c[k]) != l_False){
  1257. c[1] = c[k]; c[k] = false_lit;
  1258. watches[~c[1]].push(w);
  1259. goto NextClause; }
  1260. // Did not find watch -- clause is unit under assignment:
  1261. *j++ = w;
  1262. if (value(first) == l_False){
  1263. confl = cr;
  1264. qhead = trail.size();
  1265. // Copy the remaining watches:
  1266. while (i < end)
  1267. *j++ = *i++;
  1268. }else
  1269. uncheckedEnqueue(first, cr);
  1270. NextClause:;
  1271. }
  1272. ws.shrink(i - j);
  1273. }
  1274. ExitProp:;
  1275. propagations += num_props;
  1276. simpDB_props -= num_props;
  1277. return confl;
  1278. }
  1279. //-----------------------------
  1280. CRef Solver::propagateConfl()
  1281. {
  1282. CRef confl = CRef_Undef;
  1283. int num_props = 0;
  1284. watches.cleanAll();
  1285. watches_bin.cleanAll();
  1286. while (qhead < trail.size()){
  1287. Lit p = trail[qhead++]; // 'p' is enqueued fact to propagate.
  1288. vec<Watcher>& ws = watches[p];
  1289. Watcher *i, *j, *end;
  1290. num_props++;
  1291. //biary
  1292. vec<Watcher>& ws_bin = watches_bin[p]; // Propagate binary clauses first.
  1293. for (int k = 0; k < ws_bin.size(); k++){
  1294. Lit the_other = ws_bin[k].blocker;
  1295. if (value(the_other) == l_False){
  1296. confl = ws_bin[k].cref;
  1297. #ifdef LOOSE_PROP_STAT
  1298. return confl;
  1299. #else
  1300. //goto conflExitProp;
  1301. #endif
  1302. }else if(value(the_other) == l_Undef)
  1303. uncheckedEnqueue(the_other, ws_bin[k].cref);
  1304. }
  1305. for (i = j = (Watcher*)ws, end = i + ws.size(); i != end;){
  1306. // Try to avoid inspecting the clause:
  1307. Lit blocker = i->blocker;
  1308. if (value(blocker) == l_True){
  1309. *j++ = *i++; continue; }
  1310. // Make sure the false literal is data[1]:
  1311. CRef cr = i->cref;
  1312. Clause& c = ca[cr];
  1313. Lit false_lit = ~p;
  1314. if (c[0] == false_lit)
  1315. c[0] = c[1], c[1] = false_lit;
  1316. assert(c[1] == false_lit);
  1317. i++;
  1318. // If 0th watch is true, then clause is already satisfied.
  1319. Lit first = c[0];
  1320. Watcher w = Watcher(cr, first);
  1321. if (first != blocker && value(first) == l_True){
  1322. *j++ = w; continue; }
  1323. // Look for new watch:
  1324. for (int k = 2; k < c.size(); k++)
  1325. if (value(c[k]) != l_False){
  1326. c[1] = c[k]; c[k] = false_lit;
  1327. watches[~c[1]].push(w);
  1328. goto conflNextClause; }
  1329. // Did not find watch -- clause is unit under assignment:
  1330. *j++ = w;
  1331. if (value(first) == l_False){
  1332. confl = cr;
  1333. //qhead = trail.size();
  1334. // Copy the remaining watches:
  1335. while (i < end)
  1336. *j++ = *i++;
  1337. }else
  1338. uncheckedEnqueue(first, cr);
  1339. conflNextClause:;
  1340. }
  1341. ws.shrink(i - j);
  1342. }
  1343. conflExitProp:;
  1344. propagations += num_props;
  1345. simpDB_props -= num_props;
  1346. return confl;
  1347. return confl;
  1348. }
  1349. //------------------------------------------------
  1350. //cause no effect to origin data struct and give all undef variable a value
  1351. bool Solver::propagateConflSim(){ //cout<<"okin\n";
  1352. set<Var> undefVars;
  1353. falseModel.resize(nVars());
  1354. for(int i=0;i<nVars();i++){
  1355. if(value(i)==l_Undef){//cout<<"p1\n";
  1356. undefVars.insert((Var)i);//cout<<"p1e\n";
  1357. }else{// cout<<"p2\n";
  1358. falseModel[i] = (value(i) == l_True) ? true : false;//cout<<"p2e\n";
  1359. }
  1360. }
  1361. //cout<<"ok1\n";
  1362. vector<Lit> viewList;
  1363. for(int i=0;i<trail.size();i++) viewList.push_back(trail[i]);
  1364. int idx = qhead;
  1365. // cout<<"ok2\n";
  1366. while(!undefVars.empty()){
  1367. while(idx < viewList.size() && !undefVars.empty()){
  1368. Lit p = viewList[idx++];
  1369. vec<Watcher>& ws = watches[p];
  1370. Watcher *i, *j, *end;
  1371. vec<Watcher>& ws_bin = watches_bin[p];
  1372. for(int k=0;k<ws_bin.size();k++){
  1373. Lit the_other = ws_bin[k].blocker;
  1374. if(undefVars.find(var(the_other)) != undefVars.end()){
  1375. Var x =var(the_other);
  1376. falseModel[x] = (lbool(!sign(the_other))==l_True)?true:false;
  1377. viewList.push_back(the_other);
  1378. undefVars.erase(x);
  1379. }
  1380. }
  1381. if(undefVars.empty()) break;
  1382. for(i = (Watcher*)ws,end = i+ws.size();i!=end;i++){
  1383. CRef cr = i->cref;
  1384. Clause &c = ca[cr];
  1385. Lit udefLit = c[0];
  1386. int udefLoc = 0;
  1387. int udefNum = 0;
  1388. for(int m=0;m<c.size();m++){
  1389. if(undefVars.find(var(c[m]))!= undefVars.end()){
  1390. if(udefNum > 0) break;
  1391. udefLit = c[m];
  1392. udefLoc = m;
  1393. udefNum ++;
  1394. }
  1395. }
  1396. if(udefNum == 1){
  1397. Var x =var(udefLit);
  1398. falseModel[x] = (lbool(!sign(udefLit))==l_True)?true:false;
  1399. viewList.push_back(udefLit);
  1400. undefVars.erase(x);
  1401. }
  1402. }
  1403. }
  1404. if(!undefVars.empty()){
  1405. Lit choose = mkLit(*(undefVars.begin()),false);
  1406. Var x =var(choose);
  1407. falseModel[x] = (lbool(!sign(choose))==l_True)?true:false;
  1408. viewList.push_back(choose);
  1409. undefVars.erase(x);
  1410. }
  1411. }
  1412. //cout<<"okout\n";
  1413. return true;
  1414. }
  1415. //==================================================
  1416. /*_________________________________________________________________________________________________
  1417. |
  1418. | reduceDB : () -> [void]
  1419. |
  1420. | Description:
  1421. | Remove half of the learnt clauses, minus the clauses locked by the current assignment. Locked
  1422. | clauses are clauses that are reason to some assignment. Binary clauses are never removed.
  1423. |________________________________________________________________________________________________@*/
  1424. struct reduceDB_lt {
  1425. ClauseAllocator& ca;
  1426. reduceDB_lt(ClauseAllocator& ca_) : ca(ca_) {}
  1427. bool operator () (CRef x, CRef y) const { return ca[x].activity() < ca[y].activity(); }
  1428. };
  1429. void Solver::reduceDB()
  1430. {
  1431. int i, j;
  1432. //if (local_learnts_dirty) cleanLearnts(learnts_local, LOCAL);
  1433. //local_learnts_dirty = false;
  1434. sort(learnts_local, reduceDB_lt(ca));
  1435. int limit = learnts_local.size() / 2;
  1436. for (i = j = 0; i < learnts_local.size(); i++){
  1437. Clause& c = ca[learnts_local[i]];
  1438. if (c.mark() == LOCAL)
  1439. if (c.removable() && !locked(c) && i < limit)
  1440. removeClause(learnts_local[i]);
  1441. else{
  1442. if (!c.removable()) limit++;
  1443. c.removable(true);
  1444. learnts_local[j++] = learnts_local[i]; }
  1445. }
  1446. learnts_local.shrink(i - j);
  1447. checkGarbage();
  1448. }
  1449. void Solver::reduceDB_Tier2()
  1450. {
  1451. int i, j;
  1452. for (i = j = 0; i < learnts_tier2.size(); i++){
  1453. Clause& c = ca[learnts_tier2[i]];
  1454. if (c.mark() == TIER2)
  1455. if (!locked(c) && c.touched() + 30000 < conflicts){
  1456. learnts_local.push(learnts_tier2[i]);
  1457. c.mark(LOCAL);
  1458. //c.removable(true);
  1459. c.activity() = 0;
  1460. claBumpActivity(c);
  1461. }else
  1462. learnts_tier2[j++] = learnts_tier2[i];
  1463. }
  1464. learnts_tier2.shrink(i - j);
  1465. }
  1466. void Solver::removeSatisfied(vec<CRef>& cs)
  1467. {
  1468. int i, j;
  1469. for (i = j = 0; i < cs.size(); i++){
  1470. Clause& c = ca[cs[i]];
  1471. if (satisfied(c))
  1472. removeClause(cs[i]);
  1473. else
  1474. cs[j++] = cs[i];
  1475. }
  1476. cs.shrink(i - j);
  1477. }
  1478. void Solver::safeRemoveSatisfied(vec<CRef>& cs, unsigned valid_mark)
  1479. {
  1480. int i, j;
  1481. for (i = j = 0; i < cs.size(); i++){
  1482. Clause& c = ca[cs[i]];
  1483. if (c.mark() == valid_mark)
  1484. if (satisfied(c))
  1485. removeClause(cs[i]);
  1486. else
  1487. cs[j++] = cs[i];
  1488. }
  1489. cs.shrink(i - j);
  1490. }
  1491. void Solver::rebuildOrderHeap()
  1492. {
  1493. vec<Var> vs;
  1494. for (Var v = 0; v < nVars(); v++)
  1495. if (decision[v] && value(v) == l_Undef)
  1496. vs.push(v);
  1497. order_heap_CHB .build(vs);
  1498. order_heap_VSIDS.build(vs);
  1499. order_heap_distance.build(vs);
  1500. }
  1501. /*_________________________________________________________________________________________________
  1502. |
  1503. | simplify : [void] -> [bool]
  1504. |
  1505. | Description:
  1506. | Simplify the clause database according to the current top-level assigment. Currently, the only
  1507. | thing done here is the removal of satisfied clauses, but more things can be put here.
  1508. |________________________________________________________________________________________________@*/
  1509. bool Solver::simplify()
  1510. {
  1511. assert(decisionLevel() == 0);
  1512. if (!ok || propagate() != CRef_Undef)
  1513. return ok = false;
  1514. if (nAssigns() == simpDB_assigns || (simpDB_props > 0))
  1515. return true;
  1516. // Remove satisfied clauses:
  1517. removeSatisfied(learnts_core); // Should clean core first.
  1518. safeRemoveSatisfied(learnts_tier2, TIER2);
  1519. safeRemoveSatisfied(learnts_local, LOCAL);
  1520. if (remove_satisfied) // Can be turned off.
  1521. removeSatisfied(clauses);
  1522. checkGarbage();
  1523. rebuildOrderHeap();
  1524. simpDB_assigns = nAssigns();
  1525. simpDB_props = clauses_literals + learnts_literals; // (shouldn't depend on stats really, but it will do for now)
  1526. return true;
  1527. }
  1528. // pathCs[k] is the number of variables assigned at level k,
  1529. // it is initialized to 0 at the begining and reset to 0 after the function execution
  1530. bool Solver::collectFirstUIP(CRef confl){
  1531. involved_lits.clear();
  1532. int max_level=1;
  1533. Clause& c=ca[confl]; int minLevel=decisionLevel();
  1534. for(int i=0; i<c.size(); i++) {
  1535. Var v=var(c[i]);
  1536. // assert(!seen[v]);
  1537. if (level(v)>0) {
  1538. seen[v]=1;
  1539. var_iLevel_tmp[v]=1;
  1540. pathCs[level(v)]++;
  1541. if (minLevel>level(v)) {
  1542. minLevel=level(v);
  1543. assert(minLevel>0);
  1544. }
  1545. // varBumpActivity(v);
  1546. }
  1547. }
  1548. int limit=trail_lim[minLevel-1];
  1549. for(int i=trail.size()-1; i>=limit; i--) {
  1550. Lit p=trail[i]; Var v=var(p);
  1551. if (seen[v]) {
  1552. int currentDecLevel=level(v);
  1553. // if (currentDecLevel==decisionLevel())
  1554. // varBumpActivity(v);
  1555. seen[v]=0;
  1556. if (--pathCs[currentDecLevel]!=0) {
  1557. Clause& rc=ca[reason(v)];
  1558. int reasonVarLevel=var_iLevel_tmp[v]+1;
  1559. if(reasonVarLevel>max_level) max_level=reasonVarLevel;
  1560. if (rc.size()==2 && value(rc[0])==l_False) {
  1561. // Special case for binary clauses
  1562. // The first one has to be SAT
  1563. assert(value(rc[1]) != l_False);
  1564. Lit tmp = rc[0];
  1565. rc[0] = rc[1], rc[1] = tmp;
  1566. }
  1567. for (int j = 1; j < rc.size(); j++){
  1568. Lit q = rc[j]; Var v1=var(q);
  1569. if (level(v1) > 0) {
  1570. if (minLevel>level(v1)) {
  1571. minLevel=level(v1); limit=trail_lim[minLevel-1]; assert(minLevel>0);
  1572. }
  1573. if (seen[v1]) {
  1574. if (var_iLevel_tmp[v1]<reasonVarLevel)
  1575. var_iLevel_tmp[v1]=reasonVarLevel;
  1576. }
  1577. else {
  1578. var_iLevel_tmp[v1]=reasonVarLevel;
  1579. // varBumpActivity(v1);
  1580. seen[v1] = 1;
  1581. pathCs[level(v1)]++;
  1582. }
  1583. }
  1584. }
  1585. }
  1586. involved_lits.push(p);
  1587. }
  1588. }
  1589. double inc=var_iLevel_inc;
  1590. vec<int> level_incs; level_incs.clear();
  1591. for(int i=0;i<max_level;i++){
  1592. level_incs.push(inc);
  1593. inc = inc/my_var_decay;
  1594. }
  1595. for(int i=0;i<involved_lits.size();i++){
  1596. Var v =var(involved_lits[i]);
  1597. // double old_act=activity_distance[v];
  1598. // activity_distance[v] +=var_iLevel_inc * var_iLevel_tmp[v];
  1599. activity_distance[v]+=var_iLevel_tmp[v]*level_incs[var_iLevel_tmp[v]-1];
  1600. if(activity_distance[v]>1e100){
  1601. for(int vv=0;vv<nVars();vv++)
  1602. activity_distance[vv] *= 1e-100;
  1603. var_iLevel_inc*=1e-100;
  1604. for(int j=0; j<max_level; j++) level_incs[j]*=1e-100;
  1605. }
  1606. if (order_heap_distance.inHeap(v))
  1607. order_heap_distance.decrease(v);
  1608. // var_iLevel_inc *= (1 / my_var_decay);
  1609. }
  1610. var_iLevel_inc=level_incs[level_incs.size()-1];
  1611. return true;
  1612. }
  1613. struct UIPOrderByILevel_Lt {
  1614. Solver& solver;
  1615. const vec<double>& var_iLevel;
  1616. bool operator () (Lit x, Lit y) const
  1617. {
  1618. return var_iLevel[var(x)] < var_iLevel[var(y)] ||
  1619. (var_iLevel[var(x)]==var_iLevel[var(y)]&& solver.level(var(x))>solver.level(var(y)));
  1620. }
  1621. UIPOrderByILevel_Lt(const vec<double>& iLevel, Solver& para_solver) : solver(para_solver), var_iLevel(iLevel) { }
  1622. };
  1623. CRef Solver::propagateLits(vec<Lit>& lits) {
  1624. Lit lit;
  1625. int i;
  1626. for(i=lits.size()-1; i>=0; i--) {
  1627. lit=lits[i];
  1628. if (value(lit) == l_Undef) {
  1629. newDecisionLevel();
  1630. uncheckedEnqueue(lit);
  1631. CRef confl = propagate();
  1632. if (confl != CRef_Undef) {
  1633. return confl;
  1634. }
  1635. }
  1636. }
  1637. return CRef_Undef;
  1638. }
  1639. /*_________________________________________________________________________________________________
  1640. |
  1641. | search : (nof_conflicts : int) (params : const SearchParams&) -> [lbool]
  1642. |
  1643. | Description:
  1644. | Search for a model the specified number of conflicts.
  1645. |
  1646. | Output:
  1647. | 'l_True' if a partial assigment that is consistent with respect to the clauseset is found. If
  1648. | all variables are decision variables, this means that the clause set is satisfiable. 'l_False'
  1649. | if the clause set is unsatisfiable. 'l_Undef' if the bound on number of conflicts is reached.
  1650. |________________________________________________________________________________________________@*/
  1651. lbool Solver::search(int& nof_conflicts)
  1652. {
  1653. //--------------------------
  1654. bool hasnotCalled = true;
  1655. //==========================
  1656. assert(ok);
  1657. int backtrack_level;
  1658. int lbd;
  1659. vec<Lit> learnt_clause;
  1660. bool cached = false;
  1661. starts++;
  1662. // simplify
  1663. //
  1664. if (conflicts >= curSimplify * nbconfbeforesimplify){
  1665. // printf("c ### simplifyAll on conflict : %lld\n", conflicts);
  1666. //printf("nbClauses: %d, nbLearnts_core: %d, nbLearnts_tier2: %d, nbLearnts_local: %d, nbLearnts: %d\n",
  1667. // clauses.size(), learnts_core.size(), learnts_tier2.size(), learnts_local.size(),
  1668. // learnts_core.size() + learnts_tier2.size() + learnts_local.size());
  1669. nbSimplifyAll++;
  1670. if (!simplifyAll()){
  1671. return l_False;
  1672. }
  1673. curSimplify = (conflicts / nbconfbeforesimplify) + 1;
  1674. nbconfbeforesimplify += incSimplify;
  1675. }
  1676. for (;;){
  1677. //----------------------
  1678. /*
  1679. if(cpuTime()>timeLimit){
  1680. printf("out of Time range!\n");
  1681. exit(0);
  1682. }
  1683. */
  1684. //======================
  1685. CRef confl = propagate();
  1686. if (confl != CRef_Undef){
  1687. // CONFLICT
  1688. if (VSIDS){
  1689. if (--timer == 0 && var_decay < 0.95) timer = 5000, var_decay += 0.01;
  1690. }else
  1691. if (step_size > min_step_size) step_size -= step_size_dec;
  1692. conflicts++; nof_conflicts--;
  1693. if (conflicts == 100000 && learnts_core.size() < 100) core_lbd_cut = 5;
  1694. if (decisionLevel() == 0) return l_False;
  1695. learnt_clause.clear();
  1696. if(conflicts>50000) DISTANCE=0;
  1697. else DISTANCE=1;
  1698. if(VSIDS && DISTANCE)
  1699. collectFirstUIP(confl);
  1700. analyze(confl, learnt_clause, backtrack_level, lbd);
  1701. cancelUntil(backtrack_level);
  1702. lbd--;
  1703. if (VSIDS){
  1704. cached = false;
  1705. conflicts_VSIDS++;
  1706. lbd_queue.push(lbd);
  1707. global_lbd_sum += (lbd > 50 ? 50 : lbd); }
  1708. if (learnt_clause.size() == 1){
  1709. uncheckedEnqueue(learnt_clause[0]);
  1710. }else{
  1711. CRef cr = ca.alloc(learnt_clause, true);
  1712. ca[cr].set_lbd(lbd);
  1713. if (lbd <= core_lbd_cut){
  1714. learnts_core.push(cr);
  1715. ca[cr].mark(CORE);
  1716. }else if (lbd <= 6){
  1717. learnts_tier2.push(cr);
  1718. ca[cr].mark(TIER2);
  1719. ca[cr].touched() = conflicts;
  1720. }else{
  1721. learnts_local.push(cr);
  1722. claBumpActivity(ca[cr]); }
  1723. attachClause(cr);
  1724. uncheckedEnqueue(learnt_clause[0], cr);
  1725. }
  1726. if (drup_file){
  1727. #ifdef BIN_DRUP
  1728. binDRUP('a', learnt_clause, drup_file);
  1729. #else
  1730. for (int i = 0; i < learnt_clause.size(); i++)
  1731. fprintf(drup_file, "%i ", (var(learnt_clause[i]) + 1) * (-2 * sign(learnt_clause[i]) + 1));
  1732. fprintf(drup_file, "0\n");
  1733. #endif
  1734. }
  1735. if (VSIDS) varDecayActivity();
  1736. claDecayActivity();
  1737. /*if (--learntsize_adjust_cnt == 0){
  1738. learntsize_adjust_confl *= learntsize_adjust_inc;
  1739. learntsize_adjust_cnt = (int)learntsize_adjust_confl;
  1740. max_learnts *= learntsize_inc;
  1741. if (verbosity >= 1)
  1742. printf("c | %9d | %7d %8d %8d | %8d %8d %6.0f | %6.3f %% |\n",
  1743. (int)conflicts,
  1744. (int)dec_vars - (trail_lim.size() == 0 ? trail.size() : trail_lim[0]), nClauses(), (int)clauses_literals,
  1745. (int)max_learnts, nLearnts(), (double)learnts_literals/nLearnts(), progressEstimate()*100);
  1746. }*/
  1747. }else{
  1748. //----------------------------------
  1749. //delWith ConflPropagation
  1750. /* old version
  1751. if(getNowRatio() > conflRatio && nLearnts() > lastLearntNum * areaRatio+1){
  1752. cout<<"call "<<starts<<" ";
  1753. cout<<getNowRatio()<<"\t "<<conflRatio<<"\t "<<lastLearntNum<<"\t "<<nLearnts()<<"\t "<<UnComTime<<"\t "<<cpuTime()-UnComTime<<"\t ";
  1754. while(true){
  1755. CRef confl = propagateConfl();
  1756. if(confl == CRef_Undef){
  1757. //has no confl,
  1758. Lit next = lit_Undef;
  1759. next = pickBranchLit();
  1760. if (next == lit_Undef){
  1761. getInitModelByValue();
  1762. bool res = this->callUnCom(); cout<<"end\n";
  1763. callNum ++;
  1764. lastLearntNum = nLearnts();
  1765. if(res){
  1766. solvedByUncom = true;
  1767. ok = true;
  1768. return l_True;
  1769. }else{
  1770. cancelUntil(0);
  1771. ok = true;
  1772. return l_Undef;
  1773. }
  1774. }else{
  1775. newDecisionLevel();
  1776. uncheckedEnqueue(next);
  1777. }
  1778. }
  1779. }
  1780. }*/
  1781. if(hasnotCalled && getNowRatio() > conflRatio && nLearnts() > lastLearntNum * areaRatio+1 && UnComTime < cpuTime()*timeRatio){
  1782. hasnotCalled = false;
  1783. cout<<"c call "<<starts<<" ";
  1784. cout<<getNowRatio()<<"\t "<<conflRatio<<"\t "<<lastLearntNum<<"\t "<<nLearnts()<<"\t "<<UnComTime<<"\t "<<cpuTime()<<"\t ";
  1785. //construction
  1786. double stime = cpuTime();
  1787. propagateConflSim();
  1788. bool res = this->callUnCom();
  1789. double etime = cpuTime();
  1790. UnComTime += (etime-stime);
  1791. callNum++;
  1792. lastLearntNum = nLearnts();
  1793. cout<<"end\n";
  1794. if(res){
  1795. solvedByUncom = true;
  1796. return l_True;
  1797. }
  1798. }
  1799. //==================================
  1800. // NO CONFLICT
  1801. bool restart = false;
  1802. if (!VSIDS)
  1803. restart = nof_conflicts <= 0;
  1804. else if (!cached){
  1805. restart = lbd_queue.full() && (lbd_queue.avg() * 0.8 > global_lbd_sum / conflicts_VSIDS);
  1806. cached = true;
  1807. }
  1808. if (restart /*|| !withinBudget()*/){
  1809. lbd_queue.clear();
  1810. cached = false;
  1811. // Reached bound on number of conflicts:
  1812. progress_estimate = progressEstimate();
  1813. cancelUntil(0);
  1814. return l_Undef; }
  1815. // Simplify the set of problem clauses:
  1816. if (decisionLevel() == 0 && !simplify())
  1817. return l_False;
  1818. if (conflicts >= next_T2_reduce){
  1819. next_T2_reduce = conflicts + 10000;
  1820. reduceDB_Tier2(); }
  1821. if (conflicts >= next_L_reduce){
  1822. next_L_reduce = conflicts + 15000;
  1823. reduceDB(); }
  1824. Lit next = lit_Undef;
  1825. /*while (decisionLevel() < assumptions.size()){
  1826. // Perform user provided assumption:
  1827. Lit p = assumptions[decisionLevel()];
  1828. if (value(p) == l_True){
  1829. // Dummy decision level:
  1830. newDecisionLevel();
  1831. }else if (value(p) == l_False){
  1832. analyzeFinal(~p, conflict);
  1833. return l_False;
  1834. }else{
  1835. next = p;
  1836. break;
  1837. }
  1838. }
  1839. if (next == lit_Undef)*/{
  1840. // New variable decision:
  1841. decisions++;
  1842. next = pickBranchLit();
  1843. if (next == lit_Undef)
  1844. // Model found:
  1845. return l_True;
  1846. }
  1847. // Increase decision level and enqueue 'next'
  1848. newDecisionLevel();
  1849. uncheckedEnqueue(next);
  1850. }
  1851. }
  1852. }
  1853. double Solver::progressEstimate() const
  1854. {
  1855. double progress = 0;
  1856. double F = 1.0 / nVars();
  1857. for (int i = 0; i <= decisionLevel(); i++){
  1858. int beg = i == 0 ? 0 : trail_lim[i - 1];
  1859. int end = i == decisionLevel() ? trail.size() : trail_lim[i];
  1860. progress += pow(F, i) * (end - beg);
  1861. }
  1862. return progress / nVars();
  1863. }
  1864. /*
  1865. Finite subsequences of the Luby-sequence:
  1866. 0: 1
  1867. 1: 1 1 2
  1868. 2: 1 1 2 1 1 2 4
  1869. 3: 1 1 2 1 1 2 4 1 1 2 1 1 2 4 8
  1870. ...
  1871. */
  1872. static double luby(double y, int x){
  1873. // Find the finite subsequence that contains index 'x', and the
  1874. // size of that subsequence:
  1875. int size, seq;
  1876. for (size = 1, seq = 0; size < x+1; seq++, size = 2*size+1);
  1877. while (size-1 != x){
  1878. size = (size-1)>>1;
  1879. seq--;
  1880. x = x % size;
  1881. }
  1882. return pow(y, seq);
  1883. }
  1884. static bool switch_mode = false;
  1885. static void SIGALRM_switch(int signum) { switch_mode = true; }
  1886. // NOTE: assumptions passed in member-variable 'assumptions'.
  1887. lbool Solver::solve_()
  1888. {
  1889. double switchMode = opt_switchMode;
  1890. signal(SIGALRM, SIGALRM_switch);
  1891. alarm(switchMode);
  1892. model.clear();
  1893. conflict.clear();
  1894. if (!ok) return l_False;
  1895. solves++;
  1896. max_learnts = nClauses() * learntsize_factor;
  1897. learntsize_adjust_confl = learntsize_adjust_start_confl;
  1898. learntsize_adjust_cnt = (int)learntsize_adjust_confl;
  1899. lbool status = l_Undef;
  1900. if (verbosity >= 1){
  1901. printf("c ============================[ ReasonLS Maple_LD ]==============================\n");
  1902. //printf("c | Conflicts | ORIGINAL | LEARNT | Progress |\n");
  1903. //printf("c | | Vars Clauses Literals | Limit Clauses Lit/Cl | |\n");
  1904. printf("c areaRatio:%lf conflRatio:%lf timeRatio:%lf\n",areaRatio,conflRatio,timeRatio);
  1905. printf("c maxFlipRatio:%lf initFlipRatio:%lf raiseFlipRatio:%lf\t \n",maxflipRatio,lastflipRatio,raiseFlipRatio);
  1906. printf("c -------------------------------------------------------------------------------\n");
  1907. printf("c call|nRestart|trueRatio|expect|LastLearnts|Learnts|ccnrTime |totalTime | end\n");
  1908. printf("c ===============================================================================\n");
  1909. // c ===============================================================================
  1910. // c call 289 0.900706 0.9 0 22857 0 4.04946 end
  1911. // c call 387 0.906502 0.9 22857 43020 3.70781 10.7654 end
  1912. // c ===============================================================================
  1913. }
  1914. //-----------------
  1915. lastLearntNum = nLearnts();
  1916. //=================
  1917. add_tmp.clear();
  1918. VSIDS = true;
  1919. int init = 10000;
  1920. while (status == l_Undef && init > 0 /*&& withinBudget()*/)
  1921. status = search(init);
  1922. VSIDS = false;
  1923. // Search:
  1924. int curr_restarts = 0;
  1925. while (status == l_Undef /*&& withinBudget()*/){
  1926. if (VSIDS){
  1927. int weighted = INT32_MAX;
  1928. status = search(weighted);
  1929. }else{
  1930. int nof_conflicts = luby(restart_inc, curr_restarts) * restart_first;
  1931. curr_restarts++;
  1932. status = search(nof_conflicts);
  1933. }
  1934. if (!VSIDS && switch_mode){
  1935. VSIDS = true;
  1936. printf("c Switched to VSIDS.\n");
  1937. fflush(stdout);
  1938. picked.clear();
  1939. conflicted.clear();
  1940. almost_conflicted.clear();
  1941. #ifdef ANTI_EXPLORATION
  1942. canceled.clear();
  1943. #endif
  1944. }
  1945. }
  1946. if (verbosity >= 1)
  1947. printf("c ===============================================================================\n");
  1948. #ifdef BIN_DRUP
  1949. if (drup_file && status == l_False) binDRUP_flush(drup_file);
  1950. #endif
  1951. if (status == l_True){
  1952. // Extend & copy model:
  1953. model.growTo(nVars());
  1954. //------------------------------------------------------------------------------------------------
  1955. for (int i = 0; i < nVars(); i++) solvedByUncom ? model[i] = result_call[i] :model[i] = value(i);
  1956. //================================================================================================
  1957. }else if (status == l_False && conflict.size() == 0)
  1958. ok = false;
  1959. cancelUntil(0);
  1960. return status;
  1961. }
  1962. //=================================================================================================
  1963. // Writing CNF to DIMACS:
  1964. //
  1965. // FIXME: this needs to be rewritten completely.
  1966. static Var mapVar(Var x, vec<Var>& map, Var& max)
  1967. {
  1968. if (map.size() <= x || map[x] == -1){
  1969. map.growTo(x+1, -1);
  1970. map[x] = max++;
  1971. }
  1972. return map[x];
  1973. }
  1974. void Solver::toDimacs(FILE* f, Clause& c, vec<Var>& map, Var& max)
  1975. {
  1976. if (satisfied(c)) return;
  1977. for (int i = 0; i < c.size(); i++)
  1978. if (value(c[i]) != l_False)
  1979. fprintf(f, "%s%d ", sign(c[i]) ? "-" : "", mapVar(var(c[i]), map, max)+1);
  1980. fprintf(f, "0\n");
  1981. }
  1982. void Solver::toDimacs(const char *file, const vec<Lit>& assumps)
  1983. {
  1984. FILE* f = fopen(file, "wr");
  1985. if (f == NULL)
  1986. fprintf(stderr, "could not open file %s\n", file), exit(1);
  1987. toDimacs(f, assumps);
  1988. fclose(f);
  1989. }
  1990. void Solver::toDimacs(FILE* f, const vec<Lit>& assumps)
  1991. {
  1992. // Handle case when solver is in contradictory state:
  1993. if (!ok){
  1994. fprintf(f, "p cnf 1 2\n1 0\n-1 0\n");
  1995. return; }
  1996. vec<Var> map; Var max = 0;
  1997. // Cannot use removeClauses here because it is not safe
  1998. // to deallocate them at this point. Could be improved.
  1999. int cnt = 0;
  2000. for (int i = 0; i < clauses.size(); i++)
  2001. if (!satisfied(ca[clauses[i]]))
  2002. cnt++;
  2003. for (int i = 0; i < clauses.size(); i++)
  2004. if (!satisfied(ca[clauses[i]])){
  2005. Clause& c = ca[clauses[i]];
  2006. for (int j = 0; j < c.size(); j++)
  2007. if (value(c[j]) != l_False)
  2008. mapVar(var(c[j]), map, max);
  2009. }
  2010. // Assumptions are added as unit clauses:
  2011. cnt += assumptions.size();
  2012. fprintf(f, "p cnf %d %d\n", max, cnt);
  2013. for (int i = 0; i < assumptions.size(); i++){
  2014. assert(value(assumptions[i]) != l_False);
  2015. fprintf(f, "%s%d 0\n", sign(assumptions[i]) ? "-" : "", mapVar(var(assumptions[i]), map, max)+1);
  2016. }
  2017. for (int i = 0; i < clauses.size(); i++)
  2018. toDimacs(f, ca[clauses[i]], map, max);
  2019. if (verbosity > 0)
  2020. printf("c Wrote %d clauses with %d variables.\n", cnt, max);
  2021. }
  2022. //=================================================================================================
  2023. // Garbage Collection methods:
  2024. void Solver::relocAll(ClauseAllocator& to)
  2025. {
  2026. // All watchers:
  2027. //
  2028. // for (int i = 0; i < watches.size(); i++)
  2029. watches.cleanAll();
  2030. watches_bin.cleanAll();
  2031. for (int v = 0; v < nVars(); v++)
  2032. for (int s = 0; s < 2; s++){
  2033. Lit p = mkLit(v, s);
  2034. // printf(" >>> RELOCING: %s%d\n", sign(p)?"-":"", var(p)+1);
  2035. vec<Watcher>& ws = watches[p];
  2036. for (int j = 0; j < ws.size(); j++)
  2037. ca.reloc(ws[j].cref, to);
  2038. vec<Watcher>& ws_bin = watches_bin[p];
  2039. for (int j = 0; j < ws_bin.size(); j++)
  2040. ca.reloc(ws_bin[j].cref, to);
  2041. }
  2042. // All reasons:
  2043. //
  2044. for (int i = 0; i < trail.size(); i++){
  2045. Var v = var(trail[i]);
  2046. if (reason(v) != CRef_Undef && (ca[reason(v)].reloced() || locked(ca[reason(v)])))
  2047. ca.reloc(vardata[v].reason, to);
  2048. }
  2049. // All learnt:
  2050. //
  2051. for (int i = 0; i < learnts_core.size(); i++)
  2052. ca.reloc(learnts_core[i], to);
  2053. for (int i = 0; i < learnts_tier2.size(); i++)
  2054. ca.reloc(learnts_tier2[i], to);
  2055. for (int i = 0; i < learnts_local.size(); i++)
  2056. ca.reloc(learnts_local[i], to);
  2057. // All original:
  2058. //
  2059. int i, j;
  2060. for (i = j = 0; i < clauses.size(); i++)
  2061. if (ca[clauses[i]].mark() != 1){
  2062. ca.reloc(clauses[i], to);
  2063. clauses[j++] = clauses[i]; }
  2064. clauses.shrink(i - j);
  2065. }
  2066. void Solver::garbageCollect()
  2067. {
  2068. // Initialize the next region to a size corresponding to the estimated utilization degree. This
  2069. // is not precise but should avoid some unnecessary reallocations for the new region:
  2070. ClauseAllocator to(ca.size() - ca.wasted());
  2071. relocAll(to);
  2072. if (verbosity >= 2)
  2073. printf("c | Garbage collection: %12d bytes => %12d bytes |\n",
  2074. ca.size()*ClauseAllocator::Unit_Size, to.size()*ClauseAllocator::Unit_Size);
  2075. to.moveTo(ca);
  2076. }
  2077. //-------------------------------------------------------
  2078. //--------------------------------------------
  2079. void Solver::getInitModelByValue(){
  2080. falseModel.clear();
  2081. for(int i=0;i<nVars();i++){
  2082. if(value(i)==l_False){
  2083. falseModel.push_back(false);
  2084. }else{
  2085. falseModel.push_back(true);
  2086. }
  2087. }
  2088. }
  2089. /*
  2090. bool Solver::callUnCom(){
  2091. ls_solver ccnr;
  2092. int ret = build_instance(ccnr);
  2093. if (!ret) { printf("c init fail\n");return 1;}
  2094. vector<bool> * vp = & falseModel;
  2095. long long nowstep = (long long ) nVars()*lastflipRatio;
  2096. ccnr._max_steps = min(nowstep, maxstep);
  2097. lastflipRatio *= raiseFlipRatio;
  2098. if(ccnr.local_search(vp)){
  2099. //ccnr.print_solution();
  2100. result_call.clear();
  2101. for (int v=1; v <= ccnr._num_vars; v++)
  2102. {
  2103. if (ccnr._solution[v]==0) result_call.push_back(l_False);
  2104. else result_call.push_back(l_True);
  2105. }
  2106. return true;
  2107. }else{
  2108. //UnComTime+=get_runtime();
  2109. return false;
  2110. }
  2111. }
  2112. */
  2113. bool Solver::callUnCom(){
  2114. ls_solver ccnr;
  2115. int ret = build_instance(ccnr);
  2116. if (!ret) { printf("c init fail\n");return 1;}
  2117. if(timeRatio <= 1){
  2118. //Normal Call
  2119. vector<bool> * vp = & falseModel;
  2120. long long nowstep = (long long ) nVars()*lastflipRatio;
  2121. ccnr._max_steps = min(nowstep, maxstep);
  2122. lastflipRatio *= raiseFlipRatio;
  2123. if(ccnr.local_search(vp)){
  2124. //ccnr.print_solution();
  2125. result_call.clear();
  2126. for (int v=1; v <= ccnr._num_vars; v++)
  2127. {
  2128. if (ccnr._solution[v]==0) result_call.push_back(l_False);
  2129. else result_call.push_back(l_True);
  2130. }
  2131. return true;
  2132. }else{
  2133. //UnComTime+=get_runtime();
  2134. return false;
  2135. }
  2136. }else{
  2137. //largely increase call time of cnnr
  2138. ccnr._max_steps = maxstep;
  2139. ccnr._max_tries = 10;
  2140. if(ccnr.local_search()){
  2141. //ccnr.print_solution();
  2142. result_call.clear();
  2143. for (int v=1; v <= ccnr._num_vars; v++)
  2144. {
  2145. if (ccnr._solution[v]==0) result_call.push_back(l_False);
  2146. else result_call.push_back(l_True);
  2147. }
  2148. return true;
  2149. }else{
  2150. //UnComTime+=get_runtime();
  2151. return false;
  2152. }
  2153. }
  2154. }
  2155. bool Solver::build_instance(ls_solver &ls_s){
  2156. int ct,i,j,v;
  2157. int cur_lit;
  2158. vector<int> temp_clause;
  2159. ls_s._num_vars = nVars();
  2160. ls_s._num_clauses = nClauses()+nLearnts()+((trail_lim.size()>0)? trail_lim[0] : 0);
  2161. //cout<<ls_s._num_vars<<" "<<ls_s._num_clauses<<endl;
  2162. if(!ls_s.make_space()) return false;
  2163. //read clauses, each at a time
  2164. ct = 0;
  2165. //clause ctmp;
  2166. for(int md=0;md<4;md++){
  2167. vec<CRef> &vs = (md==0)?clauses:(md==1?learnts_core:(md==2?learnts_tier2:learnts_local));
  2168. for(i=0;i<vs.size();i++){
  2169. //ls_s._clauses.push_back(ctmp);
  2170. temp_clause.clear();
  2171. CRef &cr = vs[i];
  2172. Clause &c = ca[cr];
  2173. vector<int>().swap(temp_clause);
  2174. for(j=0;j<c.size();j++){
  2175. cur_lit = toFormal(c[j]);
  2176. temp_clause.push_back(cur_lit);
  2177. }
  2178. for(int item:temp_clause){
  2179. ls_s._clauses[ct].literals.push_back(lit(item,ct));
  2180. }
  2181. ct++;
  2182. }
  2183. }
  2184. if(trail_lim.size() >0 ){
  2185. for(i=0;i<trail_lim[0];i++){
  2186. //ls_s._clauses.push_back(ctmp);
  2187. ls_s._clauses[ct].literals.push_back(lit(toFormal(trail[i]),ct));
  2188. ct++;
  2189. }
  2190. }
  2191. for (int c=0; c < ls_s._num_clauses; c++)
  2192. {
  2193. for(lit item: ls_s._clauses[c].literals)
  2194. {
  2195. v = item.var_num;
  2196. ls_s._vars[v].literals.push_back(item);
  2197. }
  2198. }
  2199. ls_s.build_neighborhood();
  2200. return true;
  2201. }