-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMetricUpgrade.cpp
192 lines (167 loc) · 5.95 KB
/
MetricUpgrade.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
/*****************************************************************************************************************************
* Copyright:
*
* ------ COPYRIGHT NOTICE, CONDITIONS, AND DISCLAIMER START WITH THIS LINE -----------------
*
* Copyright (c) 2010-2013 Arizona Board of Regents. All Rights Reserved.
*
* Contact: Lina Karam (karam@asu.edu), Aditee Shrotre (ashrotre@asu.edu), Tejas Borkar (tsborkar@asu.edu),
* Jinjin Li (jinjinli@asu.edu)
*
* Image, Video, and Usabilty (IVU) Lab, http://ivulab.asu.edu , Arizona State University
*
* DISCLAIMER:
*
* This software is provided by the copyright holders and contributors "as is" and any express or implied
* warranties, including, but not limited to, the implied warranties of merchantability and fitness for a
* particular purpose are disclaimed.
*
* In no event shall the Arizona Board of Regents, Arizona State University, IVU Lab members, authors or contributors
* be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not
* limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption)
* however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence
* or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.
*
* ------ COPYRIGHT NOTICE, CONDITIONS, AND DISCLAIMER END WITH THIS LINE -----------------
* ****************************************************************************************************************************/
// MetricUpgrade.cpp : Uses the sparsePOP (http://www.is.titech.ac.jp/~kojima/SparsePOP/) for metric upgrade.
#include <Windows.h>
#include <stdio.h>
#include <fstream>
#include <time.h>
#include "Common.h"
using namespace std;
using namespace cv;
extern string equation;
extern int equationReduce(vector<Mat>* P, string &equation, string& reducedEquation);
int MetricUpgrade(vector<Mat>* P_BA, Mat* QFinal)
{
clock_t start_time, end_time; // For timing the Metric Upgrade operation
start_time = clock();
// Reduce the equation
string funcF;
equationReduce(P_BA, equation, funcF);
ofstream eqfile("RedEquation.txt");
if ( !eqfile.is_open() )
{
cerr << "Could not open file MetricUpgrade.gms" << endl;
return -1;
}
eqfile << funcF << endl;
eqfile.close();
// replace (2) by 2
while (1)
{
size_t pos = funcF.find("(2)");
if (pos != string::npos)
{
funcF.replace(pos, strlen("(2)"), "2");
pos += strlen("(2)");
}
else
{
break;
}
}
// Read the reference GMS file
ifstream refFile ("MetricUpgradeRef.gms");
if ( !refFile.is_open() )
{
cerr << "Could not the Reference gms file." << endl;
return -1;
}
string fileText;
refFile.seekg(0, ios::end);
fileText.resize(refFile.tellg());
refFile.seekg(0, ios::beg);
refFile.read(&fileText[0], fileText.size());
refFile.close();
// Write to the GMS file and append the objective function
ofstream gmsFile("MetricUpgrade.gms");
if ( !gmsFile.is_open() )
{
cerr << "Could not open file MetricUpgrade.gms" << endl;
return -1;
}
gmsFile << fileText;
gmsFile.seekp(0, ios::end);
gmsFile << "\n\ne1.. " << endl;
gmsFile << funcF;
gmsFile << "- objvar =E= 0;" << endl;
gmsFile.close();
//cout << "Created the GMS file.\n";
//Create a process for sparsePOP
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if( remove( "MetricUpgradeOut.txt" ) != 0 )
perror( "Error deleting file" );
cout << "Creating Process for sparsePOP.\n";
if ( !CreateProcess( TEXT("sparsePOP.exe"),
TEXT("sparsePOP.exe MetricUpgrade.gms"),
NULL, NULL, FALSE, 0,
NULL, NULL, &si, &pi)
)
{
cerr << "Failed to Create a Process.\n";
return -1;
}
// Wait for the process to complete and exit.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close the process and thread handles.
CloseHandle (pi.hProcess);
CloseHandle (pi.hThread);
// Read the elements of Q from the output File
ifstream outFile ("MetricUpgradeOut.txt");
if ( !outFile.is_open() )
{
cerr << "Could not the Output Text file.\n" << endl;
return -1;
}
string outText;
outFile.seekg(0, ios::end);
outText.resize(outFile.tellg());
outFile.seekg(0, ios::beg);
outFile.read(&outText[0], outText.size());
outFile.close();
// Extract and assign values
size_t solPos = outText.find("# Approximate optimal solution information:", 0);
solPos += strlen("# Approximate optimal solution information:");
// Find q
double q[10] = {0.0};
for ( int i = 0; i < 10; i++ )
{
char pattern[10];
sprintf(pattern, "%d: ", i+1);
size_t valPos = outText.find(pattern, solPos);
valPos += strlen(pattern);
size_t endPos = outText.find(" ", valPos);
string foundText = outText.substr(valPos, endPos - valPos);
q[i] = (double)atof(foundText.c_str());
}
// From the Final Q Matrix to be returned
//[q1 q2 q3 q4
// q2 q5 q6 q7
// q3 q6 q8 q9
// q4 q7 q9 q10]
for ( int row = 0, i = 0; row < 4; row++ )
{
for ( int col = 0; col < 4; col++ )
{
if ( col >= row )
{
QFinal->at<double>(row,col) = q[i];
i++;
}
else
{
QFinal->at<double>(row,col) = QFinal->at<double>(col,row);
}
}
}
end_time = clock();
cout << "Metric Upgrade Run Time: " << ((double) (end_time - start_time) / CLOCKS_PER_SEC) << endl;
return 1;
}