-
Notifications
You must be signed in to change notification settings - Fork 3
/
loader.h
165 lines (151 loc) · 4.14 KB
/
loader.h
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
//
// loader.hpp
// MOEA
//
// Created by Molin on 04/11/2017.
// Copyright © 2017 merlin. All rights reserved.
//
#ifndef MOEA_D_LOADER_H
#define MOEA_D_LOADER_H
#include <fstream>
#include <sstream>
#include <vector>
#include <iostream>
#include <cstring>
//
//Define structs here
//
struct Constraint
{
unsigned int num_assets = 0;
unsigned int max_assets = 1;
size_t transaction_limit = 0;
size_t cash_change = 0;
};
struct asset
{
double current_price = 0;
double holding = 0;
double cost_buy = 0;
double cost_sell = 0;
double vcost_buy = 0;
double vcost_sell = 0;
int min_buy = 0;
int min_sell = 0;
int max_buy = 0;
double max_sell = 0;
double mean_income = 0;
double diviation_r = 0;
bool sellable = false;
unsigned int id = 0;
double fundpool = 0;
int buy_asset_number = 0;
std::vector<int>history;
struct asset &operator=(struct asset x)
{
this->current_price = x.current_price;
this->holding = x.holding;
this->cost_buy = x.cost_buy;
this->cost_sell = x.cost_sell;
this->vcost_buy = x.vcost_buy;
this->vcost_sell = x.vcost_sell;
this->min_buy = x.min_buy;
this->min_sell = x.min_sell;
this->max_buy = x.max_buy;
this->max_sell = x.max_sell;
this->id = x.id;
this->fundpool = x.fundpool;
this->buy_asset_number = x.buy_asset_number;
return *this;
}
};
struct Set{
std::vector<int>data;
bool isin(const int &x){
for(int i = 0; i<data.size(); i++){
if(data[i]==x) return true;
}
return false;
}
};
//
//Loader function
//
bool loadItem( std::string path,
std::vector<struct asset> &assetArray,
struct Constraint &constraint,
double (&correlation)[31][31])
{
//
//Initialize openning file
//
//Line number starts from 1
//
std::fstream data(path, std::ios::in);
std::string input_coach;
unsigned int line = 1;
int id = 0;
if(!data.is_open()){
std::cerr<<"Error: "<<strerror(errno)<<std::endl;
return false;
}
//
//Read from file
//
//Part 2
//Line format:
//${current_price} ${current holding} ${buying cost} ${selling cost} ${variable buying cost} ${variable selling cost} ${min_buy} ${min_sell} ${max_buy} ${max_sell}
//
while( std::getline(data, input_coach)){
std::istringstream line_coach(input_coach);
if(line == 1){
line_coach>>
constraint.num_assets>>
constraint.max_assets>>
constraint.transaction_limit>>
constraint.cash_change;
line++;
continue;
}
if(line<=constraint.num_assets+1){
struct asset assetInput;
assetInput.id = id;
id++;
line_coach>>assetInput.current_price>>
assetInput.holding>>
assetInput.cost_buy>>
assetInput.cost_sell>>
assetInput.vcost_buy>>
assetInput.vcost_sell>>
assetInput.min_buy>>
assetInput.min_sell>>
assetInput.max_buy>>
assetInput.max_sell;
if(assetInput.max_sell != 0)
assetInput.sellable = true;
assetArray.push_back(assetInput);
}
//
//Part 3
//Asset properties
//${mean_return} ${standard deviation of return}
if(line>constraint.num_assets+1&&line<=(2*constraint.num_assets+1)){
int tran = line - constraint.num_assets - 2;
line_coach>>assetArray[tran].mean_income>>
assetArray[tran].diviation_r;
}
//
//Part 4
//Correlation
if(line>(2*constraint.num_assets +1)){
int i,j = 0;
double corr_buffer = 0;
line_coach>>i>>j>>corr_buffer;
correlation[i-1][j-1] = corr_buffer;
}
line++;
}
data.close();
return true;
}
#endif