PGROUTING  3.2
TSP.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: newTSP.c
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2015 Celia Virginia Vergara Castillo
11 
12 ------
13 
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18 
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23 
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 
28  ********************************************************************PGR-GNU*/
29 
31 #include "utils/array.h"
32 
33 #include "c_common/debug_macro.h"
34 #include "c_common/e_report.h"
35 #include "c_common/time_msg.h"
36 
38 #include "drivers/tsp/TSP_driver.h"
39 
40 
41 
42 PGDLLEXPORT Datum _pgr_tsp(PG_FUNCTION_ARGS);
43 
44 /******************************************************************************/
45 /* MODIFY AS NEEDED */
46 static
47 void
49  char* distances_sql,
50  int64_t start_vid,
51  int64_t end_vid,
52 
53  double time_limit,
54 
55  int64_t tries_per_temperature,
56  int64_t max_changes_per_temperature,
57  int64_t max_consecutive_non_changes,
58 
59  double initial_temperature,
60  double final_temperature,
61  double cooling_factor,
62 
63  bool randomize,
64 
65  General_path_element_t **result_tuples,
66  size_t *result_count) {
68 
69  /*
70  * errors in parameters
71  */
72 
73  if (initial_temperature < final_temperature) {
74  elog(ERROR, "Condition not met: initial_temperature"
75  " > final_temperature");
76  }
77  if (final_temperature <= 0) {
78  elog(ERROR, "Condition not met: final_temperature > 0");
79  }
80  if (cooling_factor <=0 || cooling_factor >=1) {
81  elog(ERROR, "Condition not met: 0 < cooling_factor < 1");
82  }
83  if (tries_per_temperature < 0) {
84  elog(ERROR, "Condition not met: tries_per_temperature >= 0");
85  }
86  if (max_changes_per_temperature < 1) {
87  elog(ERROR, "Condition not met: max_changes_per_temperature > 0");
88  }
89  if (max_consecutive_non_changes < 1) {
90  elog(ERROR, "Condition not met: max_consecutive_non_changes > 0");
91  }
92  if (time_limit < 0) {
93  elog(ERROR, "Condition not met: max_processing_time >= 0");
94  }
95 
96 
97  Matrix_cell_t *distances = NULL;
98  size_t total_distances = 0;
99  pgr_get_matrixRows(distances_sql, &distances, &total_distances);
100 
101  if (total_distances == 0) {
102  PGR_DBG("No distances found");
103  (*result_count) = 0;
104  (*result_tuples) = NULL;
105  pgr_SPI_finish();
106  return;
107  }
108 
109 
110  PGR_DBG("Starting timer");
111  clock_t start_t = clock();
112  char* log_msg = NULL;
113  char* notice_msg = NULL;
114  char* err_msg = NULL;
115 
116  do_pgr_tsp(
117  distances, total_distances,
118  start_vid,
119  end_vid,
120  initial_temperature,
121  final_temperature,
122  cooling_factor,
123  tries_per_temperature,
124  max_changes_per_temperature,
125  max_consecutive_non_changes,
126  randomize,
127  time_limit,
128  result_tuples,
129  result_count,
130  &log_msg,
131  &notice_msg,
132  &err_msg);
133 
134  time_msg("TSP", start_t, clock());
135 
136  if (err_msg && (*result_tuples)) {
137  pfree(*result_tuples);
138  (*result_tuples) = NULL;
139  (*result_count) = 0;
140  }
141 
142  pgr_global_report(log_msg, notice_msg, err_msg);
143 
144  if (log_msg) pfree(log_msg);
145  if (notice_msg) pfree(notice_msg);
146  if (err_msg) pfree(err_msg);
147  if (distances) pfree(distances);
148 
149  pgr_SPI_finish();
150 }
151 /* */
152 /******************************************************************************/
153 
155 PGDLLEXPORT Datum
156 _pgr_tsp(PG_FUNCTION_ARGS) {
157  FuncCallContext *funcctx;
158  TupleDesc tuple_desc;
159 
160  /**************************************************************************/
161  /* MODIFY AS NEEDED */
162  /* */
163  General_path_element_t *result_tuples = NULL;
164  size_t result_count = 0;
165  /* */
166  /**************************************************************************/
167 
168  if (SRF_IS_FIRSTCALL()) {
169  MemoryContext oldcontext;
170  funcctx = SRF_FIRSTCALL_INIT();
171  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
172 
173 
174  /**********************************************************************/
175  /* MODIFY AS NEEDED */
176  /*
177 
178  CREATE OR REPLACE FUNCTION pgr_newTSP(
179  matrix_row_sql TEXT,
180  start_id BIGINT DEFAULT 0,
181  end_id BIGINT DEFAULT 0,
182 
183  max_processing_time FLOAT DEFAULT '+infinity'::FLOAT,
184 
185  tries_per_temperature INTEGER DEFAULT 500,
186  max_changes_per_temperature INTEGER DEFAULT 60,
187  max_consecutive_non_changes INTEGER DEFAULT 200,
188 
189  initial_temperature FLOAT DEFAULT 100,
190  final_temperature FLOAT DEFAULT 0.1,
191  cooling_factor FLOAT DEFAULT 0.9,
192 
193  randomize BOOLEAN DEFAULT true,
194  */
195 
196  process(
197  text_to_cstring(PG_GETARG_TEXT_P(0)),
198  PG_GETARG_INT64(1),
199  PG_GETARG_INT64(2),
200 
201  PG_GETARG_FLOAT8(3),
202 
203  PG_GETARG_INT32(4),
204  PG_GETARG_INT32(5),
205  PG_GETARG_INT32(6),
206 
207  PG_GETARG_FLOAT8(7),
208  PG_GETARG_FLOAT8(8),
209  PG_GETARG_FLOAT8(9),
210 
211  PG_GETARG_BOOL(10),
212  &result_tuples,
213  &result_count);
214  /* */
215  /**********************************************************************/
216 
217 #if PGSQL_VERSION > 95
218  funcctx->max_calls = result_count;
219 #else
220  funcctx->max_calls = (uint32_t)result_count;
221 #endif
222 
223  funcctx->user_fctx = result_tuples;
224  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
225  != TYPEFUNC_COMPOSITE) {
226  ereport(ERROR,
227  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
228  errmsg("function returning record called in context "
229  "that cannot accept type record")));
230  }
231 
232  funcctx->tuple_desc = tuple_desc;
233  MemoryContextSwitchTo(oldcontext);
234  }
235 
236  funcctx = SRF_PERCALL_SETUP();
237  tuple_desc = funcctx->tuple_desc;
238  result_tuples = (General_path_element_t*) funcctx->user_fctx;
239 
240  if (funcctx->call_cntr < funcctx->max_calls) {
241  HeapTuple tuple;
242  Datum result;
243  Datum *values;
244  bool* nulls;
245 
246  /**********************************************************************/
247  /* MODIFY AS NEEDED */
248  // OUT seq INTEGER,
249  // OUT node BIGINT,
250  // OUT cost FLOAT,
251  // OUT agg_cost FLOAT
252 
253  values = palloc(4 * sizeof(Datum));
254  nulls = palloc(4 * sizeof(bool));
255 
256 
257  size_t i;
258  for (i = 0; i < 4; ++i) {
259  nulls[i] = false;
260  }
261 
262  // postgres starts counting from 1
263  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
264  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
265  values[2] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
266  values[3] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
267  /**********************************************************************/
268 
269  tuple = heap_form_tuple(tuple_desc, values, nulls);
270  result = HeapTupleGetDatum(tuple);
271  SRF_RETURN_NEXT(funcctx, result);
272  } else {
273  SRF_RETURN_DONE(funcctx);
274  }
275 }
276 
pgr_get_matrixRows
void pgr_get_matrixRows(char *sql, Matrix_cell_t **rows, size_t *total_rows)
bigint start_vid, bigint end_vid, float agg_cost,
Definition: matrixRows_input.c:56
process
static void process(char *distances_sql, int64_t start_vid, int64_t end_vid, double time_limit, int64_t tries_per_temperature, int64_t max_changes_per_temperature, int64_t max_consecutive_non_changes, double initial_temperature, double final_temperature, double cooling_factor, bool randomize, General_path_element_t **result_tuples, size_t *result_count)
Definition: TSP.c:48
_pgr_tsp
PGDLLEXPORT Datum _pgr_tsp(PG_FUNCTION_ARGS)
Definition: TSP.c:156
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_tsp)
time_msg.h
postgres_connection.h
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
e_report.h
matrixRows_input.h
debug_macro.h
TSP_driver.h
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
General_path_element_t::node
int64_t node
Definition: general_path_element_t.h:41
General_path_element_t
Definition: general_path_element_t.h:37
if
if(DOXYGEN_FOUND) configure_file($
Definition: doxygen/CMakeLists.txt:13
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
do_pgr_tsp
void do_pgr_tsp(Matrix_cell_t *distances, size_t total_distances, int64_t start_vid, int64_t end_vid, double initial_temperature, double final_temperature, double cooling_factor, int64_t tries_per_temperature, int64_t max_changes_per_temperature, int64_t max_consecutive_non_changes, bool randomize, double time_limit, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: TSP_driver.cpp:45
matrix_cell
Definition: matrix_cell_t.h:37
General_path_element_t::cost
double cost
Definition: general_path_element_t.h:43
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
General_path_element_t::agg_cost
double agg_cost
Definition: general_path_element_t.h:44