GreenCloud Simulator
linearpmodel.cc
Go to the documentation of this file.
1 /*
2  * linearpmodel.cc
3  *
4  * @date Feb 25, 2014
5  * @author Guzek:Mateusz
6  */
7 
8 #include "linearpmodel.h"
9 #include "../resource.h"
10 
11 static class LinearPModelClass : public TclClass {
12 public:
13  LinearPModelClass() : TclClass("LinearPModel") {}
14  TclObject* create(int argc, const char*const*argv) {
15  return (new LinearPModel());
16  }
18 
19 
21 
22  /*LastResType+2 = +1 for elements number, +1 for the intercept:*/
23  coefficients = new double[LastResType+2];
24  initialized = new bool[LastResType+2];
25  setCoefNumber(LastResType+2);
26  for(int i = 0; i < LastResType+2; i++){
27  coefficients[i]= 0;
28  initialized[i]=false;
29  }
30 
31 }
32 
34 
35  delete[] coefficients;
36  delete[] initialized;
37  name_.clear();
38 }
39 
40 void LinearPModel::setCoefNumber(int number){
41 
42  coef_number = number;
43  if(coefficients != NULL){
44  delete[] coefficients;
45  delete[] initialized;
46  }
47  coefficients = new double[coef_number];
48  initialized = new bool[coef_number];
49 }
50 
51 
53  /* Linear power model does not accept components*/
54  return;
55 }
56 
57 double LinearPModel::estimate(int size, double* predictors){
58  if(ready){
59  if(size!= coef_number - 1){
60  std::cerr <<"Incorrect size of predictors array!\n";
61  }
62  double result = coefficients[size];
63  for(int i = 0; i < size; i++){
64  result += predictors[i] * coefficients[i];
65  }
66  return result;
67  } else {
68  std::cerr << "The model is not correctly initalized.\n" ;
69  print();
70  std::cerr << "Aborting simulation";
71  abort();
72  }
73 }
74 
76  double * load = new double[coef_number];
77  for(int i = 0; i < coef_number; i++){
78  load[i] = 1;
79  }
80  return estimate(coef_number,load);
81 }
82 
83 
84 
86  std::cout << "Linear model: "<< name_ << "\n";
87  if(ready){
88  std::cout << "Coefficients:\n";
89  for(int i = 0; i < coef_number ; i++){
90  std::cout << i << ": " << coefficients[i] << "\n";
91  }
92  } else {
93  std::cout << "Model not initalized properly\n";
94  }
95 }
96 
97 void LinearPModel::setCoefficient(const char* coef,double value){
98  if(strcmp(coef, "Intercept") != 0){
99  res_type type = Resource::translateType(coef);
100  coefficients[type]=value;
101  initialized[type]=true;
102  } else {
103  coefficients[coef_number-1]= value;
104  initialized[coef_number-1]=true;
105  }
106  updateInit();
107 }
108 
109 void LinearPModel::setCoefficientNumeric(const char* coef,double value){
110  if(strcmp(coef, "Intercept") != 0){
111  int i = atoi(coef);
112  coefficients[i]=value;
113  initialized[i]=true;
114  } else {
115  coefficients[coef_number-1]= value;
116  initialized[coef_number-1]=true;
117  }
118  updateInit();
119 }
121  bool result = true;
122  for(int i = 0; i < coef_number-1; i++){
123  result = result && initialized[i];
124  }
125  ready = result;
126 }
127 
128 int LinearPModel::command(int argc, const char*const* argv)
129 {
130 
131  if (argc == 2) {
132  if (strcmp(argv[1], "print") == 0) {
133  /* print general info */
134  print();
135  return (TCL_OK);
136  }
137  } else if (argc==3){
138  if (strcmp(argv[1], "set-name") == 0) {
139  setName(argv[2]);
140  return(TCL_OK);
141  } else if (strcmp(argv[1], "set-coef-number") == 0) {
142  setCoefNumber(atoi(argv[2]));
143  return(TCL_OK);
144  } else {
145  return(TCL_ERROR);
146  }
147  }
148  else if (argc == 4) {
149  if (strcmp(argv[1], "set-coefficient") == 0) {
150  setCoefficient(argv[2],atof(argv[3]));
151  return(TCL_OK);
152  } else if (strcmp(argv[1], "set-coefficient-numeric") == 0) {
153  setCoefficientNumeric(argv[2],atof(argv[3]));
154  return(TCL_OK);
155  } else {
156  return(TCL_ERROR);
157  }
158  }
159  return (TCL_ERROR);
160 }
161 
TclObject * create(int argc, const char *const *argv)
Definition: linearpmodel.cc:14
virtual int command(int argc, const char *const *argv)
res_type
Definition: resource.h:19
virtual void addComponent(DcResource *component)
Definition: linearpmodel.cc:52
static res_type translateType(const char *t)
Definition: resource.cc:85
void setCoefficientNumeric(const char *coef, double value)
void updateInit()
void setCoefficient(const char *coef, double value)
Definition: linearpmodel.cc:97
virtual ~LinearPModel()
Definition: linearpmodel.cc:33
LinearPModelClass class_powermodel
void setCoefNumber(int number)
Definition: linearpmodel.cc:40
virtual double getMaxPower()
Definition: linearpmodel.cc:75
virtual void print()
Definition: linearpmodel.cc:85
virtual double estimate(int size, double *predictors)
Definition: linearpmodel.cc:57