/* * Example session of using Cassowary.net * Author: Jo Vermeulen * * Be sure to link against Cassowary.dll, otherwise * this won't work! * * When using the Mono commandline compiler, add the * argument -r:Cassowary.dll like this: * $ mcs -r:Cassowary.dll CassowaryExample.cs * * When using Visual Studio, right click on your * project in the Solution view and choose * 'Add reference'. Then browse to the Cassowary.dll * library. */ using System; // we want to use the Cassowary library using Cassowary; public class CassowaryExample { public static void Main(string[] args) { try { // create the constraint solver ClSimplexSolver solver = new ClSimplexSolver(); // add a few variables: x and y ClVariable x = new ClVariable("x"); ClVariable y = new ClVariable("y"); // we want x to be greater than or equal to // twice times y // [x >= 2*y] solver.AddConstraint( new ClLinearInequality(x, Cl.GEQ, Cl.Times(2, y))); // we also like x to be equal to 20, but give it a weak strength // (meaning stronger constraints will have greater precedence) // [x = 20] // // remember: the default strength is required solver.AddConstraint( new ClLinearEquation(x, 20, ClStrength.Weak)); // let's enforce that x and y together are less than or equal to 50 // [x + y <= 50] // // unfortunately, we have to wrap a few things in ClLinearExpression // instances for now (will hopefully be improved in a future release) solver.AddConstraint( new ClLinearInequality(Cl.Plus(x, new ClLinearExpression(y)), Cl.LEQ, new ClLinearExpression(50))); // there is no need to explicitly call solver.Solve(): by default // the solver auto solves after each AddConstraint() call // now print out the solution Console.WriteLine("x == " + x.Value); // should be x == 20 Console.WriteLine("y == " + y.Value); // should be y == 10 } catch (ExClRequiredFailure rf) { Console.WriteLine("The constraint system could not be solved:"); Console.WriteLine(rf.StackTrace); } catch (ExClInternalError ie) { Console.WriteLine("There was an internal Cassowary error:"); Console.WriteLine(ie.StackTrace); } catch (ExClError e) { Console.WriteLine("Other error -- " + e.GetType() + ":"); Console.WriteLine(e.StackTrace); } } }