4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
12 #include <sys/types.h>
15 #include "lpp_remote.h"
20 #define WIN32_LEAN_AND_MEAN
23 #define snprintf _snprintf
24 #define chmod(a, b) _chmod(a, b)
26 /* disable warning: 'foo' was declared deprecated, use 'bla' instead */
27 /* of course MS had to make 'bla' incompatible to 'foo', so a simple */
28 /* define will not work :-((( */
29 #pragma warning( disable : 4996 )
33 /* CPLEX-account related stuff */
34 #define DELETE_FILES /**< deletes all dumped files after use. Files on server are always deleted. */
35 #define SSH_USER_HOST "kb61@sp-smp.rz.uni-karlsruhe.de"
36 #define SSH_PASSWD_FILE "/ben/daniel/.smppw"
37 #define EXPECT_FILENAME "runme" /* name of the expect-script */
39 static FILE *ffopen(const char *base, const char *ext, const char *mode) {
43 snprintf(buf, sizeof(buf), "%s.%s", base, ext);
44 if (! (out = fopen(buf, mode))) {
45 fprintf(stderr, "Cannot open file %s in mode %s\n", buf, mode);
51 static void lpp_write_cmd(lpp_t *lpp) {
52 FILE *out = ffopen(lpp->name, "cmd", "wt");
53 fprintf(out, "set logfile %s.sol\n", lpp->name);
54 fprintf(out, "set mip strategy mipstart 1\n");
55 // fprintf(out, "set mip emphasis 3\n"); /* moving best bound */
56 fprintf(out, "set mip emphasis 0\n"); /* balance optimality and feasability */
57 fprintf(out, "set mip strategy variableselect 3\n"); /* strong branching */
58 fprintf(out, "read %s.mps\n", lpp->name);
59 fprintf(out, "read %s.mst\n", lpp->name);
60 fprintf(out, "optimize\n");
61 fprintf(out, "display solution variables -\n");
62 fprintf(out, "quit\n");
66 static void lpp_write_exp(lpp_t *lpp) {
70 pwfile = fopen(SSH_PASSWD_FILE, "rt");
71 fgets(passwd, sizeof(passwd), pwfile);
74 out = ffopen(EXPECT_FILENAME, "exp", "wt");
75 fprintf(out, "#! /usr/bin/expect\n");
76 fprintf(out, "spawn scp %s.mps %s.mst %s.cmd %s:\n", lpp->name, lpp->name, lpp->name, SSH_USER_HOST); /* copy problem files */
77 fprintf(out, "expect \"word:\"\nsend \"%s\\n\"\ninteract\n", passwd);
79 fprintf(out, "spawn ssh %s \"./cplex90 < %s.cmd\"\n", SSH_USER_HOST, lpp->name); /* solve */
80 fprintf(out, "expect \"word:\"\nsend \"%s\\n\"\ninteract\n", passwd);
82 fprintf(out, "spawn scp %s:%s.sol .\n", SSH_USER_HOST, lpp->name); /*copy back solution */
83 fprintf(out, "expect \"word:\"\nsend \"%s\\n\"\ninteract\n", passwd);
85 fprintf(out, "spawn ssh %s ./dell\n", SSH_USER_HOST); /* clean files on server */
86 fprintf(out, "expect \"word:\"\nsend \"%s\\n\"\ninteract\n", passwd);
90 static void lpp_read_solution(lpp_t *lpp) {
98 if (!(in = ffopen(lpp->name, "sol", "rt"))) {
99 lpp->sol_state = lpp_unknown;
104 fgets(buf, sizeof(buf), in);
106 /* error and solution state */
107 if (!strncmp(buf, "CPLEX Error", 11))
108 lpp->error = strdup(buf);
109 else if (!strncmp(buf, "Warning:", 8))
110 lpp->error = strdup(buf);
111 else if (!strncmp(buf, "Integer optimal solution:", 25))
112 lpp->sol_state = lpp_optimal;
113 else if (!strcmp(buf, "No integer feasible solution exists."))
114 lpp->sol_state = lpp_infeasible;
115 else if (!strcmp(buf, "Error termination, integer feasible:"))
116 lpp->sol_state = lpp_feasible;
118 else if (sscanf(buf, "Solution time = %lg sec. Iterations = %u", &sol_time, &iter) == 2) {
119 lpp->sol_time = sol_time;
120 lpp->iterations = iter;
122 /* variable values */
123 else if(!strcmp(buf, "Variable Name Solution Value")) {
126 for(i=0; i<lpp->var_next; ++i) {
127 lpp_name_t *var = lpp->vars[i];
129 var->value_kind = lpp_value_solution;
132 else if(!strncmp(buf, "All other var", 13))
134 else if (vars_section) {
135 if (sscanf(buf, "%s %lg", var_name, &var_value) == 2)
136 lpp->vars[lpp_get_var_idx(lpp, var_name)]->value = var_value;
138 assert(0 && "There should be variables to read in!");
143 printf("\n%s\n", lpp->error);
149 static void lpp_delete_files(lpp_t *lpp) {
151 int end = snprintf(buf, sizeof(buf), "%s", lpp->name);
153 snprintf(buf+end, sizeof(buf)-end, ".mps");
155 snprintf(buf+end, sizeof(buf)-end, ".mst");
157 snprintf(buf+end, sizeof(buf)-end, ".cmd");
159 snprintf(buf+end, sizeof(buf)-end, ".sol");
161 remove(EXPECT_FILENAME ".exp");
165 void lpp_solve_remote(lpp_t *lpp) {
167 out = ffopen(lpp->name, "mps", "wt");
168 mps_write_mps(lpp, s_mps_free, out);
171 out = ffopen(lpp->name, "mst", "wt");
172 mps_write_mst(lpp, s_mps_free, out);
178 /* call the expect script */
179 chmod(EXPECT_FILENAME ".exp", 0700);
180 system(EXPECT_FILENAME ".exp");
182 lpp_read_solution(lpp);
184 lpp_delete_files(lpp);