int main(int argc, char** argv) { glutInit(&argc, argv); myInit(argv[0]); create_nurbs(); setRand(); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutIdleFunc(idle); glutMainLoop(); return(0); }
void model(int dim, float alpha1, float alpha2, float alpha3, float beta6, float beta7, float beta8) { cout << "begin initialization: " << endl; loadRating(DIR_PATH,rateMatrix); //初始化完成 mean = setMeanRating(rateMatrix); //求平均值,求bu和bi的值 int i,u,j,k; //对bu,bi进行初始化,bu,bi的初始化的方法是求平均值,然后减去mean, //在计算的过程中必须要记得所有的值,包括所有的打分总数和分数总和 int tmpIndex = 0; for(i = 1; i < USER_NUM+1; ++i){ int vSize = rateMatrix[i].size(); for(j=0; j < vSize; ++j) { bu[i] += rateMatrix[i][j].rate; buNum[i] += 1; bi[rateMatrix[i][j].item] += rateMatrix[i][j].rate; biNum[rateMatrix[i][j].item] += 1; } } //以下过程求平均值 for(i = 1; i < USER_NUM+1; ++i) { if(buNum[i]>=1)bu[i] = (bu[i]/buNum[i]) - mean; else bu[i] = 0.0; //log<<i<<'\t'<<bu[i]<<endl; //buBase[i] = bu[i]; } for(i = 1; i < ITEM_NUM+1; ++i) { if(biNum[i] >=1)bi[i] = (bi[i]/biNum[i]) - mean; else bi[i] = 0.0; //biBase[i] = bi[i]; //logbi<<i<<'\t'<<bi[i]<<endl; } //@todo 不知道是否能针对初始化的过程做一些优化 //对w ,c进行初始化,初始化的方法是随机函数,不知道这种方法是否好,是否会影响结果??????? for(int i = 1; i < ITEM_NUM+1; ++i){ setRand(w[i],ITEM_NUM+1,0); //初始化q[i] setRand(c[i],ITEM_NUM+1,0); //初始化x[i] } for(int i = 1; i < ITEM_NUM+1; ++i){ setRand(q[i],K_NUM+1,0); //初始化q[i] setRand(y[i],K_NUM+1,0); //初始化p[i] } for(int i = 1; i < USER_NUM+1; ++i){ setRand(p[i],K_NUM+1,0); //初始化p[i] } cout <<"initialization end!"<<endl<< "begin iteration: " << endl; float pui = 0.0 ; // 预测的u对i的打分 double preRmse = 1000000000000.0; //用于记录上一个rmse,作为终止条件的一种,如果rmse上升了,则停止 double nowRmse = 0.0; cout <<"begin testRMSEProbe(): " << endl; RMSEProbe(); //main loop for(int step = 0; step < 70; ++step){ //只迭代35次 long double rmse = 0.0; int n = 0; for( u = 1; u < USER_NUM+1; ++u) { //循环处理每一个用户 int RuNum = rateMatrix[u].size(); //用户u打过分的item数目 float sqrtRuNum = 0.0; if(RuNum>1) sqrtRuNum = (1.0/sqrt(RuNum)); //cacluate puTemp for( k=1; k<K_NUM+1; ++k) { double sumy = 0.0; for(i=0; i < RuNum; ++i) {// 循环处理u打分过的每一个item int itemI = rateMatrix[u][i].item; sumy += y[itemI][k]; } puTemp[u][k] = p[u][k]+ sqrtRuNum * sumy; } double sumQE[K_NUM+1] = {0.0}; //迭代处理 for(i=0; i < RuNum; ++i) {// 循环处理u打分过的每一个item int itemI = rateMatrix[u][i].item; short rui = rateMatrix[u][i].rate; //实际的打分 double bui = mean + bu[u] + bi[itemI]; pui = predictRate(u,itemI); //cout<<u<<'\t'<<i<<'\t'<<pui<<'\t'<<rui<<endl; float eui = rui - pui; if( isnan(eui) ) {// fabs(eui) >= 4.2 || cout<<u<<'\t'<<i<<'\t'<<pui<<'\t'<<rui<<" "<<bu[u]<<" "<<bi[itemI]<<" "<<mean<<endl; exit(1); } rmse += eui * eui; ++n; if(n % 10000000 == 0)cout<<"step:"<<step<<" n:"<<n<<" dealed!"<<endl; bu[u] += alpha1 * (eui - beta6 * bu[u]); bi[itemI] += alpha1 * (eui - beta6 * bi[itemI]); for( k=1; k< K_NUM+1; ++k) { p[u][k] += alpha2 * (eui*q[itemI][k] - beta7*p[u][k]); q[itemI][k] += alpha2 * (eui*puTemp[u][k] - beta7*q[itemI][k]); sumQE[K_NUM+1] = eui * q[itemI][k]; } for( j=0; j< RuNum; ++j) { int itemJ = rateMatrix[u][j].item; double ruj = (double)rateMatrix[u][j].rate; w[itemI][itemJ] += alpha3 * (sqrtRuNum*eui*(ruj - mean -bu[u]-bi[itemJ]) - beta8*w[itemI][itemJ]); c[itemI][itemJ] += alpha3 * (sqrtRuNum*eui - beta8*c[itemI][itemJ]); } } for(j=0; j < RuNum; ++j) {// 循环处理u打分过的每一个item int itemJ = rateMatrix[u][j].item; for( k=1; k< K_NUM+1; ++k) { y[itemJ][k] += alpha2 * (sqrtRuNum * sumQE[k] - beta7*y[itemJ][k]); } } } nowRmse = sqrt( rmse / n); if( nowRmse >= preRmse && step >= 3) break; //如果rmse已经开始上升了,则跳出循环 else preRmse = nowRmse; cout << step << "\t" << nowRmse <<'\t'<< preRmse<<" n:"<<n<<endl; //alpha1 *= 0.9; //alpha2 *= 0.9; //alpha3 *= 0.9; RMSEProbe(); // 检查训练集情况 //alpha *= 0.992; //逐步减小学习速率 //RMSEProbe(); } //ratingAll(data); //预测所有的结果 RMSEProbe(); // 检查训练集情况 return; }
void model(int dim, float alpha, float beta) { cout << "begin initialization: " << endl; loadRating(DIR_PATH,rateMatrix); //初始化完成 mean = setMeanRating(rateMatrix); //求平均值,求bu和bi的值 int i,u,j,k; //对bu,bi进行初始化,bu,bi的初始化的方法是求平均值,然后减去mean, //在计算的过程中必须要记得所有的值,包括所有的打分总数和分数总和 int tmpIndex = 0; for(i = 1; i < USER_NUM+1; ++i){ int vSize = rateMatrix[i].size(); for(j=0; j < vSize; ++j) { bi[rateMatrix[i][j].item] += (rateMatrix[i][j].rate - mean); biNum[rateMatrix[i][j].item] += 1; } } //ofstream logbi("bi.txt"); for(i = 1; i < ITEM_NUM+1; ++i) { if(biNum[i] >=1)bi[i] = bi[i]/(biNum[i]+25); else bi[i] = 0.0; } for(i = 1; i < USER_NUM+1; ++i){ int vSize = rateMatrix[i].size(); for(j=0; j < vSize; ++j) { bu[i] += (rateMatrix[i][j].rate - mean - bi[rateMatrix[i][j].item]); buNum[i] += 1; } } //以下过程求平均值 for(i = 1; i < USER_NUM+1; ++i) { if(buNum[i]>=1)bu[i] = bu[i]/(buNum[i]+10); else bu[i] = 0.0; //logbi<<i<<" "<<buNum[i]<<" "<<bu[i]<<endl; } //logbi.close(); //@todo 不知道是否能针对初始化的过程做一些优化 //对w进行初始化,初始化的方法是随机函数,不知道这种方法是否好,是否会影响结果??????? for(int i = 1; i < ITEM_NUM+1; ++i){ setRand(q[i],K_NUM+1,mean); //初始化q[i] } for(int i = 1; i < USER_NUM+1; ++i){ setRand(p[i],K_NUM+1,mean); //初始化p[i] } cout <<"initialization end!"<<endl<< "begin iteration: " << endl; float pui = 0.0 ; // 预测的u对i的打分 double preRmse = 1000000000000.0; //用于记录上一个rmse,作为终止条件的一种,如果rmse上升了,则停止 double nowRmse = 0.0; cout <<"begin testRMSEProbe(): " << endl; RMSEProbe(); //main loop for(int step = 0; step < 90; ++step){ //只迭代60次 long double rmse = 0.0; int n = 0; for( u = 1; u < USER_NUM+1; ++u) { //循环处理每一个用户 int RuNum = rateMatrix[u].size(); //用户u打过分的item数目 float sqrtRuNum = 0.0; if(RuNum>1) sqrtRuNum = (1.0/sqrt(RuNum)); //迭代处理 for(i=0; i < RuNum; ++i) {// 循环处理u打分过的每一个item int itemI = rateMatrix[u][i].item; short rui = rateMatrix[u][i].rate; //实际的打分 pui = predictRate(u,itemI); float eui = rui - pui; if( isnan(eui) ) {// fabs(eui) >= 4.2 || cout<<u<<'\t'<<i<<'\t'<<pui<<'\t'<<rui<<" "<<bu[u]<<" "<<bi[itemI]<<" "<<mean<<endl; //printArray(q[itemI],p[u],K_NUM+1); exit(1); } rmse += eui * eui; ++n; if(n % 10000000 == 0)cout<<"step:"<<step<<" n:"<<n<<" dealed!"<<endl; bu[u] += alpha * (eui - beta * bu[u]); bi[itemI] += alpha * (eui - beta * bi[itemI]); for( k=1; k< K_NUM+1; ++k) { p[u][k] += alpha * (eui*q[itemI][k] - beta*p[u][k]); q[itemI][k] += alpha * (eui*p[u][k] - beta*q[itemI][k]); } } } nowRmse = sqrt( rmse / n); cout << step << "\t" << nowRmse <<'\t'<< preRmse<<" n:"<<n<<endl; if( nowRmse >= preRmse && step >= 3) break; //如果rmse已经开始上升了,则跳出循环 else preRmse = nowRmse; RMSEProbe(); // 检查训练集情况 //alpha *= 0.999; //逐步减小学习速率 //RMSEProbe(); } RMSEProbe(); // 检查训练集情况 return; }
void jen::Strat(int minR,int maxR,int minC,int maxC, SitRep sitrep) { dir = lt; if(rank == crown) { r = ((maxR -minR)/2 + minR); c = ((maxC -minC)/2 + minC); return; } if(rank == knight) { if(sitrep.thing[(((maxR -minR)/2 + minR))+1][((maxC -minC)/2 + minC)].rank!=knight) { r = (((maxR -minR)/2 + minR))+1; c = ((maxC -minC)/2 + minC); return; } else if(sitrep.thing[(((maxR -minR)/2 + minR))-1][((maxC -minC)/2 + minC)].rank!=knight) { r = (((maxR -minR)/2 + minR))-1; c = ((maxC -minC)/2 + minC); return; } else if(sitrep.thing[(((maxR -minR)/2 + minR))][(((maxC -minC)/2 + minC))+1].rank!=knight) { r = (((maxR -minR)/2 + minR)); c = (((maxC -minC)/2 + minC))+1; return; } else if(sitrep.thing[(((maxR -minR)/2 + minR))][(((maxC -minC)/2 + minC))-1].rank!=knight) { r = (((maxR -minR)/2 + minR)); c = (((maxC -minC)/2 + minC))-1; return; } else { setRand(minR, maxR, minC, maxC, sitrep); return; } } else if(rank == archer) { if(sitrep.thing[(((maxR -minR)/2 + minR))+1][(((maxC -minC)/2 + minC))+1].rank!=archer) { r = (((maxR -minR)/2 + minR))+1; c = (((maxC -minC)/2 + minC))+1; return; } else if(sitrep.thing[(((maxR -minR)/2 + minR))+1][(((maxC -minC)/2 + minC))-1].rank!=archer) { r = (((maxR -minR)/2 + minR))+1; c = (((maxC -minC)/2 + minC))-1; return; } else if(sitrep.thing[(((maxR -minR)/2 + minR))-1][(((maxC -minC)/2 + minC))-1].rank!=archer) { r = (((maxR -minR)/2 + minR))-1; c = (((maxC -minC)/2 + minC))-1; return; } else if(sitrep.thing[(((maxR -minR)/2 + minR))-1][(((maxC -minC)/2 + minC))+1].rank!=archer) { r = (((maxR -minR)/2 + minR))-1; c = (((maxC -minC)/2 + minC))+1; return; } if(sitrep.thing[(((maxR -minR)/2 + minR))+2][(((maxC -minC)/2 + minC))+1].rank!=archer) { r = (((maxR -minR)/2 + minR))+2; c = (((maxC -minC)/2 + minC))+1; return; } else if(sitrep.thing[(((maxR -minR)/2 + minR))+2][(((maxC -minC)/2 + minC))-1].rank!=archer) { r = (((maxR -minR)/2 + minR))+2; c = (((maxC -minC)/2 + minC))-1; return; } else if(sitrep.thing[(((maxR -minR)/2 + minR))-2][(((maxC -minC)/2 + minC))-1].rank!=archer) { r = (((maxR -minR)/2 + minR))-2; c = (((maxC -minC)/2 + minC))-1; return; } else if(sitrep.thing[(((maxR -minR)/2 + minR))-2][(((maxC -minC)/2 + minC))-1].rank!=archer) { r = (((maxR -minR)/2 + minR))-2; c = (((maxC -minC)/2 + minC))+1; return; } else { setRand(minR, maxR, minC, maxC, sitrep); return; } } else if (rank==infantry) { sideOfBoard loca = findSide(minR, maxR, minC, maxC); if(loca==brt) { int i, j; for(j=minC; j<maxC;j++) { for(i=minR; i<maxR; i++) { if(sitrep.thing[i][j].what==space) { r=i; c=j; return; } } } } if(loca==blt) { int i, j; for(j=maxC; j>minC;j--) { for(i=maxR; i>minR; i--) { if(sitrep.thing[i][j].what==space) { r=i; c=j; return; } } } } } }
void model(int dim, double & alpha, double & beta) { vector<Rating> data; loadRating("u1.base",data); //初始化 //1、首先搞清楚整个过程 //(1)求平均值 //2、一步一步实现每个过程 //3、什么时候弄完,什么时候回去 mean = setMeanRating(data); //求平均值,求bu和bi的值 //中间删除一段代码,是xlvector计算平均打分写论文用的 double cc = 1 / (double)(dim); //cout << cc << endl;exit(1); //初始化bu,就是user的bias(偏置) for(int i = 1; i < USER_NUM+1; ++i){ //cout<< bu[i] << endl; //if(bu[i] < 0) continue; //这里bu[i]小于0永远不成立,这个语句永远不会执行 p[i].clear(); //清空所有的p[i] setRand(p[i],dim,cc); //初始化p[i] } cout << bi.size() << endl; for(int i = 1; i < ITEM_NUM+1; ++i){ //if(bi[i] < 0) continue; //cout << i << endl; q[i].clear(); setRand(q[i],dim,cc); } cout << "begin : " << endl; for(int step = 0; step < 25; ++step){ //只迭代25次 double rmse = 0; double n = 0; for(int ii = 0; ii < data.size(); ++ii){ //利用每一个打分调优结果 int i = ii; //下面两句不知道做什么用? //if(data[i].value() > 3) continue; //if(data[i].test > test_level) continue; int user = data[i].user; int item = data[i].item; double rui = data[i].value(); double pui = rating(data[i]); //预测打分,向量点乘 double err = rui - pui; rmse += err * err; ++n; //更新数据 bu[user] += alpha * (err - beta * bu[user]); bi[item] += alpha * (err - beta * bi[item]); for(int k = 0; k < dim; ++k){ p[user][k] += alpha * (err * q[item][k] - beta * p[user][k]); q[item][k] += alpha * (err * p[user][k] - beta * q[item][k]); } } cout << step << "\t" << sqrt(rmse / n) << endl; alpha *= 0.98; //逐步减小学习速率 } ratingAll(data); //预测所有的结果 RMSEProbe(); // 检查训练集情况 //输出bu的值 ofstream outputbu("bu.txt"); for(int i=1; i < USER_NUM+1; i++) { outputbu << i << "\t" << bu[i] << endl; } outputbu.close(); //输出bi的值 ofstream outputbi("bi.txt"); for(int i=1; i < ITEM_NUM+1; i++) { outputbi << i << "\t"<< bi[i] << endl; } outputbu.close(); }
void model(int dim, double & alpha, double & beta) { vector<Rating> data; loadRating("u1.base",data); //初始化 /* 检查初始化是否正确 for(int i=0; i<data.size();i++){ cout << data[i].user<<" "<<data[i].item<<" "<<data[i].rate<<" "<<endl; } cout<<data.size()<<endl; exit(1); */ //1、首先搞清楚整个过程 //(1)求平均值 //2、一步一步实现每个过程 //3、什么时候弄完,什么时候回去 setMeanRating(data); //求平均值 map<int, int> ni; //? map<int, map<string, int> > itr, itn; //一个存储某部电影每月的积分总和,一个存储某部电影当月打分的总数量 for(int i = 0; i < data.size(); ++i){ int user = data[i].user; int item = data[i].item; bu[user] = 0; //svd++,用户偏差 bi[item] = 0; //item偏差 if(data[i].test == 0){ //test应该是将一批数据随机赋值为1, if(i % 1000000 == 0) cout << i << endl; //每一百万个数据打印一下,以前共100M的打分 ostringstream oss; oss << data[i].year() << "-" << data[i].month() << "-" << 1;//data[i].mday(); itr[item][oss.str()] += data[i].real; //存储每月的打分总和值 itn[item][oss.str()] += 1; //存储每月的数量 ni[item]++; //该电影打分总次数 } } //out主要记录月份对item打分的总的影响 ofstream out("netflix-bit.csv"); out << endl; for(map<int, map<string, int> >::iterator i = itr.begin(); i != itr.end(); ++i){ for(map<string, int>::iterator t = i->second.begin(); t != i->second.end(); ++t){ out << i->first << "," << t->first << "," ; out << (double)(t->second) / (double)(itn[i->first][t->first]) << "," ; out << itn[i->first][t->first] << "," << ni[i->first] << endl; //itemId,2002-7-1,itemId在2002-7的平均打分,itemId在2002-7的打分总数,该电影打分总次数 } } out.close(); //return; 这个地方是原来有的,估计是xlvector测试用的,可以不管 double cc = 1 / (double)(dim); //初始化bu,就是user的bias(偏置) for(int i = 0; i < bu.size(); ++i){ if(bu[i] < 0) continue; //这里bu[i]小于0永远不成立,这个语句永远不会执行 p[i].clear(); //清空所有的p[i] setRand(p[i],dim,cc); //初始话p[i] } for(int i = 0; i < bi.size(); ++i){ if(bi[i] < 0) continue; q[i].clear(); setRand(q[i],dim,cc); } cout << "begin : " << endl; for(int step = 0; step < 25; ++step){ //只迭代25次 double rmse = 0; double n = 0; for(int ii = 0; ii < data.size(); ++ii){ //利用每一个打分调优结果 int i = ii; //下面两句不知道做什么用? if(data[i].value() > 3) continue; if(data[i].test > test_level) continue; int user = data[i].user; int item = data[i].item; double rui = data[i].value(); double pui = rating(data[i]); //预测打分,向量点乘 double err = rui - pui; rmse += err * err; ++n; //更新数据 bu[user] += alpha * (err - beta * bu[user]); bi[item] += alpha * (err - beta * bi[item]); for(int k = 0; k < dim; ++k){ p[user][k] += alpha * (err * q[item][k] - beta * p[user][k]); q[item][k] += alpha * (err * p[user][k] - beta * q[item][k]); } } cout << step << "\t" << sqrt(rmse / n) << "\t"; RMSEProbe(data); // 检查训练集情况 alpha *= 0.92; //逐步减小学习速率 } ratingAll(data); //预测所有的结果 if(test_level == 0){ ostringstream oss; oss << "pb-svd-" << dim << ".txt"; ratingProbe(oss.str().c_str(), data); saveRating("../data/xlvector-svd20.data0", data); } if(test_level == 1){ ostringstream oss1; oss1 << "qf-svd-" << dim << ".txt"; ratingQuiz(oss1.str().c_str(), data); } }
void model(int dim, double & alpha, double & beta) { vector<Rating> data; loadRating("../data/netflix3.data",data); //初始化 setMeanRating(data); //求平均值 map<int, int> ni; map<int, map<string, int> > itr, itn; for(int i = 0; i < data.size(); ++i){ int user = data[i].user; int item = data[i].item; bu[user] = 0; bi[item] = 0; if(data[i].test == 0){ if(i % 1000000 == 0) cout << i << endl; ostringstream oss; oss << data[i].year() << "-" << data[i].month() << "-" << 1;//data[i].mday(); itr[item][oss.str()] += data[i].real; itn[item][oss.str()] += 1; ni[item]++; } } ofstream out("netflix-bit.csv"); out << endl; for(map<int, map<string, int> >::iterator i = itr.begin(); i != itr.end(); ++i){ for(map<string, int>::iterator t = i->second.begin(); t != i->second.end(); ++t){ out << i->first << "," << t->first << "," << (double)(t->second) / (double)(itn[i->first][t->first]) << "," << itn[i->first][t->first] << "," << ni[i->first] << endl; } } out.close(); return; double cc = 1 / (double)(dim); for(int i = 0; i < bu.size(); ++i){ if(bu[i] < 0) continue; p[i].clear(); setRand(p[i],dim,cc); } for(int i = 0; i < bi.size(); ++i){ if(bi[i] < 0) continue; q[i].clear(); setRand(q[i],dim,cc); } cout << "begin : " << endl; for(int step = 0; step < 25; ++step){ double rmse = 0; double n = 0; for(int ii = 0; ii < data.size(); ++ii){ int i = ii; if(data[i].value() > 3) continue; if(data[i].test > test_level) continue; int user = data[i].user; int item = data[i].item; double rui = data[i].value(); double pui = rating(data[i]); double err = rui - pui; rmse += err * err; ++n; bu[user] += alpha * (err - beta * bu[user]); bi[item] += alpha * (err - beta * bi[item]); for(int k = 0; k < dim; ++k){ p[user][k] += alpha * (err * q[item][k] - beta * p[user][k]); q[item][k] += alpha * (err * p[user][k] - beta * q[item][k]); } } cout << step << "\t" << sqrt(rmse / n) << "\t"; RMSEProbe(data); alpha *= 0.92; } ratingAll(data); if(test_level == 0){ ostringstream oss; oss << "pb-svd-" << dim << ".txt"; ratingProbe(oss.str().c_str(), data); saveRating("../data/xlvector-svd20.data0", data); } if(test_level == 1){ ostringstream oss1; oss1 << "qf-svd-" << dim << ".txt"; ratingQuiz(oss1.str().c_str(), data); } }