#include <stdlib.h>
#include <stdio.h>

#include "../spnbox.h" 

// REAL is defined as 'double' in lp_types.h

//   [r, kind] = ipslv(f, L, B, intlist, vub, vlb, ctype); 

void test_ipslv(REAL* f1, REAL L1[], REAL* B1, int m1, int n1, int* intlist1, REAL* ub1, REAL* lb1, int* ctype1) {

  int i, j;
  REAL** L;
  ipslv_r out;

  L = (double**) calloc(m1, sizeof(*L));
  for(i = 0; i < m1; i++) {
    L[i] = (double*) calloc(n1, sizeof(**L));
    for(j = 0; j < n1; j++)
      L[i][j] = L1[i*n1 + j];
  }
  out = ipslv(f1, L, B1, m1, n1, intlist1, ub1, lb1, ctype1);
  for(i = 0; i < m1; i++)
    free(L[i]);
  free(L);

  printf("\nReturn value %d (",out.kind);
  switch(out.kind) {
  case 0: printf("OPTIMAL"); break;
  case 1: printf("MILP_FAIL"); break;
  case 2: printf("INFEASIBLE"); break;
  case 3: printf("UNBOUNDED"); break;
  case 4: printf("FAILURE"); break;
  default: printf("UNKNOWN");
  }
  printf(")");
  if(out.solution) {
    printf("\n\nSolution:\n[");
    for(i = 0; i < n1; i++)
      printf("%g ", (double)out.solution[i]);
    printf("]\n\n");
  }

}

int main() {

  int i, j;

  /* Example 1 */
  // minimize 0.1x + 1.2y subject to 3x + 2y = 5 and x + 4y <= 5
  REAL f1[] = {0.1, 1.2}; 
  REAL L1[] = {3, 2, \
               1, 4};     
  REAL B1[] = {5, 5};
  int m1 = 2, n1 = 2;
  int intlist1[] = {1, 1};
  REAL ub1[] = {1, INF};
  REAL lb1[] = {-INF, 1};
  int ctype1[] = {0, -1}; // =, <=

  printf("\nExample 1\n");
  test_ipslv(f1, L1, B1, m1, n1, intlist1, ub1, lb1, ctype1);

  return 0;
}
