public void EnemyHealthPack5() { const int numRooms = 3; ConstraintThingySolver solver = new ConstraintThingySolver(); FiniteDomain <String> finiteDomain = new FiniteDomain <string>("enemy", "health-pack"); FiniteDomainVariable <String>[] roomTypes = new FiniteDomainVariable <string> [numRooms]; for (int i = 0; i < numRooms; i++) { roomTypes[i] = solver.CreateFiniteDomainVariable(finiteDomain, "enemy", "health-pack"); } RealVariable[] roomScores = new RealVariable[numRooms]; var scoreMapping = new ScoreMapping <String>(finiteDomain, new Interval(-3, -3), new Interval(3, 3)); for (int i = 0; i < numRooms; i++) { roomScores[i] = Constraint.ScoreVariable(roomTypes[i], scoreMapping); } var sum = Constraint.Add(roomScores); Constraint.InRange(sum, 0, 4); foreach (var solution in solver.Solutions.FirstElements(1)) { AssertIntersect(new Interval(0, 4), sum.UniqueValue); } }
protected override void InitializeConstraintSystem(ConstraintThingySolver solver) { const int n = 8; // i stole this formulation of N-queens from the Choco documentation IntegerVariable[] queens = new IntegerVariable[n]; for (int i = 0; i < n; i++) { queens[i] = new IntegerVariable(solver, null, new IntegerInterval(1, n)); } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int k = j - i; IntegerVariable kVar = new IntegerVariable(solver, null, new IntegerInterval(k, k)); Constraint.NotEqual(queens[i], queens[j]); Constraint.NotEqual(queens[i], Constraint.Add(queens[j], kVar)); Constraint.NotEqual(queens[i], Constraint.Subtract(queens[j], kVar)); } } }
public void PopulationWithOccurences() { const int numRooms = 5; ConstraintThingySolver solver = new ConstraintThingySolver(); // this the finite domain variables to be expanded first -- we test without this below solver.ExpansionOrder = ExpansionOrder.Deterministic; FiniteDomain <String> finiteDomain = new FiniteDomain <string>("enemy", "health-pack", "ammo", "boss", "puzzle"); FiniteDomainVariable <String>[] roomTypes = new FiniteDomainVariable <string> [numRooms]; for (int i = 0; i < numRooms; i++) { roomTypes[i] = solver.CreateFiniteDomainVariable(finiteDomain, "enemy", "health-pack", "ammo", "boss", "puzzle"); } // exactly 1 boss in the level Constraint.RequireOccurences("boss", 1, roomTypes); // at most 1 puzzle in the level Constraint.MaximumOccurences("puzzle", 1, roomTypes); RealVariable[] roomScores = new RealVariable[numRooms]; var scoreMapping = new ScoreMapping <String>(finiteDomain, "enemy".PairedWith(new Interval(-3, -3)), "health-pack".PairedWith(new Interval(2, 2)), "ammo".PairedWith(new Interval(2, 2)), "boss".PairedWith(new Interval(-6, -6)), "puzzle".PairedWith(new Interval(0, 0))); for (int i = 0; i < numRooms; i++) { roomScores[i] = Constraint.ScoreVariable(roomTypes[i], scoreMapping); } var sum = roomScores.Aggregate((a, b) => { var result = Constraint.Add(a, b); Constraint.GreaterThanOrEqual(result, 0); return(result); }); Constraint.InRange(sum, 0, 10); foreach (var solution in solver.Solutions.FirstElements(10)) { AssertIntersect(new Interval(0, 10), sum.UniqueValue); } }
private static void PlayabilityExperiment(int numRooms, int randomSeed) { ConstraintThingySolver solver = new ConstraintThingySolver(randomSeed); FiniteDomain <String> finiteDomain = new FiniteDomain <string>("enemy", "health-pack", "ammo", "boss", "puzzle"); FiniteDomainVariable <String>[] roomTypes = new FiniteDomainVariable <string> [numRooms]; for (int i = 0; i < numRooms; i++) { roomTypes[i] = solver.CreateFiniteDomainVariable(finiteDomain, "enemy", "health-pack", "ammo", "boss", "puzzle"); } // exactly 1 boss in the level Constraint.RequireOccurences("boss", 1, roomTypes); // exactly 1 puzzle in the level Constraint.RequireOccurences("puzzle", 1, roomTypes); RealVariable[] roomScores = new RealVariable[numRooms]; var scoreMapping = new ScoreMapping <String>(finiteDomain, "enemy".PairedWith(new Interval(-3, -3)), "health-pack".PairedWith(new Interval(2, 2)), "ammo".PairedWith(new Interval(2, 2)), "boss".PairedWith(new Interval(-6, -6)), "puzzle".PairedWith(new Interval(0, 0))); for (int i = 0; i < numRooms; i++) { roomScores[i] = Constraint.ScoreVariable(roomTypes[i], scoreMapping); } var sum = roomScores.Aggregate((a, b) => { var result = Constraint.Add(a, b); Constraint.GreaterThanOrEqual(result, 0); return(result); }); Constraint.InRange(sum, 0, 10); foreach (var solution in solver.Solutions.FirstElements(10)) { AssertIntersect(new Interval(0, 10), sum.UniqueValue); Assert.AreEqual(1, roomTypes.Count(room => room.UniqueValue == "puzzle")); Assert.AreEqual(1, roomTypes.Count(room => room.UniqueValue == "boss")); } }
public static void Main(string[] args) { using (Model M = new Model()) { double[] c = new double[] { 1.5, 2.5, 3.0 }; double[,] A = new double[, ] { { 2, 4, 3 }, { 3, 2, 3 }, { 2, 3, 2 } }; double[] b = new double[] { 100000.0, 50000.0, 60000.0 }; int numvar = c.Length; int numcon = b.Length; // Create a model and input data Variable x = M.Variable(numvar, Domain.GreaterThan(0.0)); Constraint con = M.Constraint(Expr.Mul(A, x), Domain.LessThan(b)); M.Objective(ObjectiveSense.Maximize, Expr.Dot(c, x)); // Solve the problem M.Solve(); printsol(x.Level()); /************** Change an element of the A matrix ****************/ con.Index(0).Add(x.Index(0)); M.Solve(); printsol(x.Level()); /*************** Add a new variable ******************************/ // Create a variable and a compound view of all variables Variable x3 = M.Variable(Domain.GreaterThan(0.0)); Variable xNew = Var.Vstack(x, x3); // Add to the exising constraint con.Add(Expr.Mul(x3, new double[] { 4, 0, 1 })); // Change the objective to include x3 M.Objective(ObjectiveSense.Maximize, Expr.Dot(new double[] { 1.5, 2.5, 3.0, 1.0 }, xNew)); M.Solve(); printsol(xNew.Level()); /**************** Add a new constraint *****************************/ M.Constraint(Expr.Dot(xNew, new double[] { 1, 2, 1, 1 }), Domain.LessThan(30000.0)); M.Solve(); printsol(xNew.Level()); } }
protected override void InitializeConstraintSystem(ConstraintThingySolver solver) { const int numRooms = 5; FiniteDomain <string> finiteDomain = new FiniteDomain <string>("enemy", "health-pack", "ammo", "boss", "puzzle"); FiniteDomainVariable <string>[] roomTypes = new FiniteDomainVariable <string> [numRooms]; for (int i = 0; i < numRooms; i++) { roomTypes[i] = solver.CreateFiniteDomainVariable(finiteDomain, "enemy", "health-pack", "ammo", "boss", "puzzle"); } // exactly 1 boss in the level Constraint.RequireOccurences("boss", 1, roomTypes); // exactly 1 puzzle in the level Constraint.RequireOccurences("puzzle", 1, roomTypes); RealVariable[] roomScores = new RealVariable[numRooms]; var scoreMapping = new ScoreMapping <string>(finiteDomain, "enemy".PairedWith(new Interval(-3, -3)), "health-pack".PairedWith(new Interval(2, 2)), "ammo".PairedWith(new Interval(2, 2)), "boss".PairedWith(new Interval(-6, -6)), "puzzle".PairedWith(new Interval(0, 0))); for (int i = 0; i < numRooms; i++) { roomScores[i] = Constraint.ScoreVariable(roomTypes[i], scoreMapping); } var sum = roomScores.Aggregate((a, b) => { var result = Constraint.Add((RealVariable)a, b); Constraint.GreaterThanOrEqual(result, 0); return(result); }); Constraint.InRange(sum, 0, 10); }
public void ImpossiblePlayability() { const int numRooms = 5; ConstraintThingySolver solver = new ConstraintThingySolver(); FiniteDomain <String> finiteDomain = new FiniteDomain <string>("enemy", "health-pack", "ammo", "boss", "puzzle"); FiniteDomainVariable <String>[] roomTypes = new FiniteDomainVariable <string> [numRooms]; for (int i = 0; i < numRooms; i++) { roomTypes[i] = solver.CreateFiniteDomainVariable(finiteDomain, "enemy", "health-pack", "ammo", "boss", "puzzle"); } // exactly 1 boss in the level Constraint.RequireOccurences("boss", 1, roomTypes); // at most 1 puzzle in the level Constraint.MaximumOccurences("puzzle", 1, roomTypes); RealVariable[] roomScores = new RealVariable[numRooms]; var scoreMapping = new ScoreMapping <String>(finiteDomain, "enemy".PairedWith(new Interval(-3, -3)), "health-pack".PairedWith(new Interval(2, 2)), "ammo".PairedWith(new Interval(2, 2)), // make placing a boss super expensive so there's no valid solution, yet still constrained to appear "boss".PairedWith(new Interval(-600, -600)), "puzzle".PairedWith(new Interval(0, 0))); for (int i = 0; i < numRooms; i++) { roomScores[i] = Constraint.ScoreVariable(roomTypes[i], scoreMapping); } var sum = Constraint.Add(roomScores); Constraint.InRange(sum, 0, 10); Assert.AreEqual(0, solver.Solutions.Count()); }
public void EightQueens() { const int n = 8; ConstraintThingySolver solver = new ConstraintThingySolver(); // i stole this formulation of N-queens from the Choco documentation IntegerVariable[] queens = new IntegerVariable[n]; for (int i = 0; i < n; i++) { queens[i] = new IntegerVariable(solver, null, new IntegerInterval(1, n)); } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int k = j - i; IntegerVariable kVar = new IntegerVariable(solver, null, new IntegerInterval(k, k)); Constraint.NotEqual(queens[i], queens[j]); Constraint.NotEqual(queens[i], Constraint.Add(queens[j], kVar)); Constraint.NotEqual(queens[i], Constraint.Subtract(queens[j], kVar)); } } int answerCounter = 0; foreach (var solution in solver.Solutions) { answerCounter++; } Assert.AreEqual(92, answerCounter); }