polylib: testlib.c Source File

- Main Page
- Classes
- Files

- File List
- File Members

# testlib.c

Go to the documentation of this file.

    00001 /*
    00002     This file is part of PolyLib.
    00003
    00004     PolyLib is free software: you can redistribute it and/or modify
    00005     it under the terms of the GNU General Public License as published by
    00006     the Free Software Foundation, either version 3 of the License, or
    00007     (at your option) any later version.
    00008
    00009     PolyLib is distributed in the hope that it will be useful,
    00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00012     GNU General Public License for more details.
    00013
    00014     You should have received a copy of the GNU General Public License
    00015     along with PolyLib.  If not, see <http://www.gnu.org/licenses/>.
    00016 */
    00017 /* main.c
    00018      COPYRIGHT
    00019           Both this software and its documentation are
    00020
    00021               Copyright 1993 by IRISA /Universite de Rennes I - France,
    00022               Copyright 1995,1996 by BYU
    00023                          all rights reserved.
    00024
    00025           Permission is granted to copy, use, and distribute
    00026           for any commercial or noncommercial purpose under the terms
    00027           of the GNU General Public license, version 2, June 1991
    00028           (see file : LICENSING).
    00029
    00030    This file along with polyhedron.c and vector.c do the following functions:
    00031     - Extraction of a minimal set of constraints from some set of constraints
    00032     - Intersection of two convexes
    00033     - Application of a linear function to some convex
    00034     - Verification that a convex is included in some other convex
    00035
    00036    They are compiled together into an executable file called "test".
    00037    The file test.in contains sample input data for the program.
    00038    The file test.out contains the output produced by the program.
    00039
    00040    This directory also contains a makefile to build and run the test.
    00041
    00042    This file is a tutorial on how to use the library.  The comments
    00043    explain whats going on.  You can use this file as a pattern for your
    00044    own program.  Good Luck !
    00045
    00046    --Doran
    00047 */
    00048
    00049 #include <stdio.h>
    00050
    00051 #include <polylib/polylib.h>
    00052
    00053
    00054 int main() {
    00055
    00056   Matrix *a, *b, *t;
    00057   Polyhedron *A, *B, *C, *D;
    00058
    00059   printf("Polyhedral Library Test\n\n");
    00060
    00061   /* read in a matrix containing your equations */
    00062   /* for example, run this program and type in these five  lines:
    00063      4 4
    00064      1 0 1 -1
    00065      1 -1 0 6
    00066      1 0 -1 7
    00067      1 1 0 -2
    00068      This is a matrix for the following inequalities
    00069      1 = inequality,  0x +  1y -1 >=0  -->      y >= 1
    00070      1 = inequality, -1x +  0y +6 >=0  -->      x <= 6
    00071      1 = inequality,  0x + -1y +7 >=0  -->      y <= 7
    00072      1 = inequality,  1x +  0y -2 >=0  -->      x >= 2
    00073      If the first number is a 0 instead of a 1, then that constraint
    00074      is an 'equality' instead of an 'inequality'.
    00075   */
    00076   a = Matrix_Read();
    00077
    00078   /* read in a second matrix containing a second set of constraints:
    00079      for example :
    00080      4 4
    00081      1 1 0 -1
    00082      1 -1 0 3
    00083      1 0 -1 5
    00084      1 0 1 -2
    00085   */
    00086   b = Matrix_Read();
    00087
    00088   /* Convert the constraints to a Polyhedron.
    00089      This operation 1. Computes the dual ray/vertice form of the
    00090      system, and 2. Eliminates redundant constraints and reduces
    00091      them to a minimal form.
    00092   */
    00093   A = Constraints2Polyhedron(a, 200);
    00094   B = Constraints2Polyhedron(b, 200);
    00095
    00096   /* the 200 is the size of the working space (in terms of number
    00097      of rays) that is allocated temporarily
    00098      -- you can enlarge or reduce it as needed */
    00099
    00100   /* There is likewise a rays to polyhedron procedure */
    00101
    00102   /* Since you are done with the matrices a and b, be a good citizen
    00103      and clean up your garbage */
    00104   Matrix_Free(a);
    00105   Matrix_Free(b);
    00106
    00107   /* If you want the the reduced set of equations back, you can
    00108      either learn to read the polyhedron structure (not hard,
    00109      look in "types.h"...
    00110      or you can get the matrix back in the same format it started
    00111      in... */
    00112   a = Polyhedron2Constraints(A);
    00113   b = Polyhedron2Constraints(B);
    00114
    00115   /* Take a look at them if you want */
    00116   printf("\na =");
    00117   Matrix_Print(stdout,P_VALUE_FMT,a);
    00118   printf("\nb =");
    00119   Matrix_Print(stdout,P_VALUE_FMT,b);
    00120
    00121   Matrix_Free(a);
    00122   Matrix_Free(b);
    00123
    00124   /* To intersect the two systems, use the polyhedron formats... */
    00125   C = DomainIntersection(A, B, 200);
    00126
    00127   /* Again, the 200 is the size of the working space */
    00128
    00129   /* This time, lets look a the polyhedron itself... */
    00130   printf("\nC = A and B =");
    00131   Polyhedron_Print(stdout,P_VALUE_FMT,C);
    00132
    00133   /*
    00134    * The operations DomainUnion, DomainDifference, and DomainConvex
    00135    * are also available
    00136    */
    00137
    00138   /*
    00139    * Read in a third matrix containing a transformation matrix,
    00140    * this one swaps the indices (x,y --> y,x):
    00141    * 3 3
    00142    * 0 1 0
    00143    * 1 0 0
    00144    * 0 0 1
    00145    */
    00146
    00147
    00148   t = Matrix_Read();
    00149
    00150   /* Take the preimage (transform the equations) of the domain C to
    00151      get D.  Are you catching on to this 200 thing yet ??? */
    00152
    00153   D = Polyhedron_Preimage(C, t, 200);
    00154
    00155   /* cleanup please */
    00156   Matrix_Free(t);
    00157
    00158   printf("\nD = transformed C =");
    00159   Polyhedron_Print(stdout,P_VALUE_FMT,D);
    00160   Domain_Free(D);
    00161
    00162   /* in a similar way, Polyhedron_Image(dom, mat, 200), takes the image
    00163      of dom under matrix mat  (transforms the vertices/rays) */
    00164
    00165   /* The function PolyhedronIncludes(Pol1, Pol2) returns 1 if Pol1
    00166      includes (covers) Pol2, and a 0 otherwise */
    00167
    00168   if (PolyhedronIncludes(A,C))
    00169     printf("\nWe expected A to cover C since C = A intersect B\n");
    00170   if (!PolyhedronIncludes(C,B))
    00171     printf("and C does not cover B...\n");
    00172
    00173   /* Final note:  some functions are defined for Domains, others
    00174    * for Polyhedrons.  A domain is simply a list of polyhedrons.
    00175    * Every polyhedron structure has a "next" pointer used to
    00176    * make a list of polyhedrons...  For instance, the union of
    00177    * two disjoint polyhedra is a domain consisting of two polyhedra.
    00178    * If you want the convex domain... you have to call
    00179    * DomainConvex(Pol1, 200) explicity.
    00180    * Note that includes does not work on domains, only on simple
    00181    * polyhedrons...
    00182    * Thats the end of the demo...  write me if you have questions.
    00183    * And remember to clean up...
    00184    */
    00185
    00186   Domain_Free(A);
    00187   Domain_Free(B);
    00188   Domain_Free(C);
    00189
    00190   return 0;
    00191 }
    00192
    00193