00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <polylib/polylib.h>
00028 #include <polylib/ranking.h>
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 Polyhedron *LexSmaller(Polyhedron *P, Polyhedron *D, unsigned dim,
00052 Polyhedron *C, unsigned MAXRAYS)
00053 {
00054 unsigned i, j, k, r;
00055 unsigned nb_parms = C->Dimension;
00056 unsigned nb_vars = dim;
00057 unsigned P_extra = P->Dimension - nb_vars - nb_parms;
00058 unsigned D_extra = D->Dimension - nb_vars - nb_parms;
00059 unsigned nb_new_parms;
00060 unsigned ncons;
00061 Matrix * cur_element, * C_times_J, * Klon;
00062 Polyhedron * P1, *C1;
00063 Polyhedron * lexico_lesser_union = NULL;
00064
00065 POL_ENSURE_INEQUALITIES(C);
00066 POL_ENSURE_INEQUALITIES(D);
00067 POL_ENSURE_INEQUALITIES(P);
00068
00069 assert(P->Dimension >= C->Dimension + dim);
00070 assert(D->Dimension >= C->Dimension + dim);
00071 nb_new_parms = nb_vars;
00072
00073
00074 if (nb_vars<=0) {
00075 printf("\nRanking > No variables, returning NULL.\n");
00076 return NULL;
00077 }
00078
00079
00080
00081
00082 if (D_extra)
00083 cur_element = Matrix_Alloc(P->NbConstraints+D->NbConstraints+nb_new_parms,
00084 P->Dimension+D_extra+nb_new_parms+2);
00085 else
00086 cur_element = Matrix_Alloc(P->NbConstraints+nb_new_parms,
00087 P->Dimension+D_extra+nb_new_parms+2);
00088
00089
00090
00091 for (i=0; i < P->NbConstraints; i++) {
00092 Vector_Copy(P->Constraint[i], cur_element->p[i], nb_vars+P_extra+1);
00093 Vector_Copy(P->Constraint[i]+1+nb_vars+P_extra,
00094 cur_element->p[i]+1+nb_vars+P_extra+D_extra+nb_new_parms,
00095 nb_parms+1);
00096 }
00097 ncons = P->NbConstraints;
00098 if (D_extra) {
00099 for (i=0; i < D->NbConstraints; i++) {
00100 r = P->NbConstraints + i;
00101 Vector_Copy(D->Constraint[i], cur_element->p[r], 1);
00102 Vector_Copy(D->Constraint[i]+1,
00103 cur_element->p[r]+1+nb_vars+P_extra+D_extra, nb_new_parms);
00104 Vector_Copy(D->Constraint[i]+1+nb_new_parms,
00105 cur_element->p[r]+1+nb_vars+P_extra, D_extra);
00106 Vector_Copy(D->Constraint[i]+1+nb_new_parms+D_extra,
00107 cur_element->p[r]+1+nb_vars+P_extra+D_extra+nb_new_parms,
00108 nb_parms+1);
00109 }
00110 ncons += D->NbConstraints;
00111 }
00112
00113
00114
00115 for (k=0, r = ncons; k < nb_vars; k++, r++) {
00116
00117
00118
00119
00120 cur_element->NbRows = r+1;
00121
00122
00123 if (k>=1) {
00124 value_set_si(cur_element->p[r-1][0], 0);
00125 value_set_si(cur_element->p[r-1][cur_element->NbColumns-1], 0);
00126 }
00127
00128 value_set_si(cur_element->p[r][0], 1);
00129 value_set_si(cur_element->p[r][k+1], -1);
00130 value_set_si(cur_element->p[r][nb_vars+P_extra+D_extra+k+1], 1);
00131
00132 value_set_si(cur_element->p[r][cur_element->NbColumns-1], -1);
00133 #ifdef ERDEBUG
00134 show_matrix(cur_element);
00135 #endif
00136
00137
00138
00139 Klon = Matrix_Copy(cur_element);
00140 P1 = Constraints2Polyhedron(Klon, MAXRAYS);
00141 Matrix_Free(Klon);
00142 P1->next = lexico_lesser_union;
00143 lexico_lesser_union = P1;
00144 }
00145
00146
00147
00148
00149 if (D_extra)
00150 C_times_J = Matrix_Alloc(C->NbConstraints, nb_new_parms+nb_parms+2);
00151 else
00152 C_times_J = Matrix_Alloc(C->NbConstraints + D->NbConstraints, D->Dimension+2);
00153
00154 for (i = 0; i < C->NbConstraints; i++) {
00155 value_assign(C_times_J->p[i][0], C->Constraint[i][0]);
00156 Vector_Copy(C->Constraint[i]+1, C_times_J->p[i]+1+nb_new_parms, nb_parms+1);
00157 }
00158
00159
00160 if (!D_extra)
00161 for (i = 0; i < D->NbConstraints; i++)
00162 Vector_Copy(D->Constraint[i], C_times_J->p[C->NbConstraints+i],
00163 D->Dimension+2);
00164
00165 #ifdef ERDEBUG
00166 show_matrix(C_times_J);
00167 #endif
00168 C1 = Constraints2Polyhedron(C_times_J, POL_NO_DUAL);
00169
00170
00171 Matrix_Free(cur_element);
00172 Matrix_Free(C_times_J);
00173
00174 C1->next = P1;
00175
00176 return C1;
00177 }
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 Enumeration *Polyhedron_LexSmallerEnumerate(Polyhedron *P, Polyhedron *D,
00193 unsigned dim,
00194 Polyhedron *C, unsigned MAXRAYS)
00195 {
00196 Enumeration * ranking;
00197 Polyhedron *RC, *RD;
00198
00199 RC = LexSmaller(P, D, dim, C, MAXRAYS);
00200 RD = RC->next;
00201 RC->next = NULL;
00202
00203
00204
00205
00206
00207 ranking = Domain_Enumerate(RD, RC, MAXRAYS, NULL);
00208
00209 Domain_Free(RD);
00210 Polyhedron_Free(RC);
00211
00212 return ranking;
00213 }
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223 Enumeration *Polyhedron_Ranking(Polyhedron *P, Polyhedron *C, unsigned MAXRAYS)
00224 {
00225 return Polyhedron_LexSmallerEnumerate(P, P, P->Dimension-C->Dimension,
00226 C, MAXRAYS);
00227 }