• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • About
  • Life
  • Tech
  • Travel
  • Work
  • Questions
  • Contact

Welcome

.

This code make allocation of ressources but some fucntions i did not understand it , can you please help me with that [closed]

April 10, 2020 by

Questions › This code make allocation of ressources but some fucntions i did not understand it , can you please help me with that [closed]
0
Vote Up
Vote Down
Garmaine asked 3 years ago
Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 4 hours ago.

This code make the allocation of ressources. but some fucntions i did not understand it , can you please help me with that . I need to undestand each function on the code it writted using c++ and ns2 for the algorithm .

Thanks

#include "linearpmodel.h"
#include "../resource.h"

static class LinearPModelClass : public TclClass {
public:
    LinearPModelClass() : TclClass("LinearPModel") {}
    TclObject* create(int argc, const char*const*argv) {
        return (new LinearPModel());
    }
} class_powermodel;


LinearPModel::LinearPModel() {

    /*LastResType+2 = +1 for elements number, +1 for the intercept:*/
    coefficients = new double[LastResType+2];
    initialized = new bool[LastResType+2];
    setCoefNumber(LastResType+2);
    for(int i = 0; i < LastResType+2; i++){
        coefficients[i]= 0;
        initialized[i]=false;
    }

}

LinearPModel::~LinearPModel() {

    delete[] coefficients;
    delete[] initialized;
    name_.clear();
}

// Not stuff like this which is a code comment 🙂

   void LinearPModel::setCoefNumber(int number){

        coef_number = number;
        if(coefficients != NULL){
        delete[] coefficients;
        delete[] initialized;
        }
        coefficients = new double[coef_number];
        initialized = new bool[coef_number];
    }


    void LinearPModel::addComponent(DcResource* component){
        /* Linear power model does not accept components*/
        return;
    }

    double LinearPModel::estimate(int size, double* predictors){
        if(ready){
            if(size!=  coef_number - 1){
                std::cerr <<"Incorrect size of predictors array!\n";
            }
            double result = coefficients[size];
            for(int i = 0; i < size; i++){
                result += predictors[i] * coefficients[i];
            }
            return result;
        } else {
            std::cerr << "The model is not correctly initalized.\n" ;
            print();
            std::cerr << "Aborting simulation";
            abort();
        }
    }

    double LinearPModel::getMaxPower(){
        double * load = new double[coef_number];
        for(int i = 0; i < coef_number; i++){
            load[i] = 1;
        }
        return estimate(coef_number,load);
    }

// Not stuff like this which is a code comment :-)

void LinearPModel::print(){
    std::cout << "Linear model: "<< name_ << "\n";
    if(ready){
        std::cout << "Coefficients:\n";
        for(int i = 0; i < coef_number ; i++){
            std::cout << i << ": " << coefficients[i] << "\n";
        }
    } else {
        std::cout << "Model not initalized properly\n";
    }
}

void LinearPModel::setCoefficient(const char* coef,double value){
    if(strcmp(coef, "Intercept") != 0){
        res_type type = Resource::translateType(coef);
        coefficients[type]=value;
        initialized[type]=true;
    } else {
        coefficients[coef_number-1]= value;
        initialized[coef_number-1]=true;
    }
    updateInit();
}

void LinearPModel::setCoefficientNumeric(const char* coef,double value){
    if(strcmp(coef, "Intercept") != 0){
        int i = atoi(coef);
        coefficients[i]=value;
        initialized[i]=true;
    } else {
        coefficients[coef_number-1]= value;
        initialized[coef_number-1]=true;
    }
    updateInit();
}

// comment

void LinearPModel::updateInit(){
    bool result = true;
    for(int i = 0; i < coef_number-1; i++){
        result = result && initialized[i];
    }
    ready = result;
}

int LinearPModel::command(int argc, const char*const* argv)
{

    if (argc == 2) {
        if (strcmp(argv[1], "print") == 0) {
            /* print general info */
            print();
            return (TCL_OK);
        }
    } else if (argc==3){
        if (strcmp(argv[1], "set-name") == 0) {
            setName(argv[2]);
            return(TCL_OK);
        } else if (strcmp(argv[1], "set-coef-number") == 0) {
            setCoefNumber(atoi(argv[2]));
            return(TCL_OK);
        } else {
            return(TCL_ERROR);
        }
    }
    else if (argc == 4) {
        if (strcmp(argv[1], "set-coefficient") == 0) {
            setCoefficient(argv[2],atof(argv[3]));
            return(TCL_OK);
        } else if (strcmp(argv[1], "set-coefficient-numeric") == 0) {
            setCoefficientNumeric(argv[2],atof(argv[3]));
            return(TCL_OK);
        } else {
            return(TCL_ERROR);
        }
    }
    return (TCL_ERROR);
}

//// Comment I need to undestand each function on the code it writted using c++ and ns2 for the algorithm .

Are you looking for the answer?
Original Question and Possible Answers can be found on `http://stackoverflow.com`

Question Tags: c++, tcl

Please login or Register to submit your answer




Primary Sidebar

Tags

Advancements architecture beautiful life best building calling city commercial convenience employment Finances Cognitive decline Future gadgets Hidden Gems highway Home houses hydration Impact Innovations lamp lighting Mental health military tech Must-See New York City occupation Productivity recreation romance sepia shopping sippy cups smartphones social Technological breakthroughs technology toddlers Treasures turns Uncover Well-being Wonders Work Young onset dementia

Newsletter

Complete the form below, and we'll send you all the latest news.

Footer

Footer Funnies

Who knew that reading the footer could be such a hilarious adventure? As we navigate websites, books, and documents, we often stumble upon the unassuming space at the bottom, only to discover a treasure trove of amusement. In this side-splitting compilation, we present 100 jokes that celebrate the unsung hero of content – the footer. Get ready to chuckle, giggle, and maybe even snort as we dive into the world of footnotes, disclaimers, and hidden comedic gems. Brace yourself for a wild ride through the footer!

Recent

  • Unveiling the Enigma: Almost-Magical Lamp Lights Highway Turns
  • The Impact of Young Onset Dementia on Employment and Finances: Optimizing Post-Diagnostic Approaches
  • 11 Wonders of 2023 Technological Breakthrough – Unveiling the Future
  • Work from Home and Stay Mentally Sane – Achieve Productivity and Well-being
  • Hidden Gems of New York City – Uncover the Must-See Treasures!

Search

Tags

Advancements architecture beautiful life best building calling city commercial convenience employment Finances Cognitive decline Future gadgets Hidden Gems highway Home houses hydration Impact Innovations lamp lighting Mental health military tech Must-See New York City occupation Productivity recreation romance sepia shopping sippy cups smartphones social Technological breakthroughs technology toddlers Treasures turns Uncover Well-being Wonders Work Young onset dementia

Copyright © 2023