From 57cbf23c4ef235f9c3484450afaa8381560445d6 Mon Sep 17 00:00:00 2001 From: Roberto Guido Date: Sun, 29 Nov 2020 19:54:31 +0100 Subject: [PATCH 01/14] first commit --- include/kitty/threshold_identification.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/kitty/threshold_identification.hpp b/include/kitty/threshold_identification.hpp index 02533d8e..056f2d71 100755 --- a/include/kitty/threshold_identification.hpp +++ b/include/kitty/threshold_identification.hpp @@ -60,7 +60,7 @@ bool is_threshold( const TT& tt, std::vector* plf = nullptr ) { std::vector linear_form; - /* TODO */ + /* TODO... */ /* if tt is non-TF: */ return false; From 1f9daf126a61e16991a5a9c065bbd251f1c6d456 Mon Sep 17 00:00:00 2001 From: Roberto Guido Date: Fri, 4 Dec 2020 16:59:53 +0100 Subject: [PATCH 02/14] test1 --- include/kitty/threshold_identification.hpp | 174 ++++++++++++++++++++- 1 file changed, 169 insertions(+), 5 deletions(-) diff --git a/include/kitty/threshold_identification.hpp b/include/kitty/threshold_identification.hpp index 056f2d71..a9c172be 100755 --- a/include/kitty/threshold_identification.hpp +++ b/include/kitty/threshold_identification.hpp @@ -33,8 +33,11 @@ #pragma once #include -// #include /* uncomment this line to include lp_solve */ +#include "lpsolve/lp_lib.h" /* uncomment this line to include lp_solve */ #include "traits.hpp" +#include "operations.hpp" +#include "cube.hpp" +#include "algorithm.hpp" namespace kitty { @@ -55,22 +58,183 @@ namespace kitty in the end. \return `true` if `tt` is a TF; `false` if `tt` is a non-TF. */ +enum Constraint_Type { GE, LE, EQ }; //GE=1 /* >= <= == */ +bool is_positive_unate_in_x( const TT& tt, const uint8_t x ) //check if the function is positive unate in the variable x +{ + auto numvars = tt.num_vars(); + auto const tt1 = cofactor0( tt, x ); + auto const tt2 = cofactor1( tt, x ); + for ( auto bit = 0; bit < ( 2 << ( numvars - 1 ) ); bit++ ) + { + if ( get_bit( tt1, bit ) <= get_bit( tt2, bit ) ) + { + continue; + } + else + { + return false; + } + } + return true; +} +bool is_negative_unate_in_x( const TT& tt, const uint8_t x ) //check if the function is negative unate in the variable x +{ + auto numvars = tt.num_vars(); + auto const tt1 = cofactor0( tt, x ); + auto const tt2 = cofactor1( tt, x ); + for ( auto bit = 0; bit < ( 2 << ( numvars - 1 ) ); bit++ ) + { + if ( get_bit( tt1, bit ) >= get_bit( tt2, bit ) ) + { + continue; + } + else + { + return false; + } + } + return true; +} +bool is_binate( const TT& tt ) //if the function is binate in any variable returns true +{ + auto numvars = tt.num_vars(); + auto count = 0; // count the number of variables with respect to which the function is binate + bool unate; + for ( auto i = 0u; i < numvars; i++ ) + { + unate = true; + auto const tt1 = cofactor0( tt, i ); + auto const tt2 = cofactor1( tt, i ); + for ( auto bit = 0; bit < ( 2 << ( numvars - 1 ) ); bit++ ) + { + if ( get_bit( tt1, bit ) >= get_bit( tt2, bit ) ) //negative unateness check + { + continue; + } + else + { + unate = false; + } + } + if(unate == false){ + unate =true; + for ( auto bit = 0; bit < ( 2 << ( numvars - 1 ) ); bit++ ) + { + if ( get_bit( tt1, bit ) <= get_bit( tt2, bit ) ) //positive unateness check + { + continue; + } + else + { + unate = false; + } + } + if (unate == false) count++; + } + if( count == numvars) //if it is binate with respect to all functions + return true; + else + return false; +} +void convert_to_binary(int64_t num,std::vector bin_num, uint32_t _num_vars) +{ + uint32_t i=0; + while(num!=0){ + bin_num.emplace_back(num%2); + num=num/2; + i++; + } + while(i!= num_vars){ + i++; + bin_num.emplace_back(0); + } + std::reverse(bin_num.begin(),bin_num.end()) + return; +} template::value>> bool is_threshold( const TT& tt, std::vector* plf = nullptr ) { std::vector linear_form; - - /* TODO... */ + /* TODO */ + std::vector ONSET, OFFSET; /* if tt is non-TF: */ - return false; - + if(is_binate) {// a function is binate in any variable, it is surely non-TF + return false; + } + /*otherwise tt could be TF*/ + for ( auto bit = tt.numvars - 1; bit > 0; bit-- ) + { + if(get_bit( tt, bit ) == 1){ //then add it to the ONSET + ONSET.emplace_back(bit); + } + else{ //else add it to the OFFSET + OFFSET.emplace_back(bit); + } + } + //NOT CORRECT + /* for ( auto i = 0u; i < tt.num_vars(); i++ ) + { + if(is_negative_unate_in_x(tt,i)){ + //change x_i with bar(x_i) <=> exchanging ONSET with the OFFSET + ONSET(i)=swap(OFFSET(i)); + } + }*/ + lprec *plp; + REAL row[tt.num_vars()+2]; + plp=make_lp(0,num_vars()+1); + if(plp == NULL){ + return false; //couldn't construct a new model + } + set_add_rowmode(lp,TRUE); + std::vector binary; + /*ONSET CONSTRAINTS*/ + for(auto i = 1u; i <= ONSET.size(); ++i){ + convert_to_binary(ONSET(i),binary,tt.num_vars()); + for ( auto k = 0u; k < tt.num_vars(); k++ ){ + if(is_negative_unate_in_x(tt,k)){ + if(binary(k) == 0) binary(k)=1; + else binary(k)=0; + } + } + for(auto j = 1u; j <= binary.size(); ++j){ + row[j]= REAL(binary(j)); + } + row[j]=-1.0; + add_constraint(plp,row,GE,0); + } + /*OFFSET CONSTRAINTS*/ + for(auto i = 1u; i <= ONSET.size(); ++i){ + convert_to_binary(ONSET(i),binary,tt.num_vars()); + for ( auto k = 0u; k < tt.num_vars(); k++ ){ + if(is_negative_unate_in_x(tt,k)){ + if(binary(k) == 0) binary(k)=1; + else binary(k)=0; + } + } + for(auto j = 1u; j <= binary.size(); ++j){ + row[j]= REAL(binary(j)); + } + row[j]=-1.0; + add_constraint(plp,row,LE,-1.0); + } + set_add_rowmode(plp, FALSE); + /*SET OBJECTIVE FUNCTION*/ + for(auto i = 1u; i <= (ONSET.size()+1); ++i){ + row[i]=1.0; + } + set_obj_fn(lp, row); + /*SOLVE LP*/ + set_minim(lp); + print_lp(plp); /* if tt is TF: */ /* push the weight and threshold values into `linear_form` */ if ( plf ) { *plf = linear_form; } + delete_lp(plp); return true; + } } } /* namespace kitty */ From a62d1eb10de67c715c65d5949c20f2a9634c753a Mon Sep 17 00:00:00 2001 From: Roberto Guido Date: Fri, 4 Dec 2020 17:06:57 +0100 Subject: [PATCH 03/14] test2 --- include/kitty/threshold_identification.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/kitty/threshold_identification.hpp b/include/kitty/threshold_identification.hpp index a9c172be..7fccf428 100755 --- a/include/kitty/threshold_identification.hpp +++ b/include/kitty/threshold_identification.hpp @@ -58,7 +58,8 @@ namespace kitty in the end. \return `true` if `tt` is a TF; `false` if `tt` is a non-TF. */ -enum Constraint_Type { GE, LE, EQ }; //GE=1 /* >= <= == */ +enum Constraint_Type { GE=1, LE, EQ }; //GE=1 /* >= <= == */ +template::value>> bool is_positive_unate_in_x( const TT& tt, const uint8_t x ) //check if the function is positive unate in the variable x { auto numvars = tt.num_vars(); @@ -151,7 +152,6 @@ void convert_to_binary(int64_t num,std::vector bin_num, uint32_t _num_vars std::reverse(bin_num.begin(),bin_num.end()) return; } -template::value>> bool is_threshold( const TT& tt, std::vector* plf = nullptr ) { std::vector linear_form; From 20004d034c9319c68c8e5eb3c15abeea4eda0683 Mon Sep 17 00:00:00 2001 From: Roberto Guido Date: Fri, 4 Dec 2020 17:13:06 +0100 Subject: [PATCH 04/14] test1 --- include/kitty/threshold_identification.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/kitty/threshold_identification.hpp b/include/kitty/threshold_identification.hpp index 7fccf428..ccb2a8f6 100755 --- a/include/kitty/threshold_identification.hpp +++ b/include/kitty/threshold_identification.hpp @@ -78,6 +78,7 @@ bool is_positive_unate_in_x( const TT& tt, const uint8_t x ) //check if the func } return true; } +template::value>> bool is_negative_unate_in_x( const TT& tt, const uint8_t x ) //check if the function is negative unate in the variable x { auto numvars = tt.num_vars(); @@ -96,6 +97,7 @@ bool is_negative_unate_in_x( const TT& tt, const uint8_t x ) //check if the func } return true; } +template::value>> bool is_binate( const TT& tt ) //if the function is binate in any variable returns true { auto numvars = tt.num_vars(); @@ -152,6 +154,7 @@ void convert_to_binary(int64_t num,std::vector bin_num, uint32_t _num_vars std::reverse(bin_num.begin(),bin_num.end()) return; } +template::value>> bool is_threshold( const TT& tt, std::vector* plf = nullptr ) { std::vector linear_form; From 9d49cff9bb3f0df57d83e2991e60db4b1bfad0a9 Mon Sep 17 00:00:00 2001 From: Roberto Guido Date: Fri, 4 Dec 2020 17:16:49 +0100 Subject: [PATCH 05/14] test1 --- include/kitty/threshold_identification.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/kitty/threshold_identification.hpp b/include/kitty/threshold_identification.hpp index ccb2a8f6..2f200e83 100755 --- a/include/kitty/threshold_identification.hpp +++ b/include/kitty/threshold_identification.hpp @@ -134,6 +134,7 @@ bool is_binate( const TT& tt ) //if the function is binate in any variable retur } if (unate == false) count++; } + } if( count == numvars) //if it is binate with respect to all functions return true; else @@ -237,7 +238,6 @@ bool is_threshold( const TT& tt, std::vector* plf = nullptr ) } delete_lp(plp); return true; - } } } /* namespace kitty */ From 854753e19a2f2954ec679e666a3c4cceeba6409b Mon Sep 17 00:00:00 2001 From: Roberto Guido Date: Fri, 4 Dec 2020 17:20:59 +0100 Subject: [PATCH 06/14] test1 --- include/kitty/threshold_identification.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/kitty/threshold_identification.hpp b/include/kitty/threshold_identification.hpp index 2f200e83..f3a6f39e 100755 --- a/include/kitty/threshold_identification.hpp +++ b/include/kitty/threshold_identification.hpp @@ -58,7 +58,7 @@ namespace kitty in the end. \return `true` if `tt` is a TF; `false` if `tt` is a non-TF. */ -enum Constraint_Type { GE=1, LE, EQ }; //GE=1 /* >= <= == */ +//enum Constraint_Type { GE=1, LE, EQ }; //GE=1 /* >= <= == */ template::value>> bool is_positive_unate_in_x( const TT& tt, const uint8_t x ) //check if the function is positive unate in the variable x { From 289fe4eac988c63e6a3112aca517628f908b4c3c Mon Sep 17 00:00:00 2001 From: Roberto Guido Date: Fri, 4 Dec 2020 17:30:32 +0100 Subject: [PATCH 07/14] test1 --- include/kitty/threshold_identification.hpp | 34 +++++++++++----------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/include/kitty/threshold_identification.hpp b/include/kitty/threshold_identification.hpp index f3a6f39e..cf3f056e 100755 --- a/include/kitty/threshold_identification.hpp +++ b/include/kitty/threshold_identification.hpp @@ -140,7 +140,7 @@ bool is_binate( const TT& tt ) //if the function is binate in any variable retur else return false; } -void convert_to_binary(int64_t num,std::vector bin_num, uint32_t _num_vars) +void convert_to_binary(int64_t num,std::vector bin_num, uint32_t num_vars) { uint32_t i=0; while(num!=0){ @@ -152,7 +152,7 @@ void convert_to_binary(int64_t num,std::vector bin_num, uint32_t _num_vars i++; bin_num.emplace_back(0); } - std::reverse(bin_num.begin(),bin_num.end()) + std::reverse(bin_num.begin(),bin_num.end()); return; } template::value>> @@ -185,40 +185,40 @@ bool is_threshold( const TT& tt, std::vector* plf = nullptr ) }*/ lprec *plp; REAL row[tt.num_vars()+2]; - plp=make_lp(0,num_vars()+1); + plp=make_lp(0,tt.num_vars()+1); if(plp == NULL){ return false; //couldn't construct a new model } - set_add_rowmode(lp,TRUE); + set_add_rowmode(plp,TRUE); std::vector binary; /*ONSET CONSTRAINTS*/ for(auto i = 1u; i <= ONSET.size(); ++i){ - convert_to_binary(ONSET(i),binary,tt.num_vars()); + convert_to_binary(ONSET[i],binary,tt.num_vars()); for ( auto k = 0u; k < tt.num_vars(); k++ ){ if(is_negative_unate_in_x(tt,k)){ - if(binary(k) == 0) binary(k)=1; - else binary(k)=0; + if(binary[k] == 0) binary[k]=1; + else binary[k]=0; } } for(auto j = 1u; j <= binary.size(); ++j){ - row[j]= REAL(binary(j)); + row[j]= REAL(binary[j]); } - row[j]=-1.0; + row[binary.size()]=-1.0; add_constraint(plp,row,GE,0); } /*OFFSET CONSTRAINTS*/ - for(auto i = 1u; i <= ONSET.size(); ++i){ - convert_to_binary(ONSET(i),binary,tt.num_vars()); + for(auto i = 1u; i <= OFFSET.size(); ++i){ + convert_to_binary(OFFSET[i],binary,tt.num_vars()); for ( auto k = 0u; k < tt.num_vars(); k++ ){ if(is_negative_unate_in_x(tt,k)){ - if(binary(k) == 0) binary(k)=1; - else binary(k)=0; + if(binary[k]== 0) binary[k]=1; + else binary[k]=0; } } for(auto j = 1u; j <= binary.size(); ++j){ - row[j]= REAL(binary(j)); + row[j]= REAL(binary[j]); } - row[j]=-1.0; + row[binary.size()]=-1.0; add_constraint(plp,row,LE,-1.0); } set_add_rowmode(plp, FALSE); @@ -226,9 +226,9 @@ bool is_threshold( const TT& tt, std::vector* plf = nullptr ) for(auto i = 1u; i <= (ONSET.size()+1); ++i){ row[i]=1.0; } - set_obj_fn(lp, row); + set_obj_fn(plp, row); /*SOLVE LP*/ - set_minim(lp); + set_minim(plp); print_lp(plp); /* if tt is TF: */ /* push the weight and threshold values into `linear_form` */ From a64bd6a2edb17305ba6c9d49555fbe32b0eb4140 Mon Sep 17 00:00:00 2001 From: Roberto Guido Date: Fri, 4 Dec 2020 17:35:26 +0100 Subject: [PATCH 08/14] test1 --- include/kitty/threshold_identification.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/kitty/threshold_identification.hpp b/include/kitty/threshold_identification.hpp index cf3f056e..07923eb7 100755 --- a/include/kitty/threshold_identification.hpp +++ b/include/kitty/threshold_identification.hpp @@ -162,11 +162,11 @@ bool is_threshold( const TT& tt, std::vector* plf = nullptr ) /* TODO */ std::vector ONSET, OFFSET; /* if tt is non-TF: */ - if(is_binate) {// a function is binate in any variable, it is surely non-TF + if(is_binate(tt)) {// a function is binate in any variable, it is surely non-TF return false; } /*otherwise tt could be TF*/ - for ( auto bit = tt.numvars - 1; bit > 0; bit-- ) + for ( auto bit = tt.num_vars() - 1; bit > 0; bit-- ) { if(get_bit( tt, bit ) == 1){ //then add it to the ONSET ONSET.emplace_back(bit); From 996e84d053e72f861070bfd89e2a25736eff1585 Mon Sep 17 00:00:00 2001 From: Roberto Guido Date: Mon, 7 Dec 2020 09:35:22 +0100 Subject: [PATCH 09/14] test_monday1 --- include/kitty/threshold_identification.hpp | 52 +++------------------- 1 file changed, 5 insertions(+), 47 deletions(-) diff --git a/include/kitty/threshold_identification.hpp b/include/kitty/threshold_identification.hpp index 07923eb7..4b5283f0 100755 --- a/include/kitty/threshold_identification.hpp +++ b/include/kitty/threshold_identification.hpp @@ -58,7 +58,6 @@ namespace kitty in the end. \return `true` if `tt` is a TF; `false` if `tt` is a non-TF. */ -//enum Constraint_Type { GE=1, LE, EQ }; //GE=1 /* >= <= == */ template::value>> bool is_positive_unate_in_x( const TT& tt, const uint8_t x ) //check if the function is positive unate in the variable x { @@ -101,44 +100,12 @@ template::v bool is_binate( const TT& tt ) //if the function is binate in any variable returns true { auto numvars = tt.num_vars(); - auto count = 0; // count the number of variables with respect to which the function is binate - bool unate; for ( auto i = 0u; i < numvars; i++ ) { - unate = true; - auto const tt1 = cofactor0( tt, i ); - auto const tt2 = cofactor1( tt, i ); - for ( auto bit = 0; bit < ( 2 << ( numvars - 1 ) ); bit++ ) - { - if ( get_bit( tt1, bit ) >= get_bit( tt2, bit ) ) //negative unateness check - { - continue; - } - else - { - unate = false; - } - } - if(unate == false){ - unate =true; - for ( auto bit = 0; bit < ( 2 << ( numvars - 1 ) ); bit++ ) - { - if ( get_bit( tt1, bit ) <= get_bit( tt2, bit ) ) //positive unateness check - { - continue; - } - else - { - unate = false; - } - } - if (unate == false) count++; + if(!(is_negative_unate_in_x(tt,i) || is_positive_unate_in_x(tt,i))) + return false; } - } - if( count == numvars) //if it is binate with respect to all functions return true; - else - return false; } void convert_to_binary(int64_t num,std::vector bin_num, uint32_t num_vars) { @@ -159,14 +126,13 @@ template::v bool is_threshold( const TT& tt, std::vector* plf = nullptr ) { std::vector linear_form; - /* TODO */ std::vector ONSET, OFFSET; /* if tt is non-TF: */ if(is_binate(tt)) {// a function is binate in any variable, it is surely non-TF return false; } /*otherwise tt could be TF*/ - for ( auto bit = tt.num_vars() - 1; bit > 0; bit-- ) + for ( auto bit = (2^(tt.num_vars()) - 1); bit > 0; bit-- ) { if(get_bit( tt, bit ) == 1){ //then add it to the ONSET ONSET.emplace_back(bit); @@ -175,14 +141,6 @@ bool is_threshold( const TT& tt, std::vector* plf = nullptr ) OFFSET.emplace_back(bit); } } - //NOT CORRECT - /* for ( auto i = 0u; i < tt.num_vars(); i++ ) - { - if(is_negative_unate_in_x(tt,i)){ - //change x_i with bar(x_i) <=> exchanging ONSET with the OFFSET - ONSET(i)=swap(OFFSET(i)); - } - }*/ lprec *plp; REAL row[tt.num_vars()+2]; plp=make_lp(0,tt.num_vars()+1); @@ -227,9 +185,10 @@ bool is_threshold( const TT& tt, std::vector* plf = nullptr ) row[i]=1.0; } set_obj_fn(plp, row); + /*PRINT LP*/ + print_lp(plp); /*SOLVE LP*/ set_minim(plp); - print_lp(plp); /* if tt is TF: */ /* push the weight and threshold values into `linear_form` */ if ( plf ) @@ -239,5 +198,4 @@ bool is_threshold( const TT& tt, std::vector* plf = nullptr ) delete_lp(plp); return true; } - } /* namespace kitty */ From 6afd160a27efd975544bd489a9c5c79ef4d03568 Mon Sep 17 00:00:00 2001 From: Roberto Guido Date: Wed, 9 Dec 2020 23:54:11 +0100 Subject: [PATCH 10/14] test2 --- include/kitty/threshold_identification.hpp | 66 ++++++++++++++++------ 1 file changed, 49 insertions(+), 17 deletions(-) diff --git a/include/kitty/threshold_identification.hpp b/include/kitty/threshold_identification.hpp index 4b5283f0..331cf073 100755 --- a/include/kitty/threshold_identification.hpp +++ b/include/kitty/threshold_identification.hpp @@ -103,12 +103,13 @@ bool is_binate( const TT& tt ) //if the function is binate in any variable retur for ( auto i = 0u; i < numvars; i++ ) { if(!(is_negative_unate_in_x(tt,i) || is_positive_unate_in_x(tt,i))) - return false; + return true; } - return true; + return false; } -void convert_to_binary(int64_t num,std::vector bin_num, uint32_t num_vars) +std::vector convert_to_binary(int64_t num, uint32_t num_vars) { + std::vector bin_num; uint32_t i=0; while(num!=0){ bin_num.emplace_back(num%2); @@ -119,8 +120,7 @@ void convert_to_binary(int64_t num,std::vector bin_num, uint32_t num_vars) i++; bin_num.emplace_back(0); } - std::reverse(bin_num.begin(),bin_num.end()); - return; + return bin_num; } template::value>> bool is_threshold( const TT& tt, std::vector* plf = nullptr ) @@ -132,7 +132,7 @@ bool is_threshold( const TT& tt, std::vector* plf = nullptr ) return false; } /*otherwise tt could be TF*/ - for ( auto bit = (2^(tt.num_vars()) - 1); bit > 0; bit-- ) + for (auto bit = 0; bit < ( 2 << ( tt.num_vars() - 1 ) ); bit++) { if(get_bit( tt, bit ) == 1){ //then add it to the ONSET ONSET.emplace_back(bit); @@ -147,38 +147,48 @@ bool is_threshold( const TT& tt, std::vector* plf = nullptr ) if(plp == NULL){ return false; //couldn't construct a new model } - set_add_rowmode(plp,TRUE); std::vector binary; + set_add_rowmode(plp,TRUE); /*ONSET CONSTRAINTS*/ - for(auto i = 1u; i <= ONSET.size(); ++i){ - convert_to_binary(ONSET[i],binary,tt.num_vars()); + for(auto i = 0u; i < ONSET.size(); ++i){ + binary=convert_to_binary(ONSET[i],tt.num_vars()); for ( auto k = 0u; k < tt.num_vars(); k++ ){ if(is_negative_unate_in_x(tt,k)){ if(binary[k] == 0) binary[k]=1; else binary[k]=0; } } - for(auto j = 1u; j <= binary.size(); ++j){ - row[j]= REAL(binary[j]); + //std::reverse(binary.begin(),binary.end()); + for(auto j = 0u; j < binary.size(); ++j){ + row[j+1]= REAL(binary[j]); } - row[binary.size()]=-1.0; + row[binary.size()+1]=-1.0; add_constraint(plp,row,GE,0); } /*OFFSET CONSTRAINTS*/ - for(auto i = 1u; i <= OFFSET.size(); ++i){ - convert_to_binary(OFFSET[i],binary,tt.num_vars()); + for(auto i = 0u; i < OFFSET.size(); ++i){ + binary=convert_to_binary(OFFSET[i],tt.num_vars()); for ( auto k = 0u; k < tt.num_vars(); k++ ){ if(is_negative_unate_in_x(tt,k)){ if(binary[k]== 0) binary[k]=1; else binary[k]=0; } } - for(auto j = 1u; j <= binary.size(); ++j){ - row[j]= REAL(binary[j]); + //std::reverse(binary.begin(),binary.end()); + for(auto j = 0u; j < binary.size(); ++j){ + row[j+1]= REAL(binary[j]); } - row[binary.size()]=-1.0; + row[binary.size()+1]=-1.0; add_constraint(plp,row,LE,-1.0); } + /*GREATER THAN 0 CONSTRAINTS*/ + for ( auto i = 0u; i < tt.num_vars()+1; i++ ){ + for ( auto j = 0u; j <= tt.num_vars()+1; j++ ){ + row[j] = 0; + } + row[i+1] = 1.0; + add_constraint(plp,row,GE,0); + } set_add_rowmode(plp, FALSE); /*SET OBJECTIVE FUNCTION*/ for(auto i = 1u; i <= (ONSET.size()+1); ++i){ @@ -189,8 +199,30 @@ bool is_threshold( const TT& tt, std::vector* plf = nullptr ) print_lp(plp); /*SOLVE LP*/ set_minim(plp); + auto ret=solve(plp); + if(ret==2) + { //the model is infeasible hence the function is non-TF + return false; + } /* if tt is TF: */ /* push the weight and threshold values into `linear_form` */ + // double sol[1+tt.num_vars()+2+tt.num_vars()+1]; + REAL sol[tt.num_vars()+1]; + //get_primal_solution(plp,sol); + get_variables(plp, sol); + //for(auto i=1u; i<(1+tt.num_vars()+1) ;i++){ + for(auto i=0u; i<(1+tt.num_vars()) ;i++){ + linear_form.push_back(int64_t(sol[i])); + //linear_form.push_back(int64_t(sol[tt.num_vars()+2+i])); + } + for ( auto k = 0u; k < tt.num_vars(); k++ ) + { + if ( is_negative_unate_in_x( tt, k ) ) + { + linear_form[k] = -linear_form[k]; + linear_form[linear_form.size() - 1] = linear_form[linear_form.size() - 1] + linear_form[k]; + } + } if ( plf ) { *plf = linear_form; From b570e3b67bc5c6ebcdf447536ca80a75e280f2e2 Mon Sep 17 00:00:00 2001 From: Roberto Guido Date: Thu, 10 Dec 2020 12:10:50 +0100 Subject: [PATCH 11/14] test_of_today --- include/kitty/threshold_identification.hpp | 81 +++++++++------------- 1 file changed, 34 insertions(+), 47 deletions(-) diff --git a/include/kitty/threshold_identification.hpp b/include/kitty/threshold_identification.hpp index 331cf073..03be52c1 100755 --- a/include/kitty/threshold_identification.hpp +++ b/include/kitty/threshold_identification.hpp @@ -39,6 +39,7 @@ #include "cube.hpp" #include "algorithm.hpp" + namespace kitty { @@ -126,61 +127,47 @@ template::v bool is_threshold( const TT& tt, std::vector* plf = nullptr ) { std::vector linear_form; - std::vector ONSET, OFFSET; + std::vector binary; /* if tt is non-TF: */ if(is_binate(tt)) {// a function is binate in any variable, it is surely non-TF return false; } /*otherwise tt could be TF*/ - for (auto bit = 0; bit < ( 2 << ( tt.num_vars() - 1 ) ); bit++) - { - if(get_bit( tt, bit ) == 1){ //then add it to the ONSET - ONSET.emplace_back(bit); - } - else{ //else add it to the OFFSET - OFFSET.emplace_back(bit); - } - } lprec *plp; REAL row[tt.num_vars()+2]; plp=make_lp(0,tt.num_vars()+1); if(plp == NULL){ return false; //couldn't construct a new model } - std::vector binary; set_add_rowmode(plp,TRUE); - /*ONSET CONSTRAINTS*/ - for(auto i = 0u; i < ONSET.size(); ++i){ - binary=convert_to_binary(ONSET[i],tt.num_vars()); - for ( auto k = 0u; k < tt.num_vars(); k++ ){ - if(is_negative_unate_in_x(tt,k)){ - if(binary[k] == 0) binary[k]=1; - else binary[k]=0; - } + /*CONSTRAINTS*/ + int64_t l=0; + for(uint64_t i = tt.num_bits(); i > 0; i--){ + binary=convert_to_binary(int64_t (l),tt.num_vars()); + for ( auto k = 0u; k < tt.num_vars(); k++ ) + { + if ( is_negative_unate_in_x( tt, k ) ) + { + if ( binary[k] == 0 ) + binary[k] = 1; + else + binary[k] = 0; } - //std::reverse(binary.begin(),binary.end()); - for(auto j = 0u; j < binary.size(); ++j){ - row[j+1]= REAL(binary[j]); - } - row[binary.size()+1]=-1.0; - add_constraint(plp,row,GE,0); - } - /*OFFSET CONSTRAINTS*/ - for(auto i = 0u; i < OFFSET.size(); ++i){ - binary=convert_to_binary(OFFSET[i],tt.num_vars()); - for ( auto k = 0u; k < tt.num_vars(); k++ ){ - if(is_negative_unate_in_x(tt,k)){ - if(binary[k]== 0) binary[k]=1; - else binary[k]=0; - } + } + for ( auto j = 0u; j < binary.size(); ++j ) + { + row[j + 1] = REAL( binary[j] ); } - //std::reverse(binary.begin(),binary.end()); - for(auto j = 0u; j < binary.size(); ++j){ - row[j+1]= REAL(binary[j]); - } - row[binary.size()+1]=-1.0; - add_constraint(plp,row,LE,-1.0); - } + row[binary.size() + 1] = -1.0; + if(get_bit(tt,l)==1) + { + add_constraint( plp, row, GE, 0 ); + } + else{ + add_constraint(plp,row,LE,-1.0); + } + l++; + } /*GREATER THAN 0 CONSTRAINTS*/ for ( auto i = 0u; i < tt.num_vars()+1; i++ ){ for ( auto j = 0u; j <= tt.num_vars()+1; j++ ){ @@ -191,14 +178,18 @@ bool is_threshold( const TT& tt, std::vector* plf = nullptr ) } set_add_rowmode(plp, FALSE); /*SET OBJECTIVE FUNCTION*/ - for(auto i = 1u; i <= (ONSET.size()+1); ++i){ + for(auto i = 1u; i <= (tt.num_vars()+1); ++i){ row[i]=1.0; } set_obj_fn(plp, row); /*PRINT LP*/ - print_lp(plp); + //print_lp(plp); /*SOLVE LP*/ set_minim(plp); + /*SET INTEGERS*/ + for(auto i = 1u; i <= (tt.num_vars()+1); ++i){ + set_int(plp,i,TRUE); + } auto ret=solve(plp); if(ret==2) { //the model is infeasible hence the function is non-TF @@ -206,14 +197,10 @@ bool is_threshold( const TT& tt, std::vector* plf = nullptr ) } /* if tt is TF: */ /* push the weight and threshold values into `linear_form` */ - // double sol[1+tt.num_vars()+2+tt.num_vars()+1]; REAL sol[tt.num_vars()+1]; - //get_primal_solution(plp,sol); get_variables(plp, sol); - //for(auto i=1u; i<(1+tt.num_vars()+1) ;i++){ for(auto i=0u; i<(1+tt.num_vars()) ;i++){ linear_form.push_back(int64_t(sol[i])); - //linear_form.push_back(int64_t(sol[tt.num_vars()+2+i])); } for ( auto k = 0u; k < tt.num_vars(); k++ ) { From f50bfcf0232e5f74c748f69e7d2bb6288edcdfb1 Mon Sep 17 00:00:00 2001 From: Roberto Guido Date: Thu, 10 Dec 2020 16:19:12 +0100 Subject: [PATCH 12/14] test3 --- include/kitty/threshold_identification.hpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/include/kitty/threshold_identification.hpp b/include/kitty/threshold_identification.hpp index 03be52c1..a4221647 100755 --- a/include/kitty/threshold_identification.hpp +++ b/include/kitty/threshold_identification.hpp @@ -134,7 +134,8 @@ bool is_threshold( const TT& tt, std::vector* plf = nullptr ) } /*otherwise tt could be TF*/ lprec *plp; - REAL row[tt.num_vars()+2]; + REAL *row= nullptr; + row = (REAL *) malloc((tt.num_vars()+2) * sizeof(*row)); plp=make_lp(0,tt.num_vars()+1); if(plp == NULL){ return false; //couldn't construct a new model @@ -183,7 +184,7 @@ bool is_threshold( const TT& tt, std::vector* plf = nullptr ) } set_obj_fn(plp, row); /*PRINT LP*/ - //print_lp(plp); + print_lp(plp); /*SOLVE LP*/ set_minim(plp); /*SET INTEGERS*/ @@ -197,7 +198,8 @@ bool is_threshold( const TT& tt, std::vector* plf = nullptr ) } /* if tt is TF: */ /* push the weight and threshold values into `linear_form` */ - REAL sol[tt.num_vars()+1]; + REAL *sol= nullptr; + sol = (REAL *) malloc((tt.num_vars()+1) * sizeof(*sol)); get_variables(plp, sol); for(auto i=0u; i<(1+tt.num_vars()) ;i++){ linear_form.push_back(int64_t(sol[i])); @@ -214,6 +216,11 @@ bool is_threshold( const TT& tt, std::vector* plf = nullptr ) { *plf = linear_form; } + /*RELEASE MEMORY ALLOCATION*/ + if(row != NULL) + free(row); + if(sol != NULL) + free(sol); delete_lp(plp); return true; } From 5e9d710b706bfade05b0e6feda0ae738dbdabf0a Mon Sep 17 00:00:00 2001 From: Roberto Guido Date: Tue, 15 Dec 2020 09:09:58 +0100 Subject: [PATCH 13/14] test of today --- include/kitty/threshold_identification.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/kitty/threshold_identification.hpp b/include/kitty/threshold_identification.hpp index a4221647..78c31646 100755 --- a/include/kitty/threshold_identification.hpp +++ b/include/kitty/threshold_identification.hpp @@ -141,7 +141,7 @@ bool is_threshold( const TT& tt, std::vector* plf = nullptr ) return false; //couldn't construct a new model } set_add_rowmode(plp,TRUE); - /*CONSTRAINTS*/ + /*CONSTRAINTS ON THE ONSET AND ON THE OFFESET*/ int64_t l=0; for(uint64_t i = tt.num_bits(); i > 0; i--){ binary=convert_to_binary(int64_t (l),tt.num_vars()); @@ -184,10 +184,10 @@ bool is_threshold( const TT& tt, std::vector* plf = nullptr ) } set_obj_fn(plp, row); /*PRINT LP*/ - print_lp(plp); + //print_lp(plp); /*SOLVE LP*/ set_minim(plp); - /*SET INTEGERS*/ + /*SET INTEGER VALUES*/ for(auto i = 1u; i <= (tt.num_vars()+1); ++i){ set_int(plp,i,TRUE); } From cd2e4bfc79ad223b98956915de6aa24c6f3311b7 Mon Sep 17 00:00:00 2001 From: Roberto Guido Date: Thu, 17 Dec 2020 18:29:46 +0100 Subject: [PATCH 14/14] today --- include/kitty/threshold_identification.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/kitty/threshold_identification.hpp b/include/kitty/threshold_identification.hpp index 78c31646..6332ea26 100755 --- a/include/kitty/threshold_identification.hpp +++ b/include/kitty/threshold_identification.hpp @@ -141,7 +141,7 @@ bool is_threshold( const TT& tt, std::vector* plf = nullptr ) return false; //couldn't construct a new model } set_add_rowmode(plp,TRUE); - /*CONSTRAINTS ON THE ONSET AND ON THE OFFESET*/ + /*CONSTRAINTS ON THE ONSET AND ON THE OFFSET*/ int64_t l=0; for(uint64_t i = tt.num_bits(); i > 0; i--){ binary=convert_to_binary(int64_t (l),tt.num_vars());