forked from Tian-Xie/NLS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNestedLogitModel.h
More file actions
86 lines (73 loc) · 1.79 KB
/
NestedLogitModel.h
File metadata and controls
86 lines (73 loc) · 1.79 KB
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
/*
NLS: NestedLogitSolver
Constrained Assortment Optimization Solver for Nested Logit Model
Author: Tian Xie (SHUFE)
Date: Oct 19, 2017
*/
#ifndef _NESTEDLOGITMODEL_H
#define _NESTEDLOGITMODEL_H
#include <vector>
#include <utility>
#include <math.h>
#include <time.h>
using std::vector;
struct TProduct
{
double V, R;
TProduct(double _V, double _R): V(_V), R(_R) {}
};
struct TCandidate
{
double V, RV;
double Intercept, Slope; // Intercept - Slope * u
TCandidate() {}
TCandidate(double _V, double _RV, double _Intercept, double _Slope) : V(_V), RV(_RV), Intercept(_Intercept), Slope(_Slope) {}
bool operator < (const TCandidate& b);
};
typedef std::pair <int, double> TPiece;
struct TNest
{
int nProduct;
double Gamma;
vector <TProduct> Product;
TCandidate** Candidate; // It may cost O(nProduct^2) to storage
int* nCandidate;
int nnCandidate;
TPiece** Piece; // Save the piecewise-linear form
int* nPiece;
int nnPiece;
TNest(double _Gamma): Gamma(_Gamma)
{
nProduct = 0;
Product.clear();
Candidate = NULL;
nCandidate = NULL;
nnCandidate = 0;
Piece = NULL;
nPiece = NULL;
nnPiece = 0;
}
bool InsertProduct(TProduct p);
void Clear();
void GenerateCandidateSet();
void GeneratePiecewiseLinear(int C);
~TNest();
};
struct NestedLogitModel
{
int nNest; // How many nests?
vector <TNest> Nest;
double V0;
NestedLogitModel();
NestedLogitModel(double _V0);
NestedLogitModel(const NestedLogitModel& MOD);
void SetV0(double _V0);
void Reset();
int InsertNest(double _Gamma);
int InsertProduct(int NestID, double v, double r);
void RandomCase(int nNest, int nProduct, time_t seed);
void GenerateAllCandidateSet();
void GenerateAllPiecewiseLinear();
~NestedLogitModel();
};
#endif