plSymbol)
of boolean type and
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Other wise stated,
is equal to one if: (i) we ask
for enforcing the constraints (
) and they are fulfilled
(
) or (ii) if we ask for not enforcing the
constraints (
) and they are not fulfilled (
). Otherwise, it is equal to cero.
and subject to the following constraints (see Figure 4.1)
![]() |
(4.3) | ||
![]() |
(4.4) |
Program 13 shows a solution to our problem.
1 /*============================================================================= 2 * File : constraint.cpp 3 *============================================================================= 4 * 5 *------------------------- Description --------------------------------------- 6 * This program shows how to impose a set of constraints to a set of variables 7 * (use of plIneqconstraint). 8 *----------------------------------------------------------------------------- 9 */ 10 11 12 #include <pl.h> 13 14 /********************************************************************** 15 Function that versifies the two imposed constraints 16 ***********************************************************************/ 17 18 void f_constraints(plValues &out, const plValues &in){ 19 plFloat x, y, c; 20 21 x = in[0]; 22 y = in[1]; 23 24 // First constraint y+4x-40 <= 0 25 c = y+4*x-40; 26 out[0] = c; 27 28 // Second constraint -9y + 8x -8 <= 0 29 c = -9*y + 8*x -8; 30 out[1] = c; 31 } 32 33 main() 34 { 35 /********************************************************************** 36 Defining the variable type, set and values 37 ***********************************************************************/ 38 int i; 39 plRealType Tx(0.0,10.0,30); 40 plRealType Ty(0.0,10.0,30); 41 plSymbol X("x",Tx); 42 plSymbol Y("y",Ty); 43 plSymbol C("c",PL_BINARY_TYPE); 44 45 /********************************************************************** 46 Construction of the distribution P(X Y) 47 ***********************************************************************/ 48 49 // Filling the parameters of the plNormal 50 float matrix[4] = {2.5, 1.45, 51 1.45, 3.67}; 52 53 plFloatMatrix Sigma(2,matrix); 54 plFloatVector mean(2); 55 mean[0] = 5.0; 56 mean[1] = 5.0; 57 58 // Construction of the normal kernel P(X Y) 59 plNormal Pxy(X^Y,mean,Sigma); 60 cout<<Pxy<<endl<<endl; 61 plKernel Pxy_compiled; 62 63 // Compile and plot P(X Y | C=true) 64 Pxy.compile(Pxy_compiled); 65 Pxy_compiled.plot("nd_normal.gnu",30); 66 67 /********************************************************************** 68 Construction of the inequality constraint P(C | X Y) 69 ***********************************************************************/ 70 plExternalFunction ext_func(X^Y, f_constraints); 71 plIneqConstraint Pc(C,ext_func,2); 72 73 /********************************************************************** 74 Construction of the joint distribution P(X Y C) = P(X Y) P(C | X Y) 75 ***********************************************************************/ 76 77 // Construction of a joint distribution 78 plJointDistribution jd(X^Y^C,Pxy^Pc); 79 cout<<"The distribution: "<<jd<<endl; 80 81 /********************************************************************** 82 Obtaining P(X Y | C = true) and P(X Y | C = false) 83 ***********************************************************************/ 84 85 // Geting P(X Y | C) 86 plCndKernel Pxy_knowing_c; 87 jd.ask(Pxy_knowing_c,X^Y,C); 88 cout<<"Question: "<<Pxy_knowing_c<<endl; 89 90 // Geting P(X Y | C=true) 91 plValues v(C); 92 v[C] = true; 93 plKernel Pxy_constrained; 94 Pxy_knowing_c.instantiate(Pxy_constrained,v); 95 cout<<"Question: "<<Pxy_constrained<<endl; 96 97 // Compile and plot P(X Y | C=true) 98 Pxy_constrained.compile(Pxy_compiled); 99 Pxy_compiled.plot("constrainted.gnu",30); 100 101 // Geting P(X Y | C=false) 102 v[C] = false; 103 plKernel Pxy_not_constrained; 104 Pxy_knowing_c.instantiate(Pxy_not_constrained,v); 105 cout<<"Question: "<<Pxy_not_constrained<<endl; 106 107 // Compile and plot P(X Y | C=false) 108 Pxy_not_constrained.compile(Pxy_compiled); 109 Pxy_compiled.plot("not_constrainted.gnu",30); 110 }
The main part on the code of Program 13 is the
construction of the inequality constraint given by the
following code n lines 71,72:
plExternalFunction ext_func(X^Y, f_constraints); plIneqConstraint Pc(C,ext_func,2);
First, an external function is constructed with a function
provided by the user. Then, the inequality
constraint is constructed. The last argumet of the
plIneqConstraint
constructor is 2, this is because there are two
constraints. The dimensionality of the output parameter out
of
the function f_constraints
(lines 18 to 31) is then equal to
this parameter. The two constraints are validated in the function
provided by the user
// First constraint y+4x+40 <= 0 c = y+4*x-40; out[0] = c; // Second constraint -9y + 8x -8 <= 0 c = -9*y + 8*x -8; out[1] = c;
It words to say that we can stop verifying the constraints after one
of them is bigger than zero. That is, the line out[0] = c;
could be written as if((out[0] = c) > 0) return;
. This is more
efficient in terms of execution time, mainly when we have a large
number of constraints to verify. Figure 4.1 shows the
resulting distributions of Program 13.