-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathagent.cpp
134 lines (103 loc) · 2.58 KB
/
agent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
Solve a consensus problem using ADMM.
*/
// #ifndef __AGENT_CPP
// #define __AGENT_CPP
#include <iostream>
#include <armadillo>
// #include "agent.h"
using namespace std;
using namespace arma;
/* Empty constructor */
Agent::Agent(){}
/* Constructor that gets the dimension of the variables. It also initializes the initial values of x and u */
Agent::Agent(const unsigned _n) {
n = _n;
x = randu<vec>(n);
u = randu<vec>(n);
}
/* Constructor that gets the local cost function, which we assume it is a quadratic function 1/2 x'Px + q'x + r
and the ADMM parameter rho. It also initializes the initial values of x and u */
Agent::Agent(const mat _P, const vec _q, const double _r, const double _rho)
{
costs.P = _P; costs.q = _q; costs.r = _r;
rho = _rho;
n = costs.P.n_rows;
x = randu<vec>(n);
u = randu<vec>(n);
}
/* Destructor */
Agent::~Agent() {}
/* Get local value of z */
vec Agent::getValueOfz(){
return z;
}
/* Set local value of z */
void Agent::setValueOfz(const arma::vec _z){
z = _z;
Agent::updateValueOfu();
}
/* Get value of x */
vec* Agent::getValueOfx(){
return &x;
}
/* Get value of u */
vec* Agent::getValueOfu(){
return &u;
}
/* Return dimension of x, n */
unsigned* Agent::getValueOfn(){
return &n;
}
/* Return ADMM parameter rho */
double* Agent::getValueOfrho(){
return ρ
}
/* Return cost in the objective function */
cost* Agent::getValueOfCosts(){
return &costs;
}
/* Compute local value of the primal iterate x at the current iteration */
void Agent::updateValueOfx(){
vec* u_ = Agent::getValueOfu();
vec z_ = Agent::getValueOfz();
double* rho_ = Agent::getValueOfrho();
unsigned* n_ = Agent::getValueOfn();
cost* costs = Agent::getValueOfCosts();
mat P_ = costs->P; vec q_ = costs->q;
x = inv( P_ + *rho_ * eye<mat>(*n_,*n_) ) * (*rho_ * (z_ - *u_) - q_ );
Agent::updateValueOfz();
}
/* Compute local value of the dual iterate u at the current iteration */
void Agent::updateValueOfu(){
vec* x_ = Agent::getValueOfx();
vec* u_ = Agent::getValueOfu();
vec z_ = Agent::getValueOfz();
u = *u_ + *x_ - z_;
Agent::updateValueOfx();
}
/* Compute local value of the dual iterate z at the current iteration */
void Agent::updateValueOfz(){
vec* x_ = Agent::getValueOfx();
vec* u_ = Agent::getValueOfu();
vec z = *x_ - *u_;
}
/* Print current iterate x */
void Agent::printx()
{
vec* x_ = Agent::getValueOfx();
x_->print("x:");
}
/* Print current iterate u */
void Agent::printu()
{
vec* u_ = Agent::getValueOfu();
u_->print("u:");
}
/* Print current iterate z */
void Agent::printz()
{
vec z_ = Agent::getValueOfz();
z_.print("z:");
}
// #endif