/*======================================================================== Copyright (C) 2006 by Geir-Tore Lindsve, Torbjørn Meistad and Yngve Raudberget, hereby refered to as "the authors". All rights reserved Permission is hereby granted, without written agreement and without license or royalty fees, to use, reproduce, prepare derivative works, distribute, and display this software and its documentation for NONCOMMERCIAL RESEARCH AND EDUCATIONAL PURPOSES, provided that (1) the above copyright notice and the following two paragraphs appear in all copies of the source code and (2) redistributions, including without limitation binaries, reproduce these notices in the supporting documentation. IN NO EVENT SHALL THE AUTHORS, OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE AUTHORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. ========================================================================*/ using System; using System.Collections.Generic; using System.Text; using Casper; using Casper.Data; using Casper.Algorithm; using Iesi.Collections; namespace CasperTest { class CasperTest { static void Main(string[] args) { /* A simple example of using Casper: * Consider the following constraint problem: * variables x0, x1, x2 * Domain for all variables :{0,1} * Rules: * 1) x0 = x1; * 2) x1 != x2; */ //Instansiate Casper Csp casper = new Csp(); //Create domains 0 - 2 as a list of integers List domain0 = new List(); List domain1 = new List(); List domain2 = new List(); //Add values 0,1 to all domains domain0.Add(0); domain0.Add(1); domain1.Add(0); domain1.Add(1); domain2.Add(0); domain2.Add(1); //Create variable objects with variable ids 0,1,2 from //the domains. casper.AddCasperVarDom(new CasperVarDom(0, domain0)); casper.AddCasperVarDom(new CasperVarDom(1, domain1)); casper.AddCasperVarDom(new CasperVarDom(2, domain2)); //Make the rules as expression objects casper.Expressions = new CSPExpressions(); CSPExpr expr1 = new CSPExprBin(new CSPExprVarInt(0), new CSPExprVarInt(1),StaticData.Operators.eq); CSPExpr expr2 = new CSPExprBin(new CSPExprVarInt(0), new CSPExprVarInt(2), StaticData.Operators.gt); //Make a set of which variables are found in which rule Set variablesInRule1 = new HybridSet(); Set variablesInRule2 = new HybridSet(); variablesInRule1.Add(0); variablesInRule1.Add(1); variablesInRule2.Add(0); variablesInRule2.Add(2); //Creating a Constraint Graph is optional, but required if //one wishes to use the minimum width ordering of variables. ConstraintGraph constraintGraph = new ConstraintGraph(); //Add the variableInRule-sets into the constrintGraph as adjacency lists. constraintGraph.UpdateAdjacencyList(variablesInRule1); constraintGraph.UpdateAdjacencyList(variablesInRule2); //*****variable ordering:****** //1. Add the constraint graph to the casper object: casper.CGraph = constraintGraph; //2. Set the variable ordering, static(default) or minimum width: casper.SetVariableOrdering(Casper.CspVariableOrdering.vo_minwidth); //Make an expression-wrapper that also include the //set of variables that are found in the rule, in casper.Expressions.AddWrappedExpr(new CSPExprWrapper(expr1, variablesInRule1)); casper.Expressions.AddWrappedExpr(new CSPExprWrapper(expr2, variablesInRule2)); //Calculate the valid domains of the problem. //This runs the CSP search algorithm 'under the hood'. try { List validDoms = casper.ValidDomains(); //Print out of valid domain values: for (int i = 0; i < validDoms.Count; i++) { Console.Write("VarID: " + validDoms[i].VarID + ": "); for (int j = 0; j < validDoms[i].DomainValues.Count; j++) Console.Write(" " + validDoms[i].DomainValues[j] + " "); Console.WriteLine(""); } } catch (Exception e) { Console.WriteLine(e); } } } }