From 011f307279f18ddbcdbd48f71f0d6ac2ff418333 Mon Sep 17 00:00:00 2001 From: farhan Date: Wed, 18 Jan 2023 00:40:55 +0600 Subject: [PATCH 1/3] added A-star Search code --- algorithms/artificial-intelligence/astar.cpp | 172 +++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 algorithms/artificial-intelligence/astar.cpp diff --git a/algorithms/artificial-intelligence/astar.cpp b/algorithms/artificial-intelligence/astar.cpp new file mode 100644 index 00000000..d94d6c3d --- /dev/null +++ b/algorithms/artificial-intelligence/astar.cpp @@ -0,0 +1,172 @@ +#include +using namespace std; + +struct myQueue{ + int x, y; + double g, h, f ; + bool flag ; +}Q[100], temp,par[100][100]; + + +int grid[100][100] ; +double heuristicCost[100][100], dis[100][100] ; + +void initialization(int n, int m, int stri, int strj) +{ + int k = 0 ; + dis[stri][strj] = 0.0 ; + for(int i = 0 ; i < n ; i++){ + for(int j = 0 ; j < m ; j++){ + Q[k].x = i ; + Q[k].y = j ; + Q[k].g = dis[i][j] ; + Q[k].h = heuristicCost[i][j] ; + Q[k].f = Q[k].g + Q[k].h ; + Q[k].flag = true ; + k++ ; + } + } +} + +bool isEmpty(int n, int m){ + for(int i = 0 ; i < n*m ; i++){ + if(Q[i].flag) + return false ; + } + return true ; +} + +int pop(int n, int m) +{ + int min = 1000000 ; + int index ; + for(int i = 0 ; i < n*m ; i++) + { + if(Q[i].f < min && Q[i].flag == true){ + min = Q[i].f ; + index = i ; + } + } + Q[index].flag = false ; + return index ; +} + +void updateQueueCost(int i, int j, double cost, int n, int m){ + for(int k = 0 ; k < n*m ; k++){ + if(Q[k].x == i && Q[k].y == j){ + Q[k].g = cost ; + Q[k].f = Q[k].g + Q[k].h ; + } + } +} +void aStarSearch(int n, int m) +{ + while(!isEmpty(n, m)){ + int index = pop(n,m) ; + temp = Q[index] ; + int i = temp.x ; + int j = temp.y ; + if(grid[i-1][j] > -1){ + if(dis[i-1][j] > dis[i][j] + grid[i-1][j]){ + dis[i-1][j] = dis[i][j] + grid[i-1][j]; + par[i-1][j].x = i; + par[i-1][j].y = j; + updateQueueCost(i-1, j, dis[i-1][j], n, m); + } + } + + if(grid[i+1][j]>-1){ + if(dis[i+1][j] > dis[i][j]+grid[i+1][j]){ + dis[i+1][j] = dis[i][j] + grid[i+1][j]; + par[i+1][j].x = i; + par[i+1][j].y = j; + updateQueueCost(i+1, j, dis[i+1][j], n, m); + cout<<"i,j: "<-1){ + if(dis[i][j-1] > dis[i][j]+grid[i][j-1]){ + dis[i][j-1] = dis[i][j]+grid[i][j-1]; + par[i][j-1].x = i; + par[i][j-1].y = j; + updateQueueCost(i, j-1, dis[i][j-1], n, m); + cout<<"i,j: "<-1){ + if(dis[i][j+1] > dis[i][j]+grid[i][j+1]){ + dis[i][j+1] = dis[i][j]+grid[i][j+1]; + par[i][j+1].x = i; + par[i][j+1].y = j; + updateQueueCost(i, j+1, dis[i][j+1], n, m); + cout<<"i,j: "< Date: Sun, 22 Jan 2023 23:18:16 +0600 Subject: [PATCH 2/3] added code and data file for NaiveBayesian algorithm --- .../Naive Bayesian/NaiveBay.txt | 10 + .../Naive Bayesian/NaiveBayesian.cpp | 269 ++++++++++++++++++ .../Naive Bayesian/naiveBayTest.txt | 3 + .../Naive Bayesian/naiveBayTesting.cpp | 214 ++++++++++++++ 4 files changed, 496 insertions(+) create mode 100644 algorithms/artificial-intelligence/Naive Bayesian/NaiveBay.txt create mode 100644 algorithms/artificial-intelligence/Naive Bayesian/NaiveBayesian.cpp create mode 100644 algorithms/artificial-intelligence/Naive Bayesian/naiveBayTest.txt create mode 100644 algorithms/artificial-intelligence/Naive Bayesian/naiveBayTesting.cpp diff --git a/algorithms/artificial-intelligence/Naive Bayesian/NaiveBay.txt b/algorithms/artificial-intelligence/Naive Bayesian/NaiveBay.txt new file mode 100644 index 00000000..9a8e4416 --- /dev/null +++ b/algorithms/artificial-intelligence/Naive Bayesian/NaiveBay.txt @@ -0,0 +1,10 @@ +yes single 125 no +no married 100 no +no single 70 no +yes married 120 no +no divorced 95 yes +no married 60 no +yes divorced 220 no +no single 85 yes +no married 75 no +no single 90 yes \ No newline at end of file diff --git a/algorithms/artificial-intelligence/Naive Bayesian/NaiveBayesian.cpp b/algorithms/artificial-intelligence/Naive Bayesian/NaiveBayesian.cpp new file mode 100644 index 00000000..0e0455a7 --- /dev/null +++ b/algorithms/artificial-intelligence/Naive Bayesian/NaiveBayesian.cpp @@ -0,0 +1,269 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +vectorRvalue; +vectorMvalue; +vectorTvalue; +vectorEvade; +vectortax; + +int numberofobject = 0; +double priorY=0, priorN=0; + +int findOccurance(string att, string cl) +{ + FILE *fptr; + char r[100], m[100], t[100], e[100]; + int count = 0 ; + if ((fptr = fopen("NaiveBay.txt", "r")) == NULL) { + printf("Error! opening file"); + exit(1); + } + while (fscanf(fptr, "%s %s %s %s", r, m, t, e)!= EOF){ + string s1 = "" ; + string s2 = "" ; + string s3 = "" ; + string s4 = "" ; + + for(int i = 0 ; i < strlen(r) ; i++){ + s1 += r[i] ; + } + for(int i = 0 ; i < strlen(m) ; i++){ + s2 += m[i] ; + } + + for(int i = 0 ; i < strlen(t) ; i++){ + s3 += t[i] ; + } + //n = std::stoi(s3); + + for(int i = 0 ; i < strlen(e) ; i++){ + s4 += e[i] ; + } + if(s1 == att && s4 == cl) count++ ; + if(s2 == att && s4 == cl) count++ ; + } + fclose(fptr); + return count ; +} + +int main() { + char r[100], m[100], t[100], e[100]; + FILE *fptr; + if ((fptr = fopen("NaiveBay.txt", "r")) == NULL){ + printf("Error! opening file"); + exit(1); + } + string s = "" ; + + int yes=0, no=0; + + while (fscanf(fptr, "%s %s %s %s", r, m, t, e)!= EOF){ + //printf("%s %s", a, c); + //printf("\n") ; + numberofobject++ ; + + bool flag = false ; //Refund + string s = "" ; + for(int i = 0 ; i < strlen(r) ; i++) + { + s += r[i] ; + } + for(int i = 0 ; i < Rvalue.size() ; i++) + { + if(s == Rvalue[i]) + { + flag = true ; + } + } + if(!flag) + Rvalue.push_back(s); + + + flag = false ; //Marital status + s = "" ; + for(int i = 0 ; i < strlen(m) ; i++) + { + s += m[i] ; + } + for(int i = 0 ; i < Mvalue.size() ; i++) + { + if(s == Mvalue[i]) + { + flag = true ; + } + } + if(!flag) + Mvalue.push_back(s); + + s = ""; + for(int i=0; i +using namespace std; + +int main() +{ + vectorrefund; + vectormarital; + vectorincome; + + //reading testing file. + FILE *fptr; + if ((fptr = fopen("naiveBayTest.txt", "r")) == NULL) { + printf("Error! opening file"); + exit(1); + } + + char r[100], m[100], in[100]; + + while(fscanf(fptr, "%s %s %s", r, m, in)!=EOF){ + string s1=""; + string s2=""; + string s3=""; + + for(int i=0; irefund1; + vectorevade1; + vectorrefhood; + + if ((fptr = fopen("refundhood.txt", "r")) == NULL) { + printf("Error! opening file"); + exit(1); + } + + char r1[100], e1[100], ref1[100]; + + while(fscanf(fptr, "%s %s %s", r1, e1, ref1)!=EOF){ + string s1=""; + string s2=""; + string s3=""; + + for(int i=0; imarry; + vectorevade2; + vectormaritalhood; + + if ((fptr = fopen("maritalhood.txt", "r")) == NULL) { + printf("Error! opening file"); + exit(1); + } + + char m1[100], e2[100], mar[100]; + + while(fscanf(fptr, "%s %s %s", m1, e2, mar)!=EOF){ + string s1=""; + string s2=""; + string s3=""; + + for(int i=0; itax; + vectorevade3; + vectortaxhood; + + if ((fptr = fopen("taxhood.txt", "r")) == NULL) { + printf("Error! opening file"); + exit(1); + } + + char t[100], e3[100], taxx[100]; + + while(fscanf(fptr, "%s %s %s", t, e3, taxx)!=EOF){ + string s1=""; + string s2=""; + string s3=""; + + for(int i=0; ino) + cout<<"'YES' Class"<<"\n"; + else + cout<<"'NO' Class"<<"\n"; + + + + return 0; +} From 6ba6acf7bca2725bd703499b9c47ba43d12a7ee5 Mon Sep 17 00:00:00 2001 From: farhan Date: Sun, 22 Jan 2023 23:46:24 +0600 Subject: [PATCH 3/3] added a text file to explain the problem --- algorithms/artificial-intelligence/Naive Bayesian/codeGuide.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 algorithms/artificial-intelligence/Naive Bayesian/codeGuide.txt diff --git a/algorithms/artificial-intelligence/Naive Bayesian/codeGuide.txt b/algorithms/artificial-intelligence/Naive Bayesian/codeGuide.txt new file mode 100644 index 00000000..e69de29b