1 | /* HeuristicLab
|
---|
2 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
3 | *
|
---|
4 | * This file is part of HeuristicLab.
|
---|
5 | *
|
---|
6 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation, either version 3 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU General Public License
|
---|
17 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | /* Source based on lrslib's chdemo.c
|
---|
21 | Copyright: David Avis 2001, avis@cs.mcgill.ca
|
---|
22 | Licensed under the GNU GPL
|
---|
23 | */
|
---|
24 |
|
---|
25 | #include <stdio.h>
|
---|
26 | #include <string.h>
|
---|
27 | #include "extfunc.h"
|
---|
28 |
|
---|
29 | __declspec(dllexport) double calculate_volume(int numPoints, int dimension, long *num, long *den) {
|
---|
30 | double result = 0.0;
|
---|
31 |
|
---|
32 | lrs_dic *P; /* structure for holding current dictionary and indices */
|
---|
33 | lrs_dat *Q; /* structure for holding static problem data */
|
---|
34 | lrs_mp_vector output; /* one line of output:ray,vertex,facet,linearity */
|
---|
35 | lrs_mp_matrix Lin; /* holds input linearities if any are found */
|
---|
36 |
|
---|
37 | long i;
|
---|
38 | long col; /* output column index for dictionary */
|
---|
39 |
|
---|
40 | printf("\nnumPoints: %d", numPoints);
|
---|
41 | printf("\ndimension: %d", dimension);
|
---|
42 | printf("\nnum: %d", num[11]);
|
---|
43 | printf("\nden: %d", den[11]);
|
---|
44 | printf("\nsizeofint: %d", sizeof(int));
|
---|
45 | printf("\nsizeoflong: %d", sizeof(long));
|
---|
46 |
|
---|
47 | /* Global initialization - done once */
|
---|
48 | if ( !lrs_init ("\nHeuristicLab lrslib wrapper"))
|
---|
49 | return -1.0;
|
---|
50 |
|
---|
51 | /* allocate and init structure for static problem data */
|
---|
52 | Q = lrs_alloc_dat ("LRS globals");
|
---|
53 | if (Q == NULL)
|
---|
54 | return -1.0;
|
---|
55 |
|
---|
56 | /* now flags in lrs_dat can be set */
|
---|
57 | Q->m=numPoints; /* number of input rows = number of vertices */
|
---|
58 | Q->n=dimension; /* number of input columns */
|
---|
59 | Q->hull = TRUE; /* convex hull problem: facet enumeration */
|
---|
60 | Q->polytope= TRUE; /* input is a polytope */
|
---|
61 | Q->getvolume= TRUE; /* compute the volume */
|
---|
62 |
|
---|
63 | output = lrs_alloc_mp_vector (Q->n);
|
---|
64 |
|
---|
65 |
|
---|
66 | P = lrs_alloc_dic (Q); /* allocate and initialize lrs_dic */
|
---|
67 | if (P == NULL)
|
---|
68 | return -1.0;
|
---|
69 |
|
---|
70 | /* Build polytope */
|
---|
71 | fill_data(P,Q, num, den);
|
---|
72 |
|
---|
73 | /* code from here is borrowed from lrs_main */
|
---|
74 |
|
---|
75 | /* Pivot to a starting dictionary */
|
---|
76 | if (!lrs_getfirstbasis (&P, Q, &Lin, FALSE))
|
---|
77 | return -1.0;
|
---|
78 |
|
---|
79 | /* We initiate reverse search from this dictionary */
|
---|
80 | /* getting new dictionaries until the search is complete */
|
---|
81 | /* User can access each output line from output which is */
|
---|
82 | /* vertex/ray/facet from the lrs_mp_vector output */
|
---|
83 | do
|
---|
84 | {
|
---|
85 | for (col = 0; col <= P->d; col++)
|
---|
86 | if (lrs_getsolution (P, Q, output, col))
|
---|
87 | lrs_printoutput (Q, output);
|
---|
88 | }
|
---|
89 | while (lrs_getnextbasis (&P, Q, FALSE));
|
---|
90 | result = get_volume(P,Q);
|
---|
91 |
|
---|
92 | lrs_printtotals (P, Q); /* print final totals */
|
---|
93 |
|
---|
94 | /* free space : do not change order of next 3 lines! */
|
---|
95 | lrs_clear_mp_vector (output, Q->n);
|
---|
96 | lrs_free_dic (P,Q); /* deallocate lrs_dic */
|
---|
97 | lrs_free_dat (Q); /* deallocate lrs_dat */
|
---|
98 | lrs_close ("HeuristicLab lrslib wrapper");
|
---|
99 |
|
---|
100 | return result;
|
---|
101 | }
|
---|
102 |
|
---|
103 | double get_volume (lrs_dic *P, lrs_dat *Q) {
|
---|
104 | double vol;
|
---|
105 |
|
---|
106 | if (Q->getvolume)
|
---|
107 | {
|
---|
108 | rescalevolume (P, Q, Q->Nvolume, Q->Dvolume);
|
---|
109 |
|
---|
110 | if (Q->polytope){
|
---|
111 | prat ("\n*Volume=", Q->Nvolume, Q->Dvolume);
|
---|
112 | } else {
|
---|
113 | prat ("\n*Pseudovolume=", Q->Nvolume, Q->Dvolume);
|
---|
114 | }
|
---|
115 |
|
---|
116 | rattodouble (Q->Nvolume, Q->Dvolume, &vol);
|
---|
117 | return vol;
|
---|
118 | }
|
---|
119 | return -1.0;
|
---|
120 | }
|
---|
121 |
|
---|
122 | void fill_data (lrs_dic *P, lrs_dat *Q, long *num, long *den)
|
---|
123 | {
|
---|
124 | int i, j;
|
---|
125 | long *tmpNum = (long*)malloc(sizeof(long)* Q->n);
|
---|
126 | long *tmpDen = (long*)malloc(sizeof(long)* Q->n);
|
---|
127 |
|
---|
128 | for (i=0;i<Q->m;i++)
|
---|
129 | {
|
---|
130 | for(j=0;j<Q->n;j++) {
|
---|
131 | tmpNum[j] = num[i*Q->n+j];
|
---|
132 | tmpDen[j] = den[i*Q->n+j];
|
---|
133 | }
|
---|
134 | lrs_set_row(P,Q,i+1,tmpNum,tmpDen,GE);
|
---|
135 | }
|
---|
136 | free(tmpNum);
|
---|
137 | free(tmpDen);
|
---|
138 | }
|
---|
139 |
|
---|