1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
#include "stdafx.h"
#define _SECURE_SCL 0
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include "smile.h"
#include "smilearn.h"
using namespace std;
void CreateNetwork(void) {
DSL_network LIN_4T;
int x1 = LIN_4T.AddNode(DSL_CPT, "x1");
int x2 = LIN_4T.AddNode(DSL_CPT, "x2");
.......
int x30 = LIN_4T.AddNode(DSL_CPT, "x30");
int A = LIN_4T.AddNode(DSL_CPT, "A");
int B = LIN_4T.AddNode(DSL_CPT, "B");
int C = LIN_4T.AddNode(DSL_CPT, "C");
int D = LIN_4T.AddNode(DSL_CPT, "D");
LIN_4T.AddArc(A, x1);
LIN_4T.AddArc(B, x2);
LIN_4T.AddArc(C, x3);
.......
LIN_4T.AddArc(D, x29);
LIN_4T.AddArc(C, x30);
LIN_4T.AddArc(D, x30);
LIN_4T.AddArc(B, A);
LIN_4T.AddArc(C, B);
LIN_4T.AddArc(D, C);
LIN_4T.WriteFile("LIN_4T.xdsl");
}
void staticEM() {
DSL_dataset LT;
if (LT.ReadFile("C:\\Users\\Bo\\Desktop\\C++\\LIN_4.txt") != DSL_OKAY) {
cout << "Cannot read data file...exiting" << endl;
exit(1);
}
DSL_network LIN_4T;
if (LIN_4T.ReadFile("LIN_4T.xdsl", DSL_XDSL_FORMAT) != DSL_OKAY) {
cout << "Cannot read network...exiting." << endl;
exit(1);
}
vector<DSL_datasetMatch> matches;
string err;
if (LT.MatchNetwork(LIN_4T, matches, err) != DSL_OKAY) {
cout << "Cannot match network...exiting." << endl;
exit(1);
}
double loglik;
DSL_em em;
if (em.Learn(LT, LIN_4T, matches, &loglik) != DSL_OKAY) {
cout << "Cannot learn parameters...exiting." << endl;
exit(1);
}
LIN_4T.UpdateBeliefs();
//ATTRIBUTE CPs
int A = LIN_4T.FindNode("A");
DSL_sysCoordinates theCoordinates(*LIN_4T.GetNode(A)->Value());
theCoordinates.GoFirst();
double P_A_1 = theCoordinates.UncheckedValue();
theCoordinates.Next();
double P_A_0 = theCoordinates.UncheckedValue();
int B = LIN_4T.FindNode("B");
theCoordinates.LinkTo(*LIN_4T.GetNode(B)->Value());
theCoordinates.GoFirst();
double P_B_1_A_1 = theCoordinates.UncheckedValue();
.......
int x30 = LIN_4T.FindNode("x30");
theCoordinates.LinkTo(*LIN_4T.GetNode(x30)->Value());
theCoordinates.GoFirst();
double P_x30_1_C_1_D_1 = theCoordinates.UncheckedValue();
theCoordinates.Next();
double P_x30_0_C_1_D_1 = theCoordinates.UncheckedValue();
theCoordinates.Next();
double P_x30_1_C_1_D_0 = theCoordinates.UncheckedValue();
theCoordinates.Next();
double P_x30_0_C_1_D_0 = theCoordinates.UncheckedValue();
theCoordinates.Next();
double P_x30_1_C_0_D_1 = theCoordinates.UncheckedValue();
theCoordinates.Next();
double P_x30_0_C_0_D_1 = theCoordinates.UncheckedValue();
theCoordinates.Next();
double P_x30_1_C_0_D_0 = theCoordinates.UncheckedValue();
theCoordinates.Next();
double P_x30_0_C_0_D_0 = theCoordinates.UncheckedValue();
theCoordinates.GoLast();
double loglikelihood = loglik;
double arr[] = {loglikelihood, P_A_1,P_A_0,
P_B_1_A_1,P_B_0_A_1,P_B_1_A_0,P_B_0_A_0,
P_C_1_B_1,P_C_0_B_1,P_C_1_B_0,P_C_0_B_0,
P_D_1_C_1,P_D_0_C_1,P_D_1_C_0,P_D_0_C_0,
P_x1_1_A_1,P_x1_0_A_1,P_x1_1_A_0,P_x1_0_A_0,
P_x2_1_B_1,P_x2_0_B_1,P_x2_1_B_0,P_x2_0_B_0,
P_x3_1_C_1,P_x3_0_C_1,P_x3_1_C_0,P_x3_0_C_0,
P_x4_1_D_1,P_x4_0_D_1,P_x4_1_D_0,P_x4_0_D_0,
........ P_x30_1_C_0_D_0,P_x30_0_C_0_D_0 };
// arr has 207 elements
std::vector<double> cp_table(arr, arr + 207);
ofstream output;
output.open("C:\\Users\\Bo\\Desktop\\C++\\output.txt", std::ofstream::out|std::ofstream::app);
for (size_t i = 0; i < cp_table.size(); i++) {
output << cp_table[i] << endl;
output.close();
}
}
int main(int argc, char* const argv[]) {
CreateNetwork();
double data[3001][90];
std::ifstream file("C:\\Users\\Bo\\Desktop\\C++\\data_l.csv");
for (int row = 0; row < 3001; ++row)
{
std::string line;
std::getline(file, line);
if (!file.good())
break;
std::stringstream iss(line);
for (int col = 0; col < 90; ++col)
{
std::string val;
std::getline(iss, val, ',');
if (!iss.good())
break;
std::stringstream convertor(val);
convertor >> data[row][col];
}
}
for (int i = 0; i< 3001; ++i) {
for (int k = 0; k < 3; ++k) {
for (int j = (k - 1) * 30; j < (k - 1) * 30 + 30; ++j) {
double dat[3001][30];
dat[i][j] = data[i][j];
ofstream LIN_4("C:\\Users\\Bo\\Desktop\\C++\\LIN_4.txt");
LIN_4 << dat[i][j];
staticEM();
}
}
}
}
|