# Constraint for a practical problem

**URL:** <https://verificationacademy.com/forums/t/constraint-for-a-practical-problem/38727>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, systemverilog-constraint, systemverilog-enum-classes-constraints\
**Created:** [February 17, 2021, 4:58am UTC](https://verificationacademy.com/forums/t/constraint-for-a-practical-problem/38727 "2021-02-17T04:58:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bachan21](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/bachan21/32/1208_2.png) [@bachan21](https://verificationacademy.com/forums/u/bachan21)\
**Post date:** [February 17, 2021, 4:58am UTC](https://verificationacademy.com/forums/t/constraint-for-a-practical-problem/38727/1 "2021-02-17T04:58:37Z")

</div>

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

```verilog

	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

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [February 17, 2021, 7:59am UTC](https://verificationacademy.com/forums/t/constraint-for-a-practical-problem/38727/2 "2021-02-17T07:59:09Z")

</div>

_In reply to [bachan21](https://verificationacademy.com/forums/t/constraint-for-a-practical-problem/38727):_

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

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

```

---

<div class="post-metadata">

**Author:** ![bachan21](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/bachan21/32/1208_2.png) [@bachan21](https://verificationacademy.com/forums/u/bachan21)\
**Post date:** [February 17, 2021, 9:32am UTC](https://verificationacademy.com/forums/t/constraint-for-a-practical-problem/38727/3 "2021-02-17T09:32:58Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/constraint-for-a-practical-problem/38727/2):_

Thanks Dave,  
I got the results.

I quote the code here with comments

```verilog
    //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

---

<div class="post-metadata">

**Author:** ![aklikhar23](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@aklikhar23](https://verificationacademy.com/forums/u/aklikhar23)\
**Post date:** [February 21, 2021, 10:13am UTC](https://verificationacademy.com/forums/t/constraint-for-a-practical-problem/38727/4 "2021-02-21T10:13:18Z")

</div>

_In reply to [bachan21](https://verificationacademy.com/forums/t/constraint-for-a-practical-problem/38727/3):_

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
