-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpackage.cpp
231 lines (220 loc) · 8.02 KB
/
package.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "package.h"
#include <nlopt.hpp>
#include <vector>
#include <stdio.h>
#include <limits>
#include <exception>
#include <iostream>
#include <map>
#include "condition.h"
#include "fee_compute.h"
#include "sum_compute.h"
#include "risk_compute.h"
#include "revenue_compute.h"
#include <math.h>
using namespace std;
double fee_ratio_uplimit=0.005;
double approach_base=1.2;
double epsilon=std::numeric_limits<double>::epsilon();
class optimization_result{
public:
nlopt::result result;
double minf;
std::vector<double> *weight;
optimization_result(nlopt::result result,double minf,std::vector<double> *weight){
this->result=result;
this->minf=minf;
this->weight=weight;
// std::vector<double> lf(np);
// this->weight=new std::vector<double>(np);
// for(int i=0;i<this->weight->size();i++)
// this->weight->at(i)=weight->at(i);
}
~optimization_result(){
delete weight;
}
};
Package::Package(int np){
this->np=np;
price=new std::vector<double>(np);
w_walk_lb=new std::vector<double>(np);
w_walk_ub=new std::vector<double>(np);
revenue_weight=new std::vector<double>(np);
risk_weight=new std::vector<double>(np*np);
}
int Package::getNp(){
return np;
}
void Package::setNp(int x){
this->np=x;
delete price;
price=new std::vector<double>(np);
delete w_walk_lb;
w_walk_lb=new std::vector<double>(np);
delete w_walk_ub;
w_walk_ub=new std::vector<double>(np);
delete revenue_weight;
revenue_weight=new std::vector<double>(np);
delete risk_weight;
risk_weight=new std::vector<double>(np*np);
}
void Package::setPrice(double *weight){
for(int i=0;i<np;i++){
price->at(i)=weight[i];
}
}
void Package::setLb(double *weight){
for(int i=0;i<np;i++){
w_walk_lb->at(i)=weight[i];
}
}
void Package::setUb(double *weight){
for(int i=0;i<np;i++){
w_walk_ub->at(i)=weight[i];
}
}
void Package::setRevenue(double *weight){
for(int i=0;i<np;i++){
revenue_weight->at(i)=weight[i];
}
}
void Package::setRisk(double *weight){
for(int i=0;i<np;i++){
for(int j=0;j<np;j++)
risk_weight->at(i*np+j)=weight[i*np+j];
}
}
void Package::run(double assets,double *w_original,double *out_weights,bool verbose){
std::vector<double> w_walk_u(np);
std::vector<double> w_walk_d(np);
std::vector<double> w_original_vec(np);
double fee_uplimit=assets*fee_ratio_uplimit;
//cout<<"up and down:"<<endl;
for(int i=0;i<np;i++){
w_walk_u[i]=w_original[i]+1.5*price->at(i)/fee_uplimit;
w_walk_d[i]=w_original[i]-1.5*price->at(i)/fee_uplimit;
w_original_vec[i]=w_original[i];
if(verbose)
cout<<w_walk_u[i]<<" "<<w_walk_d[i]<<endl;
}
Condition *condition=new Condition(assets);
condition->w_original=&w_original_vec;
condition->w_walk_d=&w_walk_d;
condition->w_walk_u=&w_walk_u;
condition->price=price;
std::vector<int> mask_d(np);
condition->mask=&mask_d;
for(int i=0;i<np;i++){
condition->mask->at(i)=0;
}
condition->revenue_weight=revenue_weight;
condition->risk_weight=risk_weight;
std::map<int,optimization_result*> result_map;
int possibility=pow(2,np);
for(int i=0;i<possibility;i++){
int fake_np=0;
for(int j=0;j<np;j++){
condition->mask->at(j)= (i & (1<<j)) ? 1:(fake_np++,0);
//condition->mask->at(j)=0;
if(verbose)
printf("%d ",condition->mask->at(j));
}
if(verbose)
printf("fake_np is %d;i is %d\n",fake_np,i);
if(fake_np==0){
//所有的数据都保持不动
std::vector<double> grad(fake_np);
double minf=revenue_cost(std::vector<double>(0),grad,condition);
if(verbose)
printf("%lf\n",minf);
std::vector<double> *weight_result=new std::vector<double>(np);
for(int j=0;j<np;j++)
weight_result->at(j)=w_original[j];
result_map.insert(std::pair<int,optimization_result*>(i,new optimization_result(nlopt::SUCCESS,minf,weight_result)));
continue;
}
// if(i==105)
// std::cout<<std::endl;
//-----------------------------------------------------------------
std::vector<double> *w_walk=new std::vector<double>(fake_np);
std::vector<double> *w_walk_lower_bounds=new std::vector<double>(fake_np);
std::vector<double> *w_walk_upper_bounds=new std::vector<double>(fake_np);
//-----------------------------------------------------------------
int t=0;
for(int j=0;j<np;j++){
if(condition->mask->at(j)==0){
w_walk->at(t)=(w_original[j]>=w_walk_ub->at(j)?w_walk_ub->at(j):(w_original[j]<=w_walk_lb->at(j)?w_walk_lb->at(j):w_original[j]));
w_walk_lower_bounds->at(t)=w_walk_lb->at(j);
w_walk_upper_bounds->at(t)=w_walk_ub->at(j);
t++;
}
}
nlopt::opt opt_local(nlopt::LD_LBFGS,fake_np);
nlopt::opt opt(nlopt::AUGLAG, fake_np);
// opt.set_min_objective(cost_f_continuous,condition);
// opt.add_inequality_constraint(fee_constrained_continous,condition,1e-8);
// opt.add_equality_constraint(sum_constrained_continous,condition,1e-8);
opt.set_min_objective(revenue_cost,condition);
opt.add_inequality_constraint(risk_constrained,condition,1e-8);
//opt.add_inequality_constraint(risk_constrained_ne,condition,1e-8);
opt.add_equality_constraint(fee_sum_constrained,condition,1e-8);
//opt.add_inequality_constraint(fee_sum_constrained,condition,1e-8);
opt.set_xtol_rel(1e-6);
opt.set_maxtime(0.05);
opt.set_lower_bounds(*w_walk_lower_bounds);
opt.set_upper_bounds(*w_walk_upper_bounds);
opt.set_local_optimizer(opt_local);
nlopt::result result;
double minf=100.0;
std::vector<double> *weight_result=new std::vector<double>(np);
try{
result = opt.optimize(*w_walk, minf);
int l=0;
for(int j=0;j<np;j++){
if(condition->mask->at(j)==0){
std::cout<<w_walk->at(l)<<" ";
weight_result->at(j)=w_walk->at(l);
l++;
}
else{
std::cout<<w_original[j]<<" ";
weight_result->at(j)=w_original[j];
}
}
std::cout<<std::endl;
std::vector<double> sx(0);
if(verbose){
std::cout<<risk_constrained(*w_walk,sx,condition)<<std::endl;
std::cout<<fee_sum_constrained(*w_walk,sx,condition)<<std::endl;
}
result_map.insert(std::pair<int,optimization_result*>(i,new optimization_result(result,minf,weight_result)));
}catch(std::exception &e){
if(verbose)
std::cout<<e.what()<<std::endl;
result_map.insert(std::pair<int,optimization_result*>(i,new optimization_result(nlopt::FAILURE,minf,weight_result)));
}
//--------------------------------------------------------------------
delete w_walk;
delete w_walk_lower_bounds;
delete w_walk_upper_bounds;
//--------------------------------------------------------------------
}
std::map<int,optimization_result*>::iterator it;
it=result_map.begin();
double mmm=100.0;
//std::vector<double> mm_weight(np);
//std::cout<<"================================="<<std::endl;
for (it;it!=result_map.end();it++)
{
if(it->second->minf<mmm && (it->second->result >0 && it->second->result <6)){
mmm=it->second->minf;
for(int i=0;i<np;i++)
//mm_weight[i]=it->second->weight->at(i);
out_weights[i]=it->second->weight->at(i);
}
if(verbose)
std::cout<<it->first<<":"<<it->second->result<<" "<<it->second->minf<<std::endl;
delete it->second; //release memory
}
delete condition; //release memory
}