Click here to Skip to main content
15,881,424 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: To write a c program to maintain a log of football match b/w 2 teams and then compare the results of the log entered by user and announce the winner. Pin
Tarun Jha27-Dec-17 1:09
Tarun Jha27-Dec-17 1:09 
AnswerRe: To write a c program to maintain a log of football match b/w 2 teams and then compare the results of the log entered by user and announce the winner. Pin
Tarun Jha27-Dec-17 1:11
Tarun Jha27-Dec-17 1:11 
AnswerRe: To write a c program to maintain a log of football match b/w 2 teams and then compare the results of the log entered by user and announce the winner. Pin
Vaclav_28-Dec-17 13:24
Vaclav_28-Dec-17 13:24 
GeneralRe: To write a c program to maintain a log of football match b/w 2 teams and then compare the results of the log entered by user and announce the winner. Pin
Victor Nijegorodov28-Dec-17 21:17
Victor Nijegorodov28-Dec-17 21:17 
GeneralRe: To write a c program to maintain a log of football match b/w 2 teams and then compare the results of the log entered by user and announce the winner. Pin
Tarun Jha31-Dec-17 12:30
Tarun Jha31-Dec-17 12:30 
QuestionClasses hierarchy? Pin
Vaclav_24-Dec-17 7:53
Vaclav_24-Dec-17 7:53 
AnswerRe: Classes hierarchy? Pin
Rick York24-Dec-17 10:36
mveRick York24-Dec-17 10:36 
QuestionCode consuming lot of RAM (Memory) Pin
User 1350945018-Dec-17 23:21
professionalUser 1350945018-Dec-17 23:21 
Below is the simplified version of the code I am using in my project. On running it, I have noticed that it consumes lots of RAM (Memory). I am not getting why is that happening, can anybody guide me in the right direction.

"main.cpp"
C++
#include "ESSolver.h"
using namespace std;

int maxIt;
int minVal;
int maxVal;

int main(){
	int function_eval = 1, nVar = 500, mu = 15, lbd = 100, rhoO = 2, rhoS = mu;
	maxIt = 1000;
	
	if( function_eval == 1){
		minVal = -32; maxVal = 32;
	}
	
	ESSolver es(nVar, mu, lbd, rhoO, rhoS);
	es.function_eval = function_eval;
	es.Init();
	es.Optimize();

	return 0;
}


"ESSolver.cpp"
C++
#include <iostream>
#include <vector>
#include "ESSolver.h"
#include <random>
using namespace std;

ESSolver::ESSolver(int _nVar, int _mu, int _lbd, int _rhoO, int _rhoS):nVar(_nVar), mu(_mu), lbd(_lbd), rhoO(_rhoO), rhoS(_rhoS)
{	
    muPop.resize(mu);
    muSigma.resize(mu);
    muSigma2.resize(mu);
    
    for (int i = 0; i < mu; i++) {
        muPop[i].resize(nVar);
        muSigma2[i].resize(nVar);
    }
    
    lambdaPop.resize(lbd);
    lambdaSigma.resize(lbd);
    lambdaSigma2.resize(lbd);
    
    for (int i = 0; i < lbd; i++) {
        lambdaPop[i].resize(nVar);
        lambdaSigma2[i].resize(nVar);
    }
    
    mulambdaPop.resize(mu+lbd);
    mulambdaSigma.resize(mu+lbd);
    mulambdaSigma2.resize(mu+lbd);
    
    for (int i = 0; i < mu+lbd; i++) {
        mulambdaPop[i].resize(nVar);
        mulambdaSigma2[i].resize(nVar);
    }
    
    rhoPop.resize(rhoO);
    rhoSigma.resize(rhoS);
    rhoSigma2.resize(rhoS);
    
    for (int i = 0; i < rhoO; i++) {
        rhoPop[i].resize(nVar);
    }
    
    for (int i = 0; i < rhoS; i++) {
        rhoSigma2[i].resize(nVar);
    }
    
	return;
}

ESSolver::~ESSolver(void) {
    return;
}

void ESSolver::Init() {
	for (int i = 0; i < mu; i++) {
		for (int j = 0; j < nVar; j++) {
			muPop[i][j] = rand_r(minVal, maxVal);
		}
	}
	
	if (mutOp == 0) {
	    for (int i = 0; i < mu; i++) {
            muSigma[i] = rand01_r();
	    }
	}
	else {
		for (int i = 0; i < mu; i++) {
		    for (int j = 0; j < nVar; j++) {
			    muSigma2[i][j] = rand01_r();
		    }
	    }
    } 
}

void ESSolver::Optimize() {
	int it     = 0; 	
	while (it < maxIt){
		// Manipulating values of 
        // muPop, muSigma, muSigma2, 
        // lambdaPop, lambdaSigma, lambdaSigma2, 
        // mulambdaPop, mulambdaSigma, mulambdaSigma2, 
        // rhoPop, rhoSigma, rhoSigma2
        it = it + 1;
	}
}

int ESSolver::rand_r(int minVal, int maxVal) {
    default_random_engine rng( random_device{}() );
    uniform_int_distribution<int> dist( minVal, maxVal);
    return dist(rng);
}

double ESSolver::rand01_r() {
    default_random_engine rng( random_device{}() );
	uniform_real_distribution<double> dist(0.0, 1.0);
    return dist(rng);
}


"ESSolver.h"
C++
#include <vector>
using namespace std;

extern int maxIt;
extern int minVal;
extern int maxVal;

class ESSolver
{
public:
	ESSolver(int, int, int, int, int);
	~ESSolver(void);
	
    void Init();
    void Optimize();
   
    int rand_r(int, int);								
    double rand01_r();
							
private:
    int nVar;
    int mu;
    int lbd;
    int rhoO;
    int rhoS;
	
	vector<vector<double>> muPop;
	vector<double> muSigma;
	vector<vector<double>> muSigma2;

	vector<vector<double>> lambdaPop;
	vector<double> lambdaSigma;
	vector<vector<double>> lambdaSigma2;

	vector<vector<double>> mulambdaPop;
	vector<double> mulambdaSigma;
	vector<vector<double>> mulambdaSigma2;

	vector<vector<double>> rhoPop;
	vector<double> rhoSigma;
	vector<vector<double>> rhoSigma2;
};


modified 29-Jan-21 21:01pm.

AnswerRe: Code consuming lot of RAM (Memory) Pin
Manish K. Agarwal19-Dec-17 0:24
Manish K. Agarwal19-Dec-17 0:24 
GeneralRe: Code consuming lot of RAM (Memory) Pin
User 1350945019-Dec-17 0:30
professionalUser 1350945019-Dec-17 0:30 
GeneralRe: Code consuming lot of RAM (Memory) Pin
Manish K. Agarwal19-Dec-17 0:48
Manish K. Agarwal19-Dec-17 0:48 
GeneralRe: Code consuming lot of RAM (Memory) Pin
User 1350945019-Dec-17 2:02
professionalUser 1350945019-Dec-17 2:02 
QuestionRe: Code consuming lot of RAM (Memory) Pin
Jochen Arndt19-Dec-17 0:57
professionalJochen Arndt19-Dec-17 0:57 
AnswerRe: Code consuming lot of RAM (Memory) Pin
User 1350945019-Dec-17 2:01
professionalUser 1350945019-Dec-17 2:01 
GeneralRe: Code consuming lot of RAM (Memory) Pin
Jochen Arndt19-Dec-17 2:35
professionalJochen Arndt19-Dec-17 2:35 
AnswerRe: Code consuming lot of RAM (Memory) Pin
Jochen Arndt19-Dec-17 2:59
professionalJochen Arndt19-Dec-17 2:59 
QuestionRe: Code consuming lot of RAM (Memory) Pin
David Crow19-Dec-17 4:12
David Crow19-Dec-17 4:12 
AnswerRe: Code consuming lot of RAM (Memory) Pin
User 1350945019-Dec-17 5:14
professionalUser 1350945019-Dec-17 5:14 
AnswerRe: Code consuming lot of RAM (Memory) Pin
David Crow19-Dec-17 5:28
David Crow19-Dec-17 5:28 
AnswerRe: Code consuming lot of RAM (Memory) Pin
User 1350945019-Dec-17 10:03
professionalUser 1350945019-Dec-17 10:03 
AnswerRe: Code consuming lot of RAM (Memory) Pin
KarstenK20-Dec-17 6:47
mveKarstenK20-Dec-17 6:47 
QuestionGet Supported File System Pin
john563218-Dec-17 22:48
john563218-Dec-17 22:48 
AnswerRe: Get Supported File System Pin
Jochen Arndt18-Dec-17 23:03
professionalJochen Arndt18-Dec-17 23:03 
GeneralRe: Get Supported File System Pin
john563218-Dec-17 23:14
john563218-Dec-17 23:14 
GeneralRe: Get Supported File System Pin
Jochen Arndt18-Dec-17 23:53
professionalJochen Arndt18-Dec-17 23:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.