public void TestNegation() { var literalFormulas = new IFormula[] { new LiteralFormula(0), new LiteralFormula(1), new NegateFormula(new LiteralFormula(2)) }; var formula = new LogicalProductFormula(literalFormulas); var conf1 = 1L | (1L << 1) | (1L << 2); var conf2 = 1L | (1L << 1); var conf3 = 1L; var conf4 = 0; Assert.IsFalse(formula.GetValue(conf1)); Assert.IsTrue(formula.GetValue(conf2)); Assert.IsFalse(formula.GetValue(conf3)); Assert.IsFalse(formula.GetValue(conf4)); }
public Instance GetInstance(int variableCount) { var formula = new LogicalProductFormula(new List <IFormula>()); var productLenth = rand.Next(MIN_PRODUCT_LENGTH, MAX_PRODUCT_LENGTH + 1); for (var i = 0; i < productLenth; i++) { var sumFormula = new LogicalSumFormula(new List <IFormula>()); formula.Formulas.Add(sumFormula); var sumLength = rand.Next(MIN_SUM_LENGTH, MAX_SUM_LENGTH + 1); for (var j = 0; j < sumLength; j++) { var variableIndex = rand.Next(variableCount); var literalFormula = new LiteralFormula(variableIndex); if (rand.Next(2) == 0) { sumFormula.Formulas.Add(literalFormula); } else { sumFormula.Formulas.Add(new NegateFormula(literalFormula)); } } } var instance = new Instance { Formula = formula, Weights = Enumerable.Range(0, variableCount).Select(x => rand.Next(MIN_WEIGHT, MAX_WEIGHT + 1)).ToArray() }; return(instance); }
public void TestBrutteForce() { // n = 4 // F = (x0 + x2' + x3).(x0' + x1 + x2').(x2 + x3).(x0 + x1 + x2' + x3').(x1' + x2).(x2' + x3') // W = (2, 4, 1, 6) // expected solution: {1, 0, 0, 1}, weight: 8 var formula = new LogicalProductFormula(new IFormula[] { // (x0 + x2' + x3) new LogicalSumFormula(new IFormula[] { new LiteralFormula(0), new NegateFormula(new LiteralFormula(2)), new LiteralFormula(3) }), // (x0' + x1 + x2') new LogicalSumFormula(new IFormula[] { new NegateFormula(new LiteralFormula(0)), new LiteralFormula(1), new NegateFormula(new LiteralFormula(2)), }), // (x2 + x3) new LogicalSumFormula(new IFormula[] { new LiteralFormula(2), new LiteralFormula(3) }), // (x0 + x1 + x2' + x3') new LogicalSumFormula(new IFormula[] { new LiteralFormula(0), new LiteralFormula(1), new NegateFormula(new LiteralFormula(2)), new NegateFormula(new LiteralFormula(3)), }), // (x1' + x2) new LogicalSumFormula(new IFormula[] { new NegateFormula(new LiteralFormula(1)), new LiteralFormula(2) }), // (x2' + x3') new LogicalSumFormula(new IFormula[] { new NegateFormula(new LiteralFormula(2)), new NegateFormula(new LiteralFormula(3)) }) }); Console.WriteLine(formula); var instance = new Instance { Formula = formula, Weights = new [] { 2, 4, 1, 6 } }; var solver = new BrutteForceSolver(); var res = solver.Solve(instance); var expectedConfiguration = 1L | (1L << 3); Assert.AreEqual(expectedConfiguration, res.Configuration); Assert.AreEqual(8, res.Weight); }