Hello
I built a personal 2D array for my current project. I also store these arrays (called Matrix) in a std::vector. The matrices are 24*181 and currently I sue between 30 and 50 of them. I don't think project details are relevant however I'm getting 3 errors which I have no idea where they might be coming from.
1)After some calculations on the data, the output will feature a random 1.#J / 1.#R or something like this. This is strange because in all my calculations I never divide by zero, plus I'll get 0-2 of those per Matrix; NOTE: this happens only after operations are done on the arrays, not on initialization.
2)In one function I try to determine the best possible Matrix. The function does it's job well but upon exit the program crashes and I have no idea why. I tried debugging with Dev C++ but nothing showed.
3)Finally, when attempting to access a specific value (within range) within a vector,I also get a crash. This is strange because it's always the same value, and judging by the output file there isn't anything wrong with that particular value. On top of that I already used the same value beforehand in another method.
#include "Matrix.hpp"
#include "Constants.hpp"
#include "Functions.hpp"
#include "InvertMatrix.hpp"
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/lu.hpp>
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <math.h>
using namespace std;
/******************************************************************************/
/* FIRST SIMULTATION */
/******************************************************************************/
void getPerDayAvg(vector<Matrix> days, vector<Matrix> &mean, int avgDays) {
for(int i = 0; i < TOTAL_DAYS - avgDays; i++) {
mean.push_back(Matrix());
for(int hour = 0; hour < HOURS; hour ++) {
for(int angle = 0; angle < ANGLES; angle ++) {
for(int day = i; day < i + avgDays; day++)
mean[i].data[hour][angle] += days[day].data[hour][angle];
mean[i].data[hour][angle] /= avgDays;
mean[i].data[hour][angle] = pow(days[avgDays+i].data[hour][angle] - mean[i].data[hour][angle],2);
}
}
}
}
void getGlobalAvg(vector<Matrix> &globalMean, vector<Matrix> &mean, int index) {
globalMean.push_back(Matrix());
for(int hour = 0; hour < HOURS; hour++) {
for(int angle = 0; angle < ANGLES; angle++) {
for(int matrix = 0; matrix < mean.size() ; matrix++)
globalMean[index-1].data[hour][angle] += mean[matrix].data[hour][angle];
globalMean[index-1].data[hour][angle] /= mean.size();
}
}
mean.clear();
}
void getMeans(vector<Matrix> dataSample, vector<Matrix> avgPD, vector<Matrix> &avgGlobal){
for(int i = 1; i <= MAX_DAYS_TAKEN; i++) {
getPerDayAvg(dataSample, avgPD, i);
getGlobalAvg(avgGlobal, avgPD, i);
}
write(avgGlobal,"constantModel.txt");
}
/******************************************************************************/
/* SECOND SIMULTATION */
/******************************************************************************/
void linearRegression(vector<Matrix> days, vector<Matrix> &prediction, vector<Matrix> today, int avgDays){
using namespace boost::numeric::ublas;
cout << "OK" <<endl;
cout << days[40].data[1][136]<<endl;
cout << "notok" <<endl;
matrix<double> first;
matrix<double> invert (avgDays, avgDays);
matrix<double> X (TOTAL_DAYS - avgDays,avgDays);
matrix<double> Y (TOTAL_DAYS - avgDays, 1);
matrix<double> B (avgDays, 1);
for(int hour = 0; hour < HOURS; hour++){
for(int angle = 0; angle < ANGLES; angle++){
for(int i = avgDays; i < TOTAL_DAYS; i++)
Y.insert_element(i-avgDays,0,days[i].data[hour][angle]);
for(int i = 0; i < TOTAL_DAYS - avgDays; i++)
for(int j = 0; j < avgDays; j++)
X.insert_element(i,j,days[i+j].data[hour][angle]);
first = prod(trans(X),X);
InvertMatrix(first,invert);
B = prod(invert,trans(X));
B = prod(B,Y);
for(int i = 0; i < avgDays; i++)
prediction[avgDays-1].data[hour][angle] += B(i,0)*days[TOTAL_DAYS - i].data[hour][angle];
prediction[avgDays - 1].data[hour][angle] = pow(prediction[avgDays - 1].data[hour][angle] - today[0].data[hour][angle],2);
}
}
}
/******************************************************************************/
/* GENERAL FUNCTIONS */
/******************************************************************************/
void simulation(Matrix average, Matrix m) {
int cost;
int currentPosition = 181;
int bestPosition;
double currentGain, bestGain;
for(int hour = 0; hour < HOURS; hour++){
cout << "Hour: "<<hour<<"\t";
currentPosition = optimalPosition(hour, m);
currentGain = m.data[hour][currentPosition];
bestPosition = optimalPosition(hour, average);
bestGain = average.data[hour][bestPosition];
cost = abs(currentPosition - bestPosition)*COST;
if(bestGain < cost && bestGain > currentGain){
currentPosition = bestPosition;
cout << " Moving panel by " << abs(currentPosition - bestPosition) << " degrees." << endl;
}
else
cout << " Not moving " << endl;
}
}
int getBest(vector<Matrix> global){
vector<int> winner;
for(int i = 0; i < MAX_DAYS_TAKEN;i++)
winner.push_back(0);
double min;
int minIndex;
for(int hour = 0; hour < HOURS; hour++) {
for(int angle = 0; angle < ANGLES; angle++) {
min = 100.0;
for(int matrix = 0; matrix < global.size() ; matrix++){
if((global[matrix].data[hour][angle] < min) and (global[matrix].data[hour][angle] != 0.0)) {
min = global[matrix].data[hour][angle];
minIndex = matrix;
}
}
if(min != 0.0)
winner[minIndex]++;
}
}
for(int i = 0; i < winner.size(); i++)
cout << winner[i] << " ";
vector<int>::iterator result;
result = max_element(winner.begin(), winner.end());
return (int)distance(winner.begin(), result);
}
void getAverage(vector<Matrix> sample, Matrix &avg, int days){
cout << days<<endl;
for(int hour = 0; hour < HOURS; hour ++) {
for(int angle = 0; angle < ANGLES; angle ++) {
for(int i = TOTAL_DAYS - days; i < TOTAL_DAYS; i++)
avg.data[hour][angle] += sample[i].data[hour][angle];
avg.data[hour][angle] /= days;
}
}
write(avg, "average.txt");
}
void init(vector<Matrix> &m, vector<Matrix> &predictions){
for(int i = 0; i < TOTAL_DAYS; i++)
m.push_back(Matrix(i));
for(int i = 0; i < MAX_DAYS_TAKEN; i++)
predictions.push_back(Matrix());
write(m,"base.txt");
}
int main(){
srand (time(NULL));
vector<Matrix> __daysSample, __meanPerDays, __globalMean, __predictions, __today;
int best;
Matrix average = Matrix();
Matrix today = Matrix(1);
init(__daysSample,__predictions);
write(today, "today.txt");
//getMeans(__daysSample, __meanPerDays, __globalMean);
//best = getBest(__globalMean) +1;
//cout << "days chosen:" << best << endl;
//getAverage(__daysSample, average,best);
//simulation(average,today);
for(int i = 1; i <= MAX_DAYS_TAKEN; i++)
linearRegression(__daysSample,__predictions,__today, i);
write(__predictions, "result.txt");
//best = getBest(__predictions) + 1;
//cout << "days chosen:" << best;
//simulation(best, __today, __predictions);
cin.get();
}
I can provide the rest if someone wants to compile. This is the main file. The first error appears in the .txt files. The second appears in main() right after getMeans(...)-->getBest(...)-->ERROR;
The final error appears in the first function simulation(...) @
prediction[avgDays-1].data[hour][angle] += B(i,0)*days[TOTAL_DAYS - i].data[hour][angle];
. Currently I get errors for hour = 1; angle = 136;