forked from sassoftware/cvrpsep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glmsep.cpp
213 lines (174 loc) · 4.9 KB
/
glmsep.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
/* SAS modified this file. */
/* (C) Copyright 2003 Jens Lysgaard. All rights reserved. */
/* OSI Certified Open Source Software */
/* This software is licensed under the Common Public License Version 1.0 */
#include <stdlib.h>
#include <stdio.h>
#include "memmod.h"
#include "basegrph.h"
#include "cnstrmgr.h"
#include "mxf.h"
void GLM_IdentifySingleSet(ReachPtr SupportPtr,
int *SmallGamma,
int BigGamma,
int NoOfCustomers,
double **XMatrix,
int *NodeList,
int *NodeListSize)
{
int i,j,k,DepotIdx;
int GraphNodes,ArcCap,CapFromSource;
double XVal,XijFactor,ScaleFactor,WeightedSum,MaxFlowValue;
double SGi, SGj, SGk, BG;
int *SourceNodeList;
double *DepotEdgeXVal;
int SourceNodeListSize;
MaxFlowPtr MXFPtr;
ScaleFactor = 1000.0;
BG = BigGamma;
GraphNodes = (NoOfCustomers + 2);
MXF_InitMem(&MXFPtr,GraphNodes,GraphNodes*5);
MXF_ClearNodeList(MXFPtr);
MXF_SetNodeListSize(MXFPtr,GraphNodes);
MXF_ClearArcList(MXFPtr);
SourceNodeList = MemGetIV(NoOfCustomers+2);
DepotEdgeXVal = MemGetDV(NoOfCustomers+1);
for (i=1; i<=NoOfCustomers; i++)
{
for (j=1; j<=SupportPtr->LP[i].CFN; j++)
{
k = SupportPtr->LP[i].FAL[j];
if ((k <= NoOfCustomers) && (k > i))
{
XVal = XMatrix[i][k];
SGi = SmallGamma[i];
SGk = SmallGamma[k];
XijFactor = 1.0 - ((SGi + SGk) / BG);
XVal *= XijFactor;
ArcCap = (int)(XVal * ScaleFactor);
MXF_AddArc(MXFPtr,i,k,ArcCap);
MXF_AddArc(MXFPtr,k,i,ArcCap);
}
}
}
DepotIdx = NoOfCustomers + 1;
for (k=1; k<=NoOfCustomers; k++) DepotEdgeXVal[k] = 0.0;
for (j=1; j<=SupportPtr->LP[DepotIdx].CFN; j++)
{
k = SupportPtr->LP[DepotIdx].FAL[j];
DepotEdgeXVal[k] = XMatrix[DepotIdx][k];
}
CapFromSource = 0;
for (i=1; i<=NoOfCustomers; i++)
{
SGi = SmallGamma[i];
XVal = (1.0 - (SGi / BG)) * DepotEdgeXVal[i];
WeightedSum = 0.0;
for (k=1; k<=SupportPtr->LP[i].CFN; k++)
{
j = SupportPtr->LP[i].FAL[k];
if (j <= NoOfCustomers)
{
SGj = SmallGamma[j];
WeightedSum += (SGj / BG) * XMatrix[i][j];
}
}
XVal -= WeightedSum;
ArcCap = (int)(XVal * ScaleFactor);
if (ArcCap > 0)
{
MXF_AddArc(MXFPtr,i,NoOfCustomers+2,ArcCap);
}
else
{
ArcCap = -ArcCap;
ArcCap++;
MXF_AddArc(MXFPtr,NoOfCustomers+1,i,ArcCap);
CapFromSource += ArcCap;
}
}
if (CapFromSource > 0)
{
MXF_CreateMates(MXFPtr);
MXF_SolveMaxFlow(MXFPtr,1,NoOfCustomers+1,NoOfCustomers+2,&MaxFlowValue,0,
&SourceNodeListSize,SourceNodeList);
SourceNodeListSize--;
}
else
{
SourceNodeListSize = 0;
}
for (i=1; i<=SourceNodeListSize; i++) NodeList[i] = SourceNodeList[i];
*NodeListSize = SourceNodeListSize;
MemFree(SourceNodeList);
MemFree(DepotEdgeXVal);
MXF_FreeMem(MXFPtr);
}
void GLMSEP_SeparateGLM(int NoOfCustomers,
int *Demand,
int CAP,
int NoOfEdges,
int *EdgeTail,
int *EdgeHead,
double *EdgeX,
int *CustList,
int *CustListSize,
double *Violation)
{
int i,j;
double LHS,RHS;
char *InNodeSet;
double **XMatrix;
ReachPtr SupportPtr;
ReachInitMem(&SupportPtr,NoOfCustomers+1);
XMatrix = MemGetDM(NoOfCustomers+2,NoOfCustomers+2);
for (i=1; i<=NoOfCustomers+1; i++)
for (j=1; j<=NoOfCustomers+1; j++)
XMatrix[i][j] = 0.0;
for (i=1; i<=NoOfEdges; i++)
{
ReachAddForwArc(SupportPtr,EdgeTail[i],EdgeHead[i]);
ReachAddForwArc(SupportPtr,EdgeHead[i],EdgeTail[i]);
XMatrix[EdgeTail[i]][EdgeHead[i]] = EdgeX[i];
XMatrix[EdgeHead[i]][EdgeTail[i]] = EdgeX[i];
}
/* Ready to call GLM separation */
GLM_IdentifySingleSet(SupportPtr,Demand,CAP,NoOfCustomers,
XMatrix,CustList,CustListSize);
if (*CustListSize > 0)
{
InNodeSet = MemGetCV(NoOfCustomers+1);
for (i=1; i<=NoOfCustomers; i++) InNodeSet[i] = 0;
for (i=1; i<=*CustListSize; i++)
{
j = CustList[i];
InNodeSet[j] = 1;
}
LHS = 0.0;
RHS = 0.0;
for (i=1; i<=NoOfCustomers; i++)
if (InNodeSet[i])
{
LHS += (CAP * XMatrix[i][NoOfCustomers+1]);
RHS += (2 * Demand[i]);
}
for (i=1; i<=NoOfCustomers; i++)
if (InNodeSet[i])
{
for (j=1; j<=NoOfCustomers; j++)
if (InNodeSet[j] == 0)
{
LHS += ((CAP - (2*Demand[j])) * XMatrix[i][j]);
}
}
*Violation = (RHS - LHS) / (1.0 * CAP);
if (*Violation < 0.02)
{
*CustListSize = 0;
*Violation = 0.0;
}
MemFree(InNodeSet);
}
MemFreeDM(XMatrix,NoOfCustomers+2);
ReachFreeMem(&SupportPtr);
}