Constraint for a practical problem

I am writing constraints for the following problem

There are seven friends A,B,C,D,E,F and G in a seven-floor building. The ground floor is no. 1, the floor above it is no.2 and so on.
E does not live on an even numbered floor. G does not live on the topmost floor. Only one person lives between E and G. A does not live on even numbered floor and does not live below F.D does not live immediately above or immediately below G. There two floors between D and E. Both B and C live on even-numbered floor. G and C live in between the two floors. No one shares same floor. F lives on floor number 5.

This is the constraints I have written for the the problem


	constraint value {a>0; a<8;
					  b>0; b<8;
					  c>0; c<8;
					  d>0; d<8;
					  e>0; e<8;
					  f>0; f<8;
					  g>0; g<8;
					  }
					  
	constraint A { a%2 !=0; a > f; }
	constraint B { b%2 == 0;}
	constraint C { c%2 ==0;}
	constraint D { soft d != g+1; soft d != g-1;}
	constraint E { e%2 !=0;}
	constraint F { f == 5;}
	constraint G { g != 7;}
	constraint E_G { soft e == g+1;  soft e == g-1;}
	constraint D_E { soft e == d+2;  soft  e == d-2;}

When I ran the simulation and checked the output manually its wrong.
I quote my output here.

A = 7

B = 6

C = 2

D = 3

E = 1

F = 5

G = 2

In reply to bachan21:

It would help to put the constraints in the same order of your description.

I think there is some confusion with your between constraints. If “only one person lives between E and G”, do you mean to say “there is one person living in between E and G”? Then the difference between E and G’s floors should be 2, not 1. There is no constraint for “G and C live in between the two floors” and I don’t know which two floors.

Also, do not use soft constraints here. Any mistakes and they may get silently discarded. Just use logical OR/AND

constraint D { d != g+1 && d != g-1;}

In reply to dave_59:

Thanks Dave,
I got the results.

I quote the code here with comments

    //Constraining values from 1 to7
    constraint value {a>0; a<8;
    b>0; b<8;
    c>0; c<8;
    d>0; d<8;
    e>0; e<8;
    f>0; f<8;
    g>0; g<8;
    }
    
    //No one shares same floor
    constraint uinque_value { unique {a,b,c,d,e,f,g};}

    //E does not live on an even numbered floor  
    constraint E { e%2 !=0;}
    //G does not live on the top most floor
    constraint G { g != 7;}
    //Only one person lives between E and G
    constraint E_G { e-g == 2 || g-e == 2;}
    //A does not live on even numbered floor and does not live below F
    constraint A { a%2 !=0; a > f; }
    //D does not live immediately above or immediately below G
    constraint D_G { d-g != 1 || g-d != 1;}
    // There two floors between D and E
    constraint D_E { e-d == 3 || d-e == 3;}
    // Both B and C live on even-numbered floor.
    constraint B { b%2 == 0;}
    constraint C { c%2 ==0;}
    // F lives on floor number 5
    constraint F { f == 5;}
    //G and C live in between the two floors.
    constraint G_C { g-c == 1 ||  c-g == 1;}

I still don’t get the statement “G and C live in between the two floors”
P.S. That was mentioned in the problem statement.

Apart from that everything is working perfectly

In reply to bachan21:

I wrote the constraints as follows and it looks like they are resolved -
rand bit [2:0] A, B, C, D, E, F, G;

constraint A_constraint{ A%2 != 0; A > F;}
constraint B_constraint {B%2 == 0;}
constraint C_constraint {C%2 == 0;}
constraint C_G { if (C > G) C <= G + 2; else C >= G - 2; } // |G - C| <= 2
constraint D_G { if(D > G) D != G + 1; else D != G - 1; }
constraint E_constraint {E%2 !=0;}
constraint D_E { if(E > D) E == D + 3; else E == D - 3; }
constraint E_G { if(E > G) E == G + 2; else E == G - 2; }
constraint F_constraint { F == 5;}
constraint G_constraint { G != 7;}
constraint no_same_floor {
unique{A, B, C, D, E, F, G};
A > 0; B > 0; C > 0; D > 0; E > 0; F > 0; G > 0;}

Got the following result-> A:7, B:4, C:2, D:6, E:3, F:5, G:1