-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNestedLogitModel.cpp
More file actions
125 lines (110 loc) · 2.33 KB
/
Copy pathNestedLogitModel.cpp
File metadata and controls
125 lines (110 loc) · 2.33 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
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
/*
NLS: NestedLogitSolver
Constrained Assortment Optimization Solver for Nested Logit Model
Author: Tian Xie (SHUFE)
Date: Oct 19, 2017
*/
#include "NestedLogitModel.h"
#include <random>
#include <time.h>
bool TNest::InsertProduct(TProduct p)
{
if (p.R < 0 || p.V < 0) return false;
nProduct ++;
Product.push_back(p);
return true;
}
void TNest::Clear()
{
nProduct = 0;
Product.clear();
if (Candidate)
{
for (int nnc = 0; nnc < nnCandidate; nnc ++)
delete Candidate[nnc];
delete Candidate;
delete nCandidate;
nnCandidate = 0;
nCandidate = NULL;
Candidate = NULL;
}
if (Piece)
{
for (int nnp = 0; nnp < nnPiece; nnp ++)
if (nPiece[nnp]) delete Piece[nnp];
delete Piece;
delete nPiece;
nnPiece = 0;
nPiece = NULL;
Piece = NULL;
}
}
TNest::~TNest()
{
Clear();
}
NestedLogitModel::NestedLogitModel()
{
Reset();
SetV0(1.0);
}
NestedLogitModel::NestedLogitModel(double _V0)
{
Reset();
SetV0(_V0);
}
NestedLogitModel::NestedLogitModel(const NestedLogitModel& MOD)
{
nNest = MOD.nNest;
Nest = MOD.Nest;
V0 = MOD.V0;
}
void NestedLogitModel::SetV0(double _V0)
{
V0 = _V0;
}
void NestedLogitModel::Reset()
{
for (int t = 0; t < Nest.size(); t ++)
Nest[t].Clear();
nNest = 0;
Nest.clear();
}
int NestedLogitModel::InsertNest(double _Gamma)
{
if (_Gamma <= 0 || _Gamma > 1) return -1;
Nest.push_back(TNest(_Gamma));
return nNest ++;
}
int NestedLogitModel::InsertProduct(int NestID, double v, double r)
{
if (NestID >= nNest) return -1;
if (v < 0) return -1;
if (r < 0) return -1;
Nest[NestID].InsertProduct(TProduct(v, r));
return Nest[NestID].nProduct - 1;
}
void NestedLogitModel::RandomCase(int nNest, int nProduct, time_t seed)
{
Reset();
SetV0(nNest * 10.0);
std::uniform_real_distribution <double> RandGamma(0.25, 0.75);
std::uniform_real_distribution <double> RandC(0.0, 1.0);
std::uniform_real_distribution <double> RandXY(0.75, 1.25);
std::mt19937 Gen(seed);
for (int i = 0; i < nNest; i ++)
{
InsertNest(RandGamma(Gen));
for (int j = 0; j < nProduct; j ++)
{
double C = RandC(Gen);
double r = 10.0 * C * C * RandXY(Gen);
double v = 10.0 * (1.0 - C) * RandXY(Gen);
InsertProduct(i, v, r);
}
}
}
NestedLogitModel::~NestedLogitModel()
{
Reset();
}