Skip to content

Commit f27fc9d

Browse files
committed
Implemented checkN and new check
1 parent 872558e commit f27fc9d

File tree

1 file changed

+69
-24
lines changed

1 file changed

+69
-24
lines changed

main.c

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ void letturamat(int **m, FILE *fin, int r, int c);
2929
void calculateOF(Sol *temp, Instances *in);
3030
int check(Sol *temp, Instances *in);
3131
int check1(Sol *temp, Instances *in);
32-
int check2(Sol *temp, Instances *in); //TODO
33-
int check3(Sol *temp, Instances *in); //TODO
32+
int check2(Sol *temp, Instances *in);
33+
int check3(Sol *temp, Instances *in);
3434
Sol solGen(Sol *temp, Instances *in); //TODO
3535

3636
int main(int argc, char* argv[])
@@ -178,36 +178,49 @@ void calculateOF(Sol *temp, Instances *in)
178178
temp->gain = (gain-cost);
179179
}
180180

181+
//int check(Sol *temp, Instances *in)
182+
//// Returns 1 if temp is feasible, 0 otherwise.
183+
//{
184+
// int c, q;
185+
// int con2=0;
186+
//
187+
// //Constraint (2)
188+
// for(q=0; q<in->Q; q++)
189+
// {
190+
// con2=0;
191+
// for(c=0; c<in->C; c++)
192+
// {
193+
// con2+=temp->Xcq[c][q];
194+
// }
195+
// if(con2>1)
196+
// {
197+
// return 0;
198+
// }
199+
// }
200+
//
201+
// //Constraint (3)
202+
// if(temp->mem > in->M)
203+
// {
204+
// return 0;
205+
// }
206+
// return 1;
207+
//}
208+
181209
int check(Sol *temp, Instances *in)
182210
// Returns 1 if temp is feasible, 0 otherwise.
183211
{
184-
int c, q;
185-
int con2=0;
186-
187-
//Constraint (2)
188-
for(q=0; q<in->Q; q++)
189-
{
190-
con2=0;
191-
for(c=0; c<in->C; c++)
192-
{
193-
con2+=temp->Xcq[c][q];
194-
}
195-
if(con2>1)
196-
{
197-
return 0;
198-
}
199-
}
200-
201-
//Constraint (3)
202-
if(temp->mem > in->M)
203-
{
212+
if (check1(temp,in) > 0 ||
213+
check2(temp,in) > 0 ||
214+
check3(temp,in) >0) {
215+
return 1;
216+
} else {
204217
return 0;
205218
}
206-
return 1;
207219
}
208220

209221
int check1(Sol *temp, Instances *in)
210222
// check constraint 1
223+
// returns the number of indexes that should be present and are not, scaled by a factor.
211224
{
212225
int c, q, i;
213226
int p1=0;
@@ -218,7 +231,7 @@ int check1(Sol *temp, Instances *in)
218231
if (temp->Xcq[c][q] == 1) {
219232
for (i=0; i<in->I; i++) {
220233
if (in->Eci[c][i] == 1 && temp->Zi == 0) {
221-
pi++;
234+
p1++;
222235
}
223236
}
224237
}
@@ -231,6 +244,38 @@ int check1(Sol *temp, Instances *in)
231244

232245
int check2(Sol *temp, Instances *in)
233246
// check constraint 2
247+
// returns 0 if every query has at most one configuration associated, otherwise it returns a value grater then 0, scaled by a factor.
234248
{
249+
int q, c;
250+
int p2, p2max=0;
251+
//TODO: aggiungere fattore di scalamento
235252

253+
for(q=0; q<in->Q; q++)
254+
{
255+
p2=0;
256+
for(c=0; c<in->C; c++)
257+
{
258+
p2+=temp->Xcq[c][q];
259+
}
260+
if(p2>p2max)
261+
{
262+
p2max=p2;
263+
}
264+
}
265+
266+
return p2max - 1;
267+
}
268+
269+
int check3(Sol *temp, Instances *in)
270+
// check constraint 3
271+
// returns 0 if limit M is not exceeded, otherwise it returns the excess amount, scaled by a factor.
272+
{
273+
//TODO: aggiungere fattore di scalamento
274+
275+
if(temp->mem > in->M)
276+
{
277+
return temp->mem - in->M;
278+
}
279+
280+
return 0;
236281
}

0 commit comments

Comments
 (0)