Solver.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\DependencyResolver;
  12. use Composer\Repository\RepositoryInterface;
  13. use Composer\Repository\PlatformRepository;
  14. /**
  15. * @author Nils Adermann <naderman@naderman.de>
  16. */
  17. class Solver
  18. {
  19. const BRANCH_LITERALS = 0;
  20. const BRANCH_LEVEL = 1;
  21. /** @var PolicyInterface */
  22. protected $policy;
  23. /** @var Pool */
  24. protected $pool;
  25. /** @var RepositoryInterface */
  26. protected $installed;
  27. /** @var Ruleset */
  28. protected $rules;
  29. /** @var RuleSetGenerator */
  30. protected $ruleSetGenerator;
  31. /** @var array */
  32. protected $jobs;
  33. /** @var int[] */
  34. protected $updateMap = array();
  35. /** @var RuleWatchGraph */
  36. protected $watchGraph;
  37. /** @var Decisions */
  38. protected $decisions;
  39. /** @var int[] */
  40. protected $installedMap;
  41. /** @var int */
  42. protected $propagateIndex;
  43. /** @var array[] */
  44. protected $branches = array();
  45. /** @var Problem[] */
  46. protected $problems = array();
  47. /** @var array */
  48. protected $learnedPool = array();
  49. /** @var array */
  50. protected $learnedWhy = array();
  51. /**
  52. * @param PolicyInterface $policy
  53. * @param Pool $pool
  54. * @param RepositoryInterface $installed
  55. */
  56. public function __construct(PolicyInterface $policy, Pool $pool, RepositoryInterface $installed)
  57. {
  58. $this->policy = $policy;
  59. $this->pool = $pool;
  60. $this->installed = $installed;
  61. $this->ruleSetGenerator = new RuleSetGenerator($policy, $pool);
  62. }
  63. /**
  64. * @return int
  65. */
  66. public function getRuleSetSize()
  67. {
  68. return count($this->rules);
  69. }
  70. // aka solver_makeruledecisions
  71. private function makeAssertionRuleDecisions()
  72. {
  73. $decisionStart = count($this->decisions) - 1;
  74. $rulesCount = count($this->rules);
  75. for ($ruleIndex = 0; $ruleIndex < $rulesCount; $ruleIndex++) {
  76. $rule = $this->rules->ruleById[$ruleIndex];
  77. if (!$rule->isAssertion() || $rule->isDisabled()) {
  78. continue;
  79. }
  80. $literals = $rule->literals;
  81. $literal = $literals[0];
  82. if (!$this->decisions->decided(abs($literal))) {
  83. $this->decisions->decide($literal, 1, $rule);
  84. continue;
  85. }
  86. if ($this->decisions->satisfy($literal)) {
  87. continue;
  88. }
  89. // found a conflict
  90. if (RuleSet::TYPE_LEARNED === $rule->getType()) {
  91. $rule->disable();
  92. continue;
  93. }
  94. $conflict = $this->decisions->decisionRule($literal);
  95. if ($conflict && RuleSet::TYPE_PACKAGE === $conflict->getType()) {
  96. $problem = new Problem($this->pool);
  97. $problem->addRule($rule);
  98. $problem->addRule($conflict);
  99. $this->disableProblem($rule);
  100. $this->problems[] = $problem;
  101. continue;
  102. }
  103. // conflict with another job
  104. $problem = new Problem($this->pool);
  105. $problem->addRule($rule);
  106. $problem->addRule($conflict);
  107. // push all of our rules (can only be job rules)
  108. // asserting this literal on the problem stack
  109. foreach ($this->rules->getIteratorFor(RuleSet::TYPE_JOB) as $assertRule) {
  110. if ($assertRule->isDisabled() || !$assertRule->isAssertion()) {
  111. continue;
  112. }
  113. $assertRuleLiterals = $assertRule->literals;
  114. $assertRuleLiteral = $assertRuleLiterals[0];
  115. if (abs($literal) !== abs($assertRuleLiteral)) {
  116. continue;
  117. }
  118. $problem->addRule($assertRule);
  119. $this->disableProblem($assertRule);
  120. }
  121. $this->problems[] = $problem;
  122. $this->decisions->resetToOffset($decisionStart);
  123. $ruleIndex = -1;
  124. }
  125. }
  126. protected function setupInstalledMap()
  127. {
  128. $this->installedMap = array();
  129. foreach ($this->installed->getPackages() as $package) {
  130. $this->installedMap[$package->id] = $package;
  131. }
  132. }
  133. /**
  134. * @param bool $ignorePlatformReqs
  135. */
  136. protected function checkForRootRequireProblems($ignorePlatformReqs)
  137. {
  138. foreach ($this->jobs as $job) {
  139. switch ($job['cmd']) {
  140. case 'update':
  141. $packages = $this->pool->whatProvides($job['packageName'], $job['constraint']);
  142. foreach ($packages as $package) {
  143. if (isset($this->installedMap[$package->id])) {
  144. $this->updateMap[$package->id] = true;
  145. }
  146. }
  147. break;
  148. case 'update-all':
  149. foreach ($this->installedMap as $package) {
  150. $this->updateMap[$package->id] = true;
  151. }
  152. break;
  153. case 'install':
  154. if ($ignorePlatformReqs && preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $job['packageName'])) {
  155. break;
  156. }
  157. if (!$this->pool->whatProvides($job['packageName'], $job['constraint'])) {
  158. $problem = new Problem($this->pool);
  159. $problem->addRule(new Rule(array(), null, null, $job));
  160. $this->problems[] = $problem;
  161. }
  162. break;
  163. }
  164. }
  165. }
  166. /**
  167. * @param Request $request
  168. * @param bool $ignorePlatformReqs
  169. * @return array
  170. */
  171. public function solve(Request $request, $ignorePlatformReqs = false)
  172. {
  173. $this->jobs = $request->getJobs();
  174. $this->setupInstalledMap();
  175. $this->rules = $this->ruleSetGenerator->getRulesFor($this->jobs, $this->installedMap, $ignorePlatformReqs);
  176. $this->checkForRootRequireProblems($ignorePlatformReqs);
  177. $this->decisions = new Decisions($this->pool);
  178. $this->watchGraph = new RuleWatchGraph;
  179. foreach ($this->rules as $rule) {
  180. $this->watchGraph->insert(new RuleWatchNode($rule));
  181. }
  182. /* make decisions based on job/update assertions */
  183. $this->makeAssertionRuleDecisions();
  184. $this->runSat(true);
  185. // decide to remove everything that's installed and undecided
  186. foreach ($this->installedMap as $packageId => $void) {
  187. if ($this->decisions->undecided($packageId)) {
  188. $this->decisions->decide(-$packageId, 1, null);
  189. }
  190. }
  191. if ($this->problems) {
  192. throw new SolverProblemsException($this->problems, $this->installedMap);
  193. }
  194. $transaction = new Transaction($this->policy, $this->pool, $this->installedMap, $this->decisions);
  195. return $transaction->getOperations();
  196. }
  197. /**
  198. * Makes a decision and propagates it to all rules.
  199. *
  200. * Evaluates each term affected by the decision (linked through watches)
  201. * If we find unit rules we make new decisions based on them
  202. *
  203. * @param int $level
  204. * @return Rule|null A rule on conflict, otherwise null.
  205. */
  206. protected function propagate($level)
  207. {
  208. while ($this->decisions->validOffset($this->propagateIndex)) {
  209. $decision = $this->decisions->atOffset($this->propagateIndex);
  210. $conflict = $this->watchGraph->propagateLiteral(
  211. $decision[Decisions::DECISION_LITERAL],
  212. $level,
  213. $this->decisions
  214. );
  215. $this->propagateIndex++;
  216. if ($conflict) {
  217. return $conflict;
  218. }
  219. }
  220. return null;
  221. }
  222. /**
  223. * Reverts a decision at the given level.
  224. *
  225. * @param int $level
  226. */
  227. private function revert($level)
  228. {
  229. while (!$this->decisions->isEmpty()) {
  230. $literal = $this->decisions->lastLiteral();
  231. if ($this->decisions->undecided($literal)) {
  232. break;
  233. }
  234. $decisionLevel = $this->decisions->decisionLevel($literal);
  235. if ($decisionLevel <= $level) {
  236. break;
  237. }
  238. $this->decisions->revertLast();
  239. $this->propagateIndex = count($this->decisions);
  240. }
  241. while (!empty($this->branches) && $this->branches[count($this->branches) - 1][self::BRANCH_LEVEL] >= $level) {
  242. array_pop($this->branches);
  243. }
  244. }
  245. /**
  246. * setpropagatelearn
  247. *
  248. * add free decision (a positive literal) to decision queue
  249. * increase level and propagate decision
  250. * return if no conflict.
  251. *
  252. * in conflict case, analyze conflict rule, add resulting
  253. * rule to learnt rule set, make decision from learnt
  254. * rule (always unit) and re-propagate.
  255. *
  256. * returns the new solver level or 0 if unsolvable
  257. *
  258. * @param int $level
  259. * @param string|int $literal
  260. * @param bool $disableRules
  261. * @param Rule $rule
  262. * @return int
  263. */
  264. private function setPropagateLearn($level, $literal, $disableRules, Rule $rule)
  265. {
  266. $level++;
  267. $this->decisions->decide($literal, $level, $rule);
  268. while (true) {
  269. $rule = $this->propagate($level);
  270. if (!$rule) {
  271. break;
  272. }
  273. if ($level == 1) {
  274. return $this->analyzeUnsolvable($rule, $disableRules);
  275. }
  276. // conflict
  277. list($learnLiteral, $newLevel, $newRule, $why) = $this->analyze($level, $rule);
  278. if ($newLevel <= 0 || $newLevel >= $level) {
  279. throw new SolverBugException(
  280. "Trying to revert to invalid level ".(int) $newLevel." from level ".(int) $level."."
  281. );
  282. } elseif (!$newRule) {
  283. throw new SolverBugException(
  284. "No rule was learned from analyzing $rule at level $level."
  285. );
  286. }
  287. $level = $newLevel;
  288. $this->revert($level);
  289. $this->rules->add($newRule, RuleSet::TYPE_LEARNED);
  290. $this->learnedWhy[spl_object_hash($newRule)] = $why;
  291. $ruleNode = new RuleWatchNode($newRule);
  292. $ruleNode->watch2OnHighest($this->decisions);
  293. $this->watchGraph->insert($ruleNode);
  294. $this->decisions->decide($learnLiteral, $level, $newRule);
  295. }
  296. return $level;
  297. }
  298. /**
  299. * @param int $level
  300. * @param array $decisionQueue
  301. * @param bool $disableRules
  302. * @param Rule $rule
  303. * @return int
  304. */
  305. private function selectAndInstall($level, array $decisionQueue, $disableRules, Rule $rule)
  306. {
  307. // choose best package to install from decisionQueue
  308. $literals = $this->policy->selectPreferredPackages($this->pool, $this->installedMap, $decisionQueue, $rule->getRequiredPackage());
  309. $selectedLiteral = array_shift($literals);
  310. // if there are multiple candidates, then branch
  311. if (count($literals)) {
  312. $this->branches[] = array($literals, $level);
  313. }
  314. return $this->setPropagateLearn($level, $selectedLiteral, $disableRules, $rule);
  315. }
  316. /**
  317. * @param int $level
  318. * @param Rule $rule
  319. * @return array
  320. */
  321. protected function analyze($level, Rule $rule)
  322. {
  323. $analyzedRule = $rule;
  324. $ruleLevel = 1;
  325. $num = 0;
  326. $l1num = 0;
  327. $seen = array();
  328. $learnedLiterals = array(null);
  329. $decisionId = count($this->decisions);
  330. $this->learnedPool[] = array();
  331. while (true) {
  332. $this->learnedPool[count($this->learnedPool) - 1][] = $rule;
  333. foreach ($rule->literals as $literal) {
  334. // skip the one true literal
  335. if ($this->decisions->satisfy($literal)) {
  336. continue;
  337. }
  338. if (isset($seen[abs($literal)])) {
  339. continue;
  340. }
  341. $seen[abs($literal)] = true;
  342. $l = $this->decisions->decisionLevel($literal);
  343. if (1 === $l) {
  344. $l1num++;
  345. } elseif ($level === $l) {
  346. $num++;
  347. } else {
  348. // not level1 or conflict level, add to new rule
  349. $learnedLiterals[] = $literal;
  350. if ($l > $ruleLevel) {
  351. $ruleLevel = $l;
  352. }
  353. }
  354. }
  355. $l1retry = true;
  356. while ($l1retry) {
  357. $l1retry = false;
  358. if (!$num && !--$l1num) {
  359. // all level 1 literals done
  360. break 2;
  361. }
  362. while (true) {
  363. if ($decisionId <= 0) {
  364. throw new SolverBugException(
  365. "Reached invalid decision id $decisionId while looking through $rule for a literal present in the analyzed rule $analyzedRule."
  366. );
  367. }
  368. $decisionId--;
  369. $decision = $this->decisions->atOffset($decisionId);
  370. $literal = $decision[Decisions::DECISION_LITERAL];
  371. if (isset($seen[abs($literal)])) {
  372. break;
  373. }
  374. }
  375. unset($seen[abs($literal)]);
  376. if ($num && 0 === --$num) {
  377. $learnedLiterals[0] = -abs($literal);
  378. if (!$l1num) {
  379. break 2;
  380. }
  381. foreach ($learnedLiterals as $i => $learnedLiteral) {
  382. if ($i !== 0) {
  383. unset($seen[abs($learnedLiteral)]);
  384. }
  385. }
  386. // only level 1 marks left
  387. $l1num++;
  388. $l1retry = true;
  389. }
  390. }
  391. $decision = $this->decisions->atOffset($decisionId);
  392. $rule = $decision[Decisions::DECISION_REASON];
  393. }
  394. $why = count($this->learnedPool) - 1;
  395. if (!$learnedLiterals[0]) {
  396. throw new SolverBugException(
  397. "Did not find a learnable literal in analyzed rule $analyzedRule."
  398. );
  399. }
  400. $newRule = new Rule($learnedLiterals, Rule::RULE_LEARNED, $why);
  401. return array($learnedLiterals[0], $ruleLevel, $newRule, $why);
  402. }
  403. /**
  404. * @param Problem $problem
  405. * @param Rule $conflictRule
  406. */
  407. private function analyzeUnsolvableRule(Problem $problem, Rule $conflictRule)
  408. {
  409. $why = spl_object_hash($conflictRule);
  410. if ($conflictRule->getType() == RuleSet::TYPE_LEARNED) {
  411. $learnedWhy = $this->learnedWhy[$why];
  412. $problemRules = $this->learnedPool[$learnedWhy];
  413. foreach ($problemRules as $problemRule) {
  414. $this->analyzeUnsolvableRule($problem, $problemRule);
  415. }
  416. return;
  417. }
  418. if ($conflictRule->getType() == RuleSet::TYPE_PACKAGE) {
  419. // package rules cannot be part of a problem
  420. return;
  421. }
  422. $problem->nextSection();
  423. $problem->addRule($conflictRule);
  424. }
  425. /**
  426. * @param Rule $conflictRule
  427. * @param bool $disableRules
  428. * @return int
  429. */
  430. private function analyzeUnsolvable(Rule $conflictRule, $disableRules)
  431. {
  432. $problem = new Problem($this->pool);
  433. $problem->addRule($conflictRule);
  434. $this->analyzeUnsolvableRule($problem, $conflictRule);
  435. $this->problems[] = $problem;
  436. $seen = array();
  437. $literals = $conflictRule->literals;
  438. foreach ($literals as $literal) {
  439. // skip the one true literal
  440. if ($this->decisions->satisfy($literal)) {
  441. continue;
  442. }
  443. $seen[abs($literal)] = true;
  444. }
  445. foreach ($this->decisions as $decision) {
  446. $literal = $decision[Decisions::DECISION_LITERAL];
  447. // skip literals that are not in this rule
  448. if (!isset($seen[abs($literal)])) {
  449. continue;
  450. }
  451. $why = $decision[Decisions::DECISION_REASON];
  452. $problem->addRule($why);
  453. $this->analyzeUnsolvableRule($problem, $why);
  454. $literals = $why->literals;
  455. foreach ($literals as $literal) {
  456. // skip the one true literal
  457. if ($this->decisions->satisfy($literal)) {
  458. continue;
  459. }
  460. $seen[abs($literal)] = true;
  461. }
  462. }
  463. if ($disableRules) {
  464. foreach ($this->problems[count($this->problems) - 1] as $reason) {
  465. $this->disableProblem($reason['rule']);
  466. }
  467. $this->resetSolver();
  468. return 1;
  469. }
  470. return 0;
  471. }
  472. /**
  473. * @param Rule $why
  474. */
  475. private function disableProblem(Rule $why)
  476. {
  477. $job = $why->getJob();
  478. if (!$job) {
  479. $why->disable();
  480. return;
  481. }
  482. // disable all rules of this job
  483. foreach ($this->rules as $rule) {
  484. /** @var Rule $rule */
  485. if ($job === $rule->getJob()) {
  486. $rule->disable();
  487. }
  488. }
  489. }
  490. private function resetSolver()
  491. {
  492. $this->decisions->reset();
  493. $this->propagateIndex = 0;
  494. $this->branches = array();
  495. $this->enableDisableLearnedRules();
  496. $this->makeAssertionRuleDecisions();
  497. }
  498. /**
  499. * enable/disable learnt rules
  500. *
  501. * we have enabled or disabled some of our rules. We now re-enable all
  502. * of our learnt rules except the ones that were learnt from rules that
  503. * are now disabled.
  504. */
  505. private function enableDisableLearnedRules()
  506. {
  507. foreach ($this->rules->getIteratorFor(RuleSet::TYPE_LEARNED) as $rule) {
  508. $why = $this->learnedWhy[spl_object_hash($rule)];
  509. $problemRules = $this->learnedPool[$why];
  510. $foundDisabled = false;
  511. foreach ($problemRules as $problemRule) {
  512. if ($problemRule->isDisabled()) {
  513. $foundDisabled = true;
  514. break;
  515. }
  516. }
  517. if ($foundDisabled && $rule->isEnabled()) {
  518. $rule->disable();
  519. } elseif (!$foundDisabled && $rule->isDisabled()) {
  520. $rule->enable();
  521. }
  522. }
  523. }
  524. /**
  525. * @param bool $disableRules
  526. */
  527. private function runSat($disableRules = true)
  528. {
  529. $this->propagateIndex = 0;
  530. /*
  531. * here's the main loop:
  532. * 1) propagate new decisions (only needed once)
  533. * 2) fulfill jobs
  534. * 3) fulfill all unresolved rules
  535. * 4) minimalize solution if we had choices
  536. * if we encounter a problem, we rewind to a safe level and restart
  537. * with step 1
  538. */
  539. $decisionQueue = array();
  540. $decisionSupplementQueue = array();
  541. /**
  542. * @todo this makes $disableRules always false; determine the rationale and possibly remove dead code?
  543. */
  544. $disableRules = array();
  545. $level = 1;
  546. $systemLevel = $level + 1;
  547. $installedPos = 0;
  548. while (true) {
  549. if (1 === $level) {
  550. $conflictRule = $this->propagate($level);
  551. if (null !== $conflictRule) {
  552. if ($this->analyzeUnsolvable($conflictRule, $disableRules)) {
  553. continue;
  554. }
  555. return;
  556. }
  557. }
  558. // handle job rules
  559. if ($level < $systemLevel) {
  560. $iterator = $this->rules->getIteratorFor(RuleSet::TYPE_JOB);
  561. foreach ($iterator as $rule) {
  562. if ($rule->isEnabled()) {
  563. $decisionQueue = array();
  564. $noneSatisfied = true;
  565. foreach ($rule->literals as $literal) {
  566. if ($this->decisions->satisfy($literal)) {
  567. $noneSatisfied = false;
  568. break;
  569. }
  570. if ($literal > 0 && $this->decisions->undecided($literal)) {
  571. $decisionQueue[] = $literal;
  572. }
  573. }
  574. if ($noneSatisfied && count($decisionQueue)) {
  575. // prune all update packages until installed version
  576. // except for requested updates
  577. if (count($this->installed) != count($this->updateMap)) {
  578. $prunedQueue = array();
  579. foreach ($decisionQueue as $literal) {
  580. if (isset($this->installedMap[abs($literal)])) {
  581. $prunedQueue[] = $literal;
  582. if (isset($this->updateMap[abs($literal)])) {
  583. $prunedQueue = $decisionQueue;
  584. break;
  585. }
  586. }
  587. }
  588. $decisionQueue = $prunedQueue;
  589. }
  590. }
  591. if ($noneSatisfied && count($decisionQueue)) {
  592. $oLevel = $level;
  593. $level = $this->selectAndInstall($level, $decisionQueue, $disableRules, $rule);
  594. if (0 === $level) {
  595. return;
  596. }
  597. if ($level <= $oLevel) {
  598. break;
  599. }
  600. }
  601. }
  602. }
  603. $systemLevel = $level + 1;
  604. // jobs left
  605. $iterator->next();
  606. if ($iterator->valid()) {
  607. continue;
  608. }
  609. }
  610. if ($level < $systemLevel) {
  611. $systemLevel = $level;
  612. }
  613. for ($i = 0, $n = 0; $n < count($this->rules); $i++, $n++) {
  614. if ($i == count($this->rules)) {
  615. $i = 0;
  616. }
  617. $rule = $this->rules->ruleById[$i];
  618. $literals = $rule->literals;
  619. if ($rule->isDisabled()) {
  620. continue;
  621. }
  622. $decisionQueue = array();
  623. // make sure that
  624. // * all negative literals are installed
  625. // * no positive literal is installed
  626. // i.e. the rule is not fulfilled and we
  627. // just need to decide on the positive literals
  628. //
  629. foreach ($literals as $literal) {
  630. if ($literal <= 0) {
  631. if (!$this->decisions->decidedInstall(abs($literal))) {
  632. continue 2; // next rule
  633. }
  634. } else {
  635. if ($this->decisions->decidedInstall(abs($literal))) {
  636. continue 2; // next rule
  637. }
  638. if ($this->decisions->undecided(abs($literal))) {
  639. $decisionQueue[] = $literal;
  640. }
  641. }
  642. }
  643. // need to have at least 2 item to pick from
  644. if (count($decisionQueue) < 2) {
  645. continue;
  646. }
  647. $level = $this->selectAndInstall($level, $decisionQueue, $disableRules, $rule);
  648. if (0 === $level) {
  649. return;
  650. }
  651. // something changed, so look at all rules again
  652. $n = -1;
  653. }
  654. if ($level < $systemLevel) {
  655. continue;
  656. }
  657. // minimization step
  658. if (count($this->branches)) {
  659. $lastLiteral = null;
  660. $lastLevel = null;
  661. $lastBranchIndex = 0;
  662. $lastBranchOffset = 0;
  663. for ($i = count($this->branches) - 1; $i >= 0; $i--) {
  664. list($literals, $l) = $this->branches[$i];
  665. foreach ($literals as $offset => $literal) {
  666. if ($literal && $literal > 0 && $this->decisions->decisionLevel($literal) > $l + 1) {
  667. $lastLiteral = $literal;
  668. $lastBranchIndex = $i;
  669. $lastBranchOffset = $offset;
  670. $lastLevel = $l;
  671. }
  672. }
  673. }
  674. if ($lastLiteral) {
  675. unset($this->branches[$lastBranchIndex][self::BRANCH_LITERALS][$lastBranchOffset]);
  676. $level = $lastLevel;
  677. $this->revert($level);
  678. $why = $this->decisions->lastReason();
  679. $level = $this->setPropagateLearn($level, $lastLiteral, $disableRules, $why);
  680. if ($level == 0) {
  681. return;
  682. }
  683. continue;
  684. }
  685. }
  686. break;
  687. }
  688. }
  689. }