PIPS
sc_simplex_feasibility.h File Reference

This header provides functions to test whether a constraint system is feasible, using the simplex method. More...

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <strings.h>
#include <limits.h>
#include "boolean.h"
#include "assert.h"
#include "vecteur.h"
#include "contrainte.h"
#include "sc.h"
+ Include dependency graph for sc_simplex_feasibility.h:

Go to the source code of this file.

Data Structures

struct  vec_s
 Type of vectors. More...
 
struct  constr_s
 Type of linear constraint. More...
 
struct  table_s
 Type of simplex tableau. More...
 
struct  vartbl_s
 Type of variable tables. More...
 

Macros

#define DEBUG(code)   {}
 

Functions

static void * safe_malloc (size_t size)
 
static bool sc_get_feasibility (Psysteme sys, int ofl_ctrl)
 Main Function. More...
 

Variables

Each variable is represented by a non-negative, integer value.

#define VAR_NULL   (-1)
 A special value used to represent the absence of variable. More...
 
#define VAR_MAXNB   (1971)
 Maximum number of variables. More...
 
#define var_fprint(stream, var)   (fprintf(stream, "x%d", var))
 Output var on stdio stream stream. More...
 
#define var_print(var)   (var_fprint(stdout, var))
 Output var on stdout. More...
 
typedef int var_t
 Type of variables. More...
 

Vectors

A vector is a structure to map each variable to a rational value.

#define VEC_NULL   NULL
 The empty vector. More...
 
#define vec_init(vec)   (vec = VEC_NULL)
 Set vec to be the empty vector. More...
 
#define vec_print(vec)   (vec_fprint(stdout, vec))
 Output vec on stdout. More...
 
typedef struct vec_s vec_s
 Type of vectors. More...
 
typedef struct vec_svec_p
 
static void vec_clear (vec_p *pvec)
 Free the space occupied by pvec. More...
 
static void vec_set (vec_p *pvec, vec_p vec)
 Copy vec into pvec. More...
 
static void vec_append_atfirst (vec_p *pvec, var_t var, qval_t coeff)
 Add the element (var, @coeff) to pvec, in first position. More...
 
static void vec_append (vec_p *pvec, var_t var, qval_t coeff)
 Add the element (var, coeff) to pvec. More...
 
static void vec_get_coeff (qval_t coeff, vec_p vec, var_t var)
 Get the value associated to var in vec, and store it into coeff. More...
 
static void vec_iadd (vec_p *pvec, vec_p vec)
 Set pvec to pvec + vec. More...
 
static void vec_imul (vec_p *pvec, qval_t coeff)
 Set pvec to coeff times pvec. More...
 
static void vec_iaddmul (vec_p *pvec, qval_t coeff, vec_p vec)
 Set pvec to pvec + coeff times vec. More...
 
static void vec_ineg (vec_p vec)
 Set vec to -vec. More...
 
static int NOWUNUSED vec_fprint (FILE *stream, vec_p vec)
 Output vec on stdio stream stream. More...
 

Linear Constraints

A linear constraint is either a linear equality (a1 x1 + ...

  • an xn = b) or a linear inequality (a1 x1 + ... + an xn <= b).
#define constr_get_coeff(coeff, constr, var)    (vec_get_coeff(coeff, (constr)->vec, var))
 Get the coefficient of var in constr, and store it into coeff. More...
 
#define constr_print(constr)   (constr_fprint(stdout, constr))
 Output constr on stdout. More...
 
enum  constrrel_t { CONSTR_EQ , CONSTR_LE }
 Type of linear constraint relations. More...
 
typedef struct constr_s constr_s
 Type of linear constraint. More...
 
typedef struct constr_sconstr_p
 
typedef constr_s constr_t[1]
 Type of linear constraint. More...
 
static void constr_init (constr_p constr)
 Initialize constr and set it to 0 = 0. More...
 
static void constr_clear (constr_p constr)
 Free the space occupied by constr. More...
 
static void NOWUNUSED constr_set (constr_p constr1, constr_p constr2)
 Copy constr2 into constr1. More...
 
static void constr_iadd (constr_p constr1, constr_p constr2)
 Set constr1 to constr1 + constr2. More...
 
static void constr_imul (constr_p constr, qval_t coeff)
 Set constr to coeff times constr. More...
 
static void constr_iaddmul (constr_p constr1, qval_t coeff, constr_p constr2)
 Set constr1 to constr1 + coeff times constr2. More...
 
static void constr_makepos (constr_p constr)
 Turn constr into an equivalent constraint whose constant term is non-negative. More...
 
static void constr_apply_pivot (constr_p constr, var_t pivot_var, constr_p pivot_constr)
 Make pivot_var coefficient null in constr, using pivot_constr. More...
 
static int NOWUNUSED constr_fprint (FILE *stream, constr_p constr)
 Output constr on stdio stream stream. More...
 

Simplex Tableau

A simplex tableau consists in a list of constraint, an objective function to minimize and its current value.

Those data evolve when the simplex is running.

#define table_get_nbvars(tbl)   ((tbl)->nbvars)
 Number of variables in tbl. More...
 
#define table_get_nbconstrs(tbl)   ((tbl)->nbconstrs)
 Number of constraints in tbl. More...
 
#define table_print(tbl)   (table_fprint(stdout, tbl))
 Output tbl on stdout. More...
 
typedef struct table_s table_s
 Type of simplex tableau. More...
 
typedef struct table_stable_p
 
typedef table_s table_t[1]
 Type of simplex tableau. More...
 
static void table_init (table_p tbl, int nbconstrs)
 Initialize tbl, with room for nbconstrs constraints. More...
 
static void table_clear (table_p tbl)
 Free the space occupied by tbl. More...
 
static void table_makepos (table_p tbl)
 Turn constraints into tbl into equivalent ones, whose constant term is non-negative. More...
 
static void table_addsignvars (table_p tbl)
 Ensure that all variables in tbl are non-negative. More...
 
static void table_addofsvars (table_p tbl)
 Ensure that all constraints in tbl are equalities. More...
 
static int NOWUNUSED table_fprint (FILE *stream, table_p tbl)
 Output tbl on stdio stream stream. More...
 
static void table_canonicalize (table_p tbl)
 Canonicalize tbl: ensure that all variables are non-negative, and all constraints are equalities whose constant term is non-negative. More...
 
static void table_set_obj (table_p tbl)
 Initialize the objective function and value from constraints in tbl. More...
 
static void table_addobjvars (table_p tbl)
 Add objective variables to each constraint of @tbl whose constant term is not zero. More...
 
static void table_prepare (table_p tbl)
 Canonicalize tbl, set the objective and add objective variables. More...
 
static var_t table_get_pivotvar (table_p tbl)
 Get the next pivot variable in @tbl. More...
 
static var_t table_get_assocvar (table_p tbl, int row)
 Retrieve the variable associated with the row (i.e. More...
 
static int table_get_pivotrow (table_p tbl, var_t var)
 
static bool table_get_pivot (table_p tbl, var_t *pvar, int *prow)
 Get the next pivot variable and row in @tbl, and store them in pvar and prow respectively. More...
 
static void table_apply_pivot (table_p tbl, var_t var, int row)
 Apply pivot (var, row) in tbl. More...
 
static void table_run_simplex (table_p tbl)
 Run simplex algorithm on @tbl. More...
 
static bool table_get_feasibility (table_p tbl)
 Determine whether the constraint system described in tbl is feasible, using simplex method. More...
 

Datatype Conversion

Here are utility functions to convert Linear types (Variable, Vecteur, Contrainte, Systeme) to datatypes used in this files.

In most of conversion functions, a variable table vartbl is passed, to help translating Linear named variables into indices.

#define vartbl_clear(vartbl)
 Free the space occupied by vartbl. More...
 
typedef struct vartbl_svartbl_p
 
typedef vartbl_s vartbl_t[1]
 Type of variable tables. More...
 
static void vartbl_init (vartbl_p vartbl)
 Initialize @vartbl to an empty table. More...
 
static var_t vartbl_find (vartbl_p vartbl, Variable name)
 Get the variable index associated to name, creating a new one if necessary. More...
 
static void vec_set_vecteur (vartbl_t vartbl, vec_p *pvec, Pvecteur vec)
 Copy vec into pvec. More...
 
static void constr_set_contrainte (vartbl_t vartbl, constr_p constr1, Pcontrainte constr2, bool is_ineq)
 Copy constr2 into constr1. More...
 
static void table_init_set_systeme (table_p tbl, Psysteme sys)
 Initialize tbl from sys. More...
 

Detailed Description

This header provides functions to test whether a constraint system is feasible, using the simplex method.

It can be used with any of the headers arith_fixprec.c or arith_mulprec.c, this in fixed- or multiple-precision.

Definition in file sc_simplex_feasibility.h.

Macro Definition Documentation

◆ constr_get_coeff

#define constr_get_coeff (   coeff,
  constr,
  var 
)     (vec_get_coeff(coeff, (constr)->vec, var))

Get the coefficient of var in constr, and store it into coeff.

Definition at line 415 of file sc_simplex_feasibility.h.

◆ constr_print

#define constr_print (   constr)    (constr_fprint(stdout, constr))

Output constr on stdout.

Definition at line 504 of file sc_simplex_feasibility.h.

◆ DEBUG

#define DEBUG (   code)    {}

Definition at line 50 of file sc_simplex_feasibility.h.

◆ table_get_nbconstrs

#define table_get_nbconstrs (   tbl)    ((tbl)->nbconstrs)

Number of constraints in tbl.

Definition at line 569 of file sc_simplex_feasibility.h.

◆ table_get_nbvars

#define table_get_nbvars (   tbl)    ((tbl)->nbvars)

Number of variables in tbl.

Definition at line 564 of file sc_simplex_feasibility.h.

◆ table_print

#define table_print (   tbl)    (table_fprint(stdout, tbl))

Output tbl on stdout.

Definition at line 961 of file sc_simplex_feasibility.h.

◆ var_fprint

#define var_fprint (   stream,
  var 
)    (fprintf(stream, "x%d", var))

Output var on stdio stream stream.

Definition at line 90 of file sc_simplex_feasibility.h.

◆ VAR_MAXNB

#define VAR_MAXNB   (1971)

Maximum number of variables.

Definition at line 85 of file sc_simplex_feasibility.h.

◆ VAR_NULL

#define VAR_NULL   (-1)

A special value used to represent the absence of variable.

Definition at line 80 of file sc_simplex_feasibility.h.

◆ var_print

#define var_print (   var)    (var_fprint(stdout, var))

Output var on stdout.

Definition at line 95 of file sc_simplex_feasibility.h.

◆ vartbl_clear

#define vartbl_clear (   vartbl)

Free the space occupied by vartbl.

Definition at line 998 of file sc_simplex_feasibility.h.

◆ vec_init

#define vec_init (   vec)    (vec = VEC_NULL)

Set vec to be the empty vector.

Definition at line 125 of file sc_simplex_feasibility.h.

◆ VEC_NULL

#define VEC_NULL   NULL

The empty vector.

Definition at line 120 of file sc_simplex_feasibility.h.

◆ vec_print

#define vec_print (   vec)    (vec_fprint(stdout, vec))

Output vec on stdout.

Definition at line 348 of file sc_simplex_feasibility.h.

Typedef Documentation

◆ constr_p

typedef struct constr_s * constr_p

◆ constr_s

typedef struct constr_s constr_s

Type of linear constraint.

◆ constr_t

typedef constr_s constr_t[1]

Type of linear constraint.

Definition at line 381 of file sc_simplex_feasibility.h.

◆ table_p

typedef struct table_s * table_p

◆ table_s

typedef struct table_s table_s

Type of simplex tableau.

The objective function and the associated value are stored into a constraint obj, other constraints are in the constraint table constr.

◆ table_t

typedef table_s table_t[1]

Type of simplex tableau.

Definition at line 531 of file sc_simplex_feasibility.h.

◆ var_t

typedef int var_t

Type of variables.

Definition at line 75 of file sc_simplex_feasibility.h.

◆ vartbl_p

typedef struct vartbl_s * vartbl_p

◆ vartbl_t

typedef vartbl_s vartbl_t[1]

Type of variable tables.

Definition at line 985 of file sc_simplex_feasibility.h.

◆ vec_p

typedef struct vec_s * vec_p

◆ vec_s

typedef struct vec_s vec_s

Type of vectors.

Typically, most of values are equal to zero, so a sparse, linked-list structure is used. Internally, variables are ordered by decreasing order.

Enumeration Type Documentation

◆ constrrel_t

Type of linear constraint relations.

Enumerator
CONSTR_EQ 

equality

CONSTR_LE 

inequality

Definition at line 363 of file sc_simplex_feasibility.h.

364 {
365  CONSTR_EQ, /**< equality */
366  CONSTR_LE /**< inequality */
367 } constrrel_t;
constrrel_t
Type of linear constraint relations.
@ CONSTR_LE
inequality
@ CONSTR_EQ
equality

Function Documentation

◆ constr_apply_pivot()

static void constr_apply_pivot ( constr_p  constr,
var_t  pivot_var,
constr_p  pivot_constr 
)
static

Make pivot_var coefficient null in constr, using pivot_constr.

pivot_constr must be an equality.

Definition at line 474 of file sc_simplex_feasibility.h.

476 {
477  qval_t coeff;
478  qval_init(coeff);
479  constr_get_coeff(coeff, constr, pivot_var);
480  qval_neg(coeff, coeff);
481  constr_iaddmul(constr, coeff, pivot_constr);
482  qval_clear(coeff);
483 }
qval_s qval_t[1]
Type of rational numbers.
#define qval_clear(q)
Free the space occupied by q.
#define qval_neg(q1, q2)
Set q1 to -q2.
#define qval_init(q)
Initialize q and set its value to 0/1.
#define constr_get_coeff(coeff, constr, var)
Get the coefficient of var in constr, and store it into coeff.
static void constr_iaddmul(constr_p constr1, qval_t coeff, constr_p constr2)
Set constr1 to constr1 + coeff times constr2.

References constr_get_coeff, constr_iaddmul(), qval_clear, qval_init, and qval_neg.

Referenced by table_apply_pivot().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ constr_clear()

static void constr_clear ( constr_p  constr)
static

Free the space occupied by constr.

Definition at line 396 of file sc_simplex_feasibility.h.

397 {
398  vec_clear(&constr->vec);
399  qval_clear(constr->cst);
400 }
static void vec_clear(vec_p *pvec)
Free the space occupied by pvec.
qval_t cst
constant term
vec_p vec
coefficients of variables

References constr_s::cst, qval_clear, constr_s::vec, and vec_clear().

Referenced by table_clear().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ constr_fprint()

static int NOWUNUSED constr_fprint ( FILE *  stream,
constr_p  constr 
)
static

Output constr on stdio stream stream.

Definition at line 488 of file sc_simplex_feasibility.h.

489 {
490  int c = vec_fprint(stream, constr->vec);
491  if (constr->rel == CONSTR_EQ) {
492  c += fprintf(stream, " == ");
493  }
494  else {
495  c += fprintf(stream, " <= ");
496  }
497  c += qval_fprint(stream, constr->cst);
498  return c;
499 }
#define qval_fprint(stream, q)
Output q on stdio stream stream.
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...
static int NOWUNUSED vec_fprint(FILE *stream, vec_p vec)
Output vec on stdio stream stream.
constrrel_t rel
equality or inequality relation

References CONSTR_EQ, constr_s::cst, fprintf(), qval_fprint, constr_s::rel, constr_s::vec, and vec_fprint().

Referenced by table_fprint().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ constr_iadd()

static void constr_iadd ( constr_p  constr1,
constr_p  constr2 
)
static

Set constr1 to constr1 + constr2.

constr2 must be an equality.

Definition at line 422 of file sc_simplex_feasibility.h.

423 {
424  // general case is not needed
425  assert(constr2->rel == CONSTR_EQ);
426  vec_iadd(&constr1->vec, constr2->vec);
427  qval_add(constr1->cst, constr1->cst, constr2->cst);
428 }
#define qval_add(q1, q2, q3)
Set q1 to q2 + q3.
#define assert(ex)
Definition: newgen_assert.h:41
static void vec_iadd(vec_p *pvec, vec_p vec)
Set pvec to pvec + vec.

References assert, CONSTR_EQ, constr_s::cst, qval_add, constr_s::rel, constr_s::vec, and vec_iadd().

Referenced by table_set_obj().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ constr_iaddmul()

static void constr_iaddmul ( constr_p  constr1,
qval_t  coeff,
constr_p  constr2 
)
static

Set constr1 to constr1 + coeff times constr2.

constr2 must be an equality or coeff a non-negative value.

Definition at line 444 of file sc_simplex_feasibility.h.

445 {
446  if (!qval_equal_i(coeff, 0, 1)) {
447  vec_iaddmul(&constr1->vec, coeff, constr2->vec);
448  qval_t tmp;
449  qval_init(tmp);
450  qval_mul(tmp, coeff, constr2->cst);
451  qval_add(constr1->cst, constr1->cst, tmp);
452  qval_clear(tmp);
453  }
454 }
#define qval_equal_i(q1, q2num, q2den)
Return non-zero if q and q2num/q2den are equal, zero if they are non-equal.
#define qval_mul(q1, q2, q3)
Set q1 to q2 times q3.
static void vec_iaddmul(vec_p *pvec, qval_t coeff, vec_p vec)
Set pvec to pvec + coeff times vec.

References constr_s::cst, qval_add, qval_clear, qval_equal_i, qval_init, qval_mul, constr_s::vec, and vec_iaddmul().

Referenced by constr_apply_pivot().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ constr_imul()

static void constr_imul ( constr_p  constr,
qval_t  coeff 
)
static

Set constr to coeff times constr.

constr must be an equality or coeff a non-negative value.

Definition at line 434 of file sc_simplex_feasibility.h.

435 {
436  vec_imul(&constr->vec, coeff);
437  qval_mul(constr->cst, constr->cst, coeff);
438 }
static void vec_imul(vec_p *pvec, qval_t coeff)
Set pvec to coeff times pvec.

References constr_s::cst, qval_mul, constr_s::vec, and vec_imul().

Referenced by table_apply_pivot().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ constr_init()

static void constr_init ( constr_p  constr)
static

Initialize constr and set it to 0 = 0.

Definition at line 386 of file sc_simplex_feasibility.h.

387 {
388  vec_init(constr->vec);
389  constr->rel = CONSTR_EQ;
390  qval_init(constr->cst);
391 }
#define vec_init(vec)
Set vec to be the empty vector.

References CONSTR_EQ, constr_s::cst, qval_init, constr_s::rel, constr_s::vec, and vec_init.

Referenced by table_init().

+ Here is the caller graph for this function:

◆ constr_makepos()

static void constr_makepos ( constr_p  constr)
static

Turn constr into an equivalent constraint whose constant term is non-negative.

constr must be an equality.

Definition at line 461 of file sc_simplex_feasibility.h.

462 {
463  assert(constr->rel == CONSTR_EQ);
464  if (qval_cmp_i(constr->cst, 0, 1) < 0) {
465  vec_ineg(constr->vec);
466  qval_neg(constr->cst, constr->cst);
467  }
468 }
#define qval_cmp_i(q1, q2num, q2den)
Compare q1 and q2num/q2den.
static void vec_ineg(vec_p vec)
Set vec to -vec.

References assert, CONSTR_EQ, constr_s::cst, qval_cmp_i, qval_neg, constr_s::rel, constr_s::vec, and vec_ineg().

Referenced by table_makepos().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ constr_set()

static void NOWUNUSED constr_set ( constr_p  constr1,
constr_p  constr2 
)
static

Copy constr2 into constr1.

Definition at line 405 of file sc_simplex_feasibility.h.

406 {
407  vec_set(&constr1->vec, constr2->vec);
408  constr1->rel = constr2->rel;
409  qval_set(constr1->cst, constr2->cst);
410 }
#define qval_set(q1, q2)
Set the value of q1 from q2.
static void vec_set(vec_p *pvec, vec_p vec)
Copy vec into pvec.

References constr_s::cst, qval_set, constr_s::rel, constr_s::vec, and vec_set().

+ Here is the call graph for this function:

◆ constr_set_contrainte()

static void constr_set_contrainte ( vartbl_t  vartbl,
constr_p  constr1,
Pcontrainte  constr2,
bool  is_ineq 
)
static

Copy constr2 into constr1.

Definition at line 1037 of file sc_simplex_feasibility.h.

1039 {
1040  vec_set_vecteur(vartbl, &constr1->vec, constr2->vecteur);
1041  constr1->rel = is_ineq ? CONSTR_LE : CONSTR_EQ;
1042  Pvecteur vec;
1043  for (vec = constr2->vecteur; vec != NULL; vec = vec->succ) {
1044  if (vec->var == TCST) {
1045  qval_set_i(constr1->cst, -vec->val, 1);
1046  break;
1047  }
1048  }
1049 }
#define qval_set_i(q1, q2num, q2den)
Set the value of q to q2num/q2den.
static void vec_set_vecteur(vartbl_t vartbl, vec_p *pvec, Pvecteur vec)
Copy vec into pvec.
Pvecteur vecteur
le type des coefficients dans les vecteurs: Value est defini dans le package arithmetique
Definition: vecteur-local.h:89
Value val
Definition: vecteur-local.h:91
Variable var
Definition: vecteur-local.h:90
struct Svecteur * succ
Definition: vecteur-local.h:92
#define TCST
VARIABLE REPRESENTANT LE TERME CONSTANT.

References CONSTR_EQ, CONSTR_LE, constr_s::cst, qval_set_i, constr_s::rel, Svecteur::succ, TCST, Svecteur::val, Svecteur::var, constr_s::vec, vec_set_vecteur(), and Scontrainte::vecteur.

Referenced by table_init_set_systeme().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ safe_malloc()

static void* safe_malloc ( size_t  size)
static

Definition at line 56 of file sc_simplex_feasibility.h.

57 {
58  void* m = malloc(size);
59  if (m == NULL) {
60  fprintf(stderr, "[" __FILE__ "] out of memory\n");
61  abort();
62  }
63  return m;
64 }
void * malloc(YYSIZE_T)
#define abort()
Definition: misc-local.h:53

References abort, fprintf(), and malloc().

Referenced by sc_get_feasibility(), table_init(), vec_append_atfirst(), vec_iadd(), vec_iaddmul(), and vec_set().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sc_get_feasibility()

static bool sc_get_feasibility ( Psysteme  sys,
int  ofl_ctrl 
)
inlinestatic

Main Function.

Determine whether a system sys of equations and inequations is feasible. Parameter ofl_ctrl indicates whether an overflow control is performed (possible values: NO_OFL_CTRL, FWD_OFL_CTRL).

Definition at line 1085 of file sc_simplex_feasibility.h.

1086 {
1087  if (ofl_ctrl != FWD_OFL_CTRL) {
1088  fprintf(stderr, "[sc_simplexe_feasibility] "
1089  "should not (yet) be called with control %d...\n", ofl_ctrl);
1090  }
1091  volatile table_p tbl = safe_malloc(sizeof(table_t));
1092  table_init_set_systeme(tbl, sys);
1094  table_clear(tbl);
1095  free(tbl);
1096  if (ofl_ctrl == FWD_OFL_CTRL) {
1097  RETHROW();
1098  }
1099  return true;
1100  }
1101  bool feasible = table_get_feasibility(tbl);
1102  table_clear(tbl);
1103  free(tbl);
1105  return feasible;
1106 }
#define CATCH(what)
@ overflow_error
@ simplex_arithmetic_error
@ timeout_error
#define UNCATCH(what)
#define RETHROW()
void free(void *)
static void table_init_set_systeme(table_p tbl, Psysteme sys)
Initialize tbl from sys.
static void table_clear(table_p tbl)
Free the space occupied by tbl.
static void * safe_malloc(size_t size)
table_s table_t[1]
Type of simplex tableau.
static bool table_get_feasibility(table_p tbl)
Determine whether the constraint system described in tbl is feasible, using simplex method.
Type of simplex tableau.
#define FWD_OFL_CTRL

References CATCH, fprintf(), free(), FWD_OFL_CTRL, overflow_error, RETHROW, safe_malloc(), simplex_arithmetic_error, table_clear(), table_get_feasibility(), table_init_set_systeme(), timeout_error, and UNCATCH.

+ Here is the call graph for this function:

◆ table_addobjvars()

static void table_addobjvars ( table_p  tbl)
static

Add objective variables to each constraint of @tbl whose constant term is not zero.

Definition at line 728 of file sc_simplex_feasibility.h.

729 {
730  qval_t one;
731  qval_init(one); qval_set_i(one, 1, 1);
732  int i;
733  for (i = 0; i < tbl->nbconstrs; i++) {
734  constr_p constr = tbl->constrs[i];
735  assert(qval_cmp_i(constr->cst, 0, 1) >= 0);
736  if (!qval_equal_i(constr->cst, 0, 1)) {
737  var_t var = tbl->nbvars;
738  vec_append_atfirst(&constr->vec, var, one);
739  tbl->nbvars++;
740  }
741  }
742  qval_clear(one);
743 }
static void vec_append_atfirst(vec_p *pvec, var_t var, qval_t coeff)
Add the element (var, @coeff) to pvec, in first position.
int var_t
Type of variables.
Type of linear constraint.
int nbvars
number of variables
int nbconstrs
number of constraints
constr_t * constrs
constraints

References assert, table_s::constrs, constr_s::cst, table_s::nbconstrs, table_s::nbvars, qval_clear, qval_cmp_i, qval_equal_i, qval_init, qval_set_i, constr_s::vec, and vec_append_atfirst().

Referenced by table_prepare().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ table_addofsvars()

static void table_addofsvars ( table_p  tbl)
static

Ensure that all constraints in tbl are equalities.

A new offset variable is introduced into inequalities to turn them in equalities. E.g., inequality a1 x1 + ... + an xn <= b becomes: a1 x1 + ... + an xn + y = b.

Definition at line 666 of file sc_simplex_feasibility.h.

667 {
668  qval_t one;
669  qval_init(one); qval_set_i(one, 1, 1);
670  int i;
671  for (i = 0; i < tbl->nbconstrs; i++) {
672  constr_p constr = tbl->constrs[i];
673  if (constr->rel == CONSTR_LE) {
674  vec_append_atfirst(&constr->vec, tbl->nbvars, one);
675  tbl->nbvars++;
676  constr->rel = CONSTR_EQ;
677  }
678  }
679  qval_clear(one);
680 }

References CONSTR_EQ, CONSTR_LE, table_s::constrs, table_s::nbconstrs, table_s::nbvars, qval_clear, qval_init, qval_set_i, constr_s::rel, constr_s::vec, and vec_append_atfirst().

Referenced by table_canonicalize().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ table_addsignvars()

static void table_addsignvars ( table_p  tbl)
static

Ensure that all variables in tbl are non-negative.

Each occurrence of a unknown-sign variable a x is turned into a x+ - a x-, where @x+ and @x- are new, non-negative variables. Each occurrence of a negative variable a x is turned into -a x', where x' is a new, non-negative variable. We try to do this only when necessary, to limit the amount of newly created variables.

Definition at line 593 of file sc_simplex_feasibility.h.

594 {
595  // first, we try to determine the sign of variables
596  static int vars_info[VAR_MAXNB]; // -1 negative or null, +1 positive or null, 0 unknown
597  int i;
598  for (i = 0; i < VAR_MAXNB; i++) {
599  vars_info[i] = 0;
600  }
601  for (i = 0; i < tbl->nbconstrs; i++) {
602  constr_p constr = tbl->constrs[i];
603  vec_p vec = constr->vec;
604  if (vec != VEC_NULL && vec->succ == VEC_NULL) {
605  var_t var = vec->var;
606  int lsign = value_sign(qval_cmp_i(vec->coeff, 0, 1));
607  assert(lsign != 0);
608  int rsign = value_sign(qval_cmp_i(constr->cst, 0, 1));
609  int sign;
610  if (constr->rel == CONSTR_EQ) {
611  sign = rsign == 0 ? 1 : lsign * rsign;
612  }
613  else {
614  sign = rsign == 0 ? -lsign : (rsign == 1 ? 0 : lsign * rsign);
615  }
616  if (sign != 0 && vars_info[var] == 0) {
617  vars_info[var] = sign;
618  }
619  }
620  }
621  // negative variables are inverted
622  for (i = 0; i < tbl->nbconstrs; i++) {
623  constr_p constr = tbl->constrs[i];
624  vec_p vec;
625  for (vec = constr->vec; vec != VEC_NULL; vec = vec->succ) {
626  var_t var = vec->var;
627  if (vars_info[var] < 0) {
628  qval_neg(vec->coeff, vec->coeff);
629  }
630  }
631  }
632  // unknown sign variables are split
633  var_t var;
634  for (var = tbl->nbvars - 1; var >= 0; var--) {
635  if (vars_info[var] == 0) {
636  vars_info[var] = tbl->nbvars;
637  tbl->nbvars++;
638  }
639  else {
640  vars_info[var] = VAR_NULL;
641  }
642  }
643  // and then, injected into constraints
644  qval_t coeff;
645  qval_init(coeff);
646  for (i = 0; i < tbl->nbconstrs; i++) {
647  constr_p constr = tbl->constrs[i];
648  vec_p vec;
649  for (vec = constr->vec; vec != VEC_NULL; vec = vec->succ) {
650  var_t var = vec->var;
651  if (vars_info[var] != VAR_NULL) {
652  qval_neg(coeff, vec->coeff);
653  vec_append_atfirst(&constr->vec, vars_info[var], coeff);
654  }
655  }
656  }
657  qval_clear(coeff);
658 }
#define value_sign(v)
trian operators on values
#define VEC_NULL
The empty vector.
#define VAR_MAXNB
Maximum number of variables.
#define VAR_NULL
A special value used to represent the absence of variable.
Type of vectors.
var_t var
mapped variable
qval_t coeff
value associated to the variable
struct vec_s * succ
rest of the vector

References assert, vec_s::coeff, CONSTR_EQ, table_s::constrs, constr_s::cst, table_s::nbconstrs, table_s::nbvars, qval_clear, qval_cmp_i, qval_init, qval_neg, constr_s::rel, vec_s::succ, value_sign, vec_s::var, VAR_MAXNB, VAR_NULL, constr_s::vec, vec_append_atfirst(), and VEC_NULL.

Referenced by table_canonicalize().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ table_apply_pivot()

static void table_apply_pivot ( table_p  tbl,
var_t  var,
int  row 
)
static

Apply pivot (var, row) in tbl.

Definition at line 865 of file sc_simplex_feasibility.h.

866 {
867  constr_p constr = tbl->constrs[row];
868  // pivot constraint is normalized s.t. its coefficient on var is 1
869  qval_t coeff;
870  qval_init(coeff);
871  constr_get_coeff(coeff, constr, var);
872  qval_inv(coeff, coeff);
873  constr_imul(constr, coeff);
874  qval_clear(coeff);
875  // then is substracted from other constraints s.t. the coefficient on var is 0
876  constr_apply_pivot(tbl->obj, var, constr);
877  int i;
878  for (i = 0; i < tbl->nbconstrs; i++) {
879  if (i != row) {
880  constr_apply_pivot(tbl->constrs[i], var, constr);
881  }
882  }
883 }
#define qval_inv(q1, q2)
Set q1 to 1/q2.
static void constr_apply_pivot(constr_p constr, var_t pivot_var, constr_p pivot_constr)
Make pivot_var coefficient null in constr, using pivot_constr.
static void constr_imul(constr_p constr, qval_t coeff)
Set constr to coeff times constr.
constr_t obj
objective function and value

References constr_apply_pivot(), constr_get_coeff, constr_imul(), table_s::constrs, table_s::nbconstrs, table_s::obj, qval_clear, qval_init, and qval_inv.

Referenced by table_run_simplex().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ table_canonicalize()

static void table_canonicalize ( table_p  tbl)
static

Canonicalize tbl: ensure that all variables are non-negative, and all constraints are equalities whose constant term is non-negative.

Definition at line 688 of file sc_simplex_feasibility.h.

689 {
690  DEBUG(
691  fprintf(stderr, "Initial system:\n");
692  table_fprint(stderr, tbl);
693  );
694  table_addsignvars(tbl);
695  DEBUG(
696  fprintf(stderr, "\nWith signed variables:\n");
697  table_fprint(stderr, tbl);
698  );
699  table_addofsvars(tbl);
700  DEBUG(
701  fprintf(stderr, "\nWith offset variables:\n");
702  table_fprint(stderr, tbl);
703  );
704  table_makepos(tbl);
705  DEBUG(
706  fprintf(stderr, "\nWith positive constants:\n");
707  table_fprint(stderr, tbl);
708  );
709 }
static void table_addsignvars(table_p tbl)
Ensure that all variables in tbl are non-negative.
#define DEBUG(code)
static int NOWUNUSED table_fprint(FILE *, table_p)
Output tbl on stdio stream stream.
static void table_addofsvars(table_p tbl)
Ensure that all constraints in tbl are equalities.
static void table_makepos(table_p tbl)
Turn constraints into tbl into equivalent ones, whose constant term is non-negative.

References DEBUG, fprintf(), table_addofsvars(), table_addsignvars(), table_fprint(), and table_makepos().

Referenced by table_prepare().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ table_clear()

static void table_clear ( table_p  tbl)
static

Free the space occupied by tbl.

Definition at line 551 of file sc_simplex_feasibility.h.

552 {
553  constr_clear(tbl->obj);
554  int i;
555  for (i = 0; i < tbl->nbconstrs; i++) {
556  constr_clear(tbl->constrs[i]);
557  }
558  free(tbl->constrs);
559 }
static void constr_clear(constr_p constr)
Free the space occupied by constr.

References constr_clear(), table_s::constrs, free(), table_s::nbconstrs, and table_s::obj.

Referenced by sc_get_feasibility().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ table_fprint()

static int NOWUNUSED table_fprint ( FILE *  stream,
table_p  tbl 
)
static

Output tbl on stdio stream stream.

Definition at line 930 of file sc_simplex_feasibility.h.

931 {
932  int c = constr_fprint(stream, tbl->obj);
933  c += fprintf(stream, "\n");
934  int i;
935  for (i = 0; i < 40; i++) {
936  c += fprintf(stream, "-");
937  }
938  c += fprintf(stream, "\n");
939  if (tbl->nbconstrs == 0) {
940  c += fprintf(stream, "true\n");
941  }
942  else {
943  for (i = 0; i < tbl->nbconstrs; i++) {
944  c += fprintf(stream, "(%2d: ", i);
945  c += var_fprint(stream, table_get_assocvar(tbl, i));
946  c += fprintf(stream, ") ");
947  c += constr_fprint(stream, tbl->constrs[i]);
948  c += fprintf(stream, "\n");
949  }
950  }
951  for (i = 0; i < 40; i++) {
952  c += fprintf(stream, "-");
953  }
954  c += fprintf(stream, "\n");
955  return c;
956 }
#define var_fprint(stream, var)
Output var on stdio stream stream.
static var_t table_get_assocvar(table_p tbl, int row)
Retrieve the variable associated with the row (i.e.
static int NOWUNUSED constr_fprint(FILE *stream, constr_p constr)
Output constr on stdio stream stream.

References constr_fprint(), table_s::constrs, fprintf(), table_s::nbconstrs, table_s::obj, table_get_assocvar(), and var_fprint.

Referenced by table_canonicalize(), table_prepare(), and table_run_simplex().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ table_get_assocvar()

static var_t table_get_assocvar ( table_p  tbl,
int  row 
)
static

Retrieve the variable associated with the row (i.e.

constraint) row in tbl.

Definition at line 781 of file sc_simplex_feasibility.h.

782 {
783  if (row != -1) {
784  qval_t tmp;
785  qval_init(tmp);
786  constr_p constr = tbl->constrs[row];
787  vec_p vec;
788  for (vec = constr->vec; vec != VEC_NULL; vec = vec->succ) {
789  if (qval_equal_i(vec->coeff, 1, 1)) {
790  var_t var = vec->var;
791  bool unitcol = true;
792  int i;
793  for (i = 0; i < tbl->nbconstrs; i++) {
794  if (i != row) {
795  constr_get_coeff(tmp, tbl->constrs[i], var);
796  if (!qval_equal_i(tmp, 0, 1)) {
797  unitcol = false;
798  break;
799  }
800  }
801  }
802  if (unitcol) {
803  return var;
804  }
805  }
806  }
807  qval_clear(tmp);
808  }
809  return VAR_NULL;
810 }

References vec_s::coeff, constr_get_coeff, table_s::constrs, table_s::nbconstrs, qval_clear, qval_equal_i, qval_init, vec_s::succ, vec_s::var, VAR_NULL, constr_s::vec, and VEC_NULL.

Referenced by table_fprint(), and table_get_pivotrow().

+ Here is the caller graph for this function:

◆ table_get_feasibility()

static bool table_get_feasibility ( table_p  tbl)
static

Determine whether the constraint system described in tbl is feasible, using simplex method.

Definition at line 920 of file sc_simplex_feasibility.h.

921 {
922  table_prepare(tbl);
923  table_run_simplex(tbl);
924  return qval_equal_i(tbl->obj->cst, 0, 1);
925 }
static void table_run_simplex(table_p tbl)
Run simplex algorithm on @tbl.
static void table_prepare(table_p tbl)
Canonicalize tbl, set the objective and add objective variables.

References table_s::obj, qval_equal_i, table_prepare(), and table_run_simplex().

Referenced by sc_get_feasibility().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ table_get_pivot()

static bool table_get_pivot ( table_p  tbl,
var_t pvar,
int prow 
)
static

Get the next pivot variable and row in @tbl, and store them in pvar and prow respectively.

Definition at line 855 of file sc_simplex_feasibility.h.

856 {
857  *pvar = table_get_pivotvar(tbl);
858  *prow = table_get_pivotrow(tbl, *pvar);
859  return *prow != -1;
860 }
static int table_get_pivotrow(table_p tbl, var_t var)
static var_t table_get_pivotvar(table_p tbl)
Get the next pivot variable in @tbl.

References table_get_pivotrow(), and table_get_pivotvar().

Referenced by table_run_simplex().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ table_get_pivotrow()

static int table_get_pivotrow ( table_p  tbl,
var_t  var 
)
static

Definition at line 818 of file sc_simplex_feasibility.h.

819 {
820  int pivot_row = -1;
821  if (var != VAR_NULL) {
822  var_t pivot_assoc = VAR_NULL;
823  qval_t pivot_ratio, ratio;
824  qval_init(pivot_ratio); qval_init(ratio);
825  int i;
826  for (i = 0; i < tbl->nbconstrs; i++) {
827  constr_p constr = tbl->constrs[i];
828  constr_get_coeff(ratio, constr, var);
829  if (qval_cmp_i(ratio, 0, 1) > 0) {
830  assert(qval_cmp_i(constr->cst, 0, 1) >= 0);
831  qval_div(ratio, constr->cst, ratio);
832  if (pivot_row == -1 || qval_cmp(ratio, pivot_ratio) < 0) {
833  pivot_row = i;
834  pivot_assoc = table_get_assocvar(tbl, pivot_row);
835  qval_set(pivot_ratio, ratio);
836  }
837  else if (qval_cmp(ratio, pivot_ratio) == 0) {
838  var_t assoc = table_get_assocvar(tbl, i);
839  if (assoc < pivot_assoc) {
840  pivot_row = i;
841  pivot_assoc = assoc;
842  }
843  }
844  }
845  }
846  qval_clear(pivot_ratio); qval_clear(ratio);
847  }
848  return pivot_row;
849 }
#define qval_div(q1, q2, q3)
Set q1 to q2/q3.
#define qval_cmp(q1, q2)
Compare q1 and q2.

References assert, constr_get_coeff, table_s::constrs, constr_s::cst, table_s::nbconstrs, qval_clear, qval_cmp, qval_cmp_i, qval_div, qval_init, qval_set, table_get_assocvar(), and VAR_NULL.

Referenced by table_get_pivot().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ table_get_pivotvar()

static var_t table_get_pivotvar ( table_p  tbl)
static

Get the next pivot variable in @tbl.

Bland's rule is used to ensure termination: pivot variable is the lowest-numbered variable whose objective coefficient is negative.

Definition at line 765 of file sc_simplex_feasibility.h.

766 {
767  var_t var = VAR_NULL;
768  vec_p vec;
769  for (vec = tbl->obj->vec; vec != VEC_NULL; vec = vec->succ) {
770  if (qval_cmp_i(vec->coeff, 0, 1) > 0) {
771  var = vec->var;
772  }
773  }
774  return var;
775 }

References vec_s::coeff, table_s::obj, qval_cmp_i, vec_s::succ, vec_s::var, VAR_NULL, and VEC_NULL.

Referenced by table_get_pivot().

+ Here is the caller graph for this function:

◆ table_init()

static void table_init ( table_p  tbl,
int  nbconstrs 
)
static

Initialize tbl, with room for nbconstrs constraints.

Definition at line 536 of file sc_simplex_feasibility.h.

537 {
538  constr_init(tbl->obj);
539  tbl->constrs = safe_malloc(nbconstrs * sizeof(constr_t));
540  int i;
541  for (i = 0; i < nbconstrs; i++) {
542  constr_init(tbl->constrs[i]);
543  }
544  tbl->nbvars = 0;
545  tbl->nbconstrs = nbconstrs;
546 }
static void constr_init(constr_p constr)
Initialize constr and set it to 0 = 0.
constr_s constr_t[1]
Type of linear constraint.

References constr_init(), table_s::constrs, table_s::nbconstrs, table_s::nbvars, table_s::obj, and safe_malloc().

Referenced by table_init_set_systeme().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ table_init_set_systeme()

static void table_init_set_systeme ( table_p  tbl,
Psysteme  sys 
)
static

Initialize tbl from sys.

Definition at line 1054 of file sc_simplex_feasibility.h.

1055 {
1056  vartbl_t vartbl;
1057  vartbl_init(vartbl);
1058  table_init(tbl, sys->nb_eq + sys->nb_ineq);
1059  int i = 0;
1060  Pcontrainte constr;
1061  for (constr = sys->egalites; constr != NULL; constr = constr->succ) {
1062  constr_set_contrainte(vartbl, tbl->constrs[i], constr, false);
1063  i++;
1064  }
1065  for (constr = sys->inegalites; constr != NULL; constr = constr->succ) {
1066  constr_set_contrainte(vartbl, tbl->constrs[i], constr, true);
1067  i++;
1068  }
1069  tbl->nbvars = vartbl->nbvars;
1070  vartbl_clear(vartbl);
1071 }
static void table_init(table_p tbl, int nbconstrs)
Initialize tbl, with room for nbconstrs constraints.
#define vartbl_clear(vartbl)
Free the space occupied by vartbl.
vartbl_s vartbl_t[1]
Type of variable tables.
static void constr_set_contrainte(vartbl_t vartbl, constr_p constr1, Pcontrainte constr2, bool is_ineq)
Copy constr2 into constr1.
static void vartbl_init(vartbl_p vartbl)
Initialize @vartbl to an empty table.
struct Scontrainte * succ
Pcontrainte inegalites
Definition: sc-local.h:71
Pcontrainte egalites
Definition: sc-local.h:70
int nb_ineq
Definition: sc-local.h:73
int nb_eq
Definition: sc-local.h:72

References constr_set_contrainte(), table_s::constrs, Ssysteme::egalites, Ssysteme::inegalites, Ssysteme::nb_eq, Ssysteme::nb_ineq, table_s::nbvars, Scontrainte::succ, table_init(), vartbl_clear, and vartbl_init().

Referenced by sc_get_feasibility().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ table_makepos()

static void table_makepos ( table_p  tbl)
static

Turn constraints into tbl into equivalent ones, whose constant term is non-negative.

All constraints must be equalities.

Definition at line 576 of file sc_simplex_feasibility.h.

577 {
578  int i;
579  for (i = 0; i < tbl->nbconstrs; i++) {
580  constr_makepos(tbl->constrs[i]);
581  }
582 }
static void constr_makepos(constr_p constr)
Turn constr into an equivalent constraint whose constant term is non-negative.

References constr_makepos(), table_s::constrs, and table_s::nbconstrs.

Referenced by table_canonicalize().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ table_prepare()

static void table_prepare ( table_p  tbl)
static

Canonicalize tbl, set the objective and add objective variables.

Definition at line 748 of file sc_simplex_feasibility.h.

749 {
750  table_canonicalize(tbl);
751  table_set_obj(tbl);
752  table_addobjvars(tbl);
753  DEBUG(
754  fprintf(stderr, "\nWith objective:\n");
755  table_fprint(stderr, tbl);
756  fprintf(stderr, "\n");
757  );
758 }
static void table_set_obj(table_p tbl)
Initialize the objective function and value from constraints in tbl.
static void table_canonicalize(table_p tbl)
Canonicalize tbl: ensure that all variables are non-negative, and all constraints are equalities whos...
static void table_addobjvars(table_p tbl)
Add objective variables to each constraint of @tbl whose constant term is not zero.

References DEBUG, fprintf(), table_addobjvars(), table_canonicalize(), table_fprint(), and table_set_obj().

Referenced by table_get_feasibility().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ table_run_simplex()

static void table_run_simplex ( table_p  tbl)
static

Run simplex algorithm on @tbl.

Definition at line 888 of file sc_simplex_feasibility.h.

889 {
890  int i = 0;
891  while (true) {
892  var_t var;
893  int row;
894  if (table_get_pivot(tbl, &var, &row)) {
895  table_apply_pivot(tbl, var, row);
896  i++;
897  DEBUG(
898  fprintf(stderr, "After iteration %d:\n", i);
899  fprintf(stderr, "Pivot variable: ");
900  var_fprint(stderr, var);
901  fprintf(stderr, "\nPivot row: %d\nTable:\n", row);
902  table_fprint(stderr, tbl);
903  fprintf(stderr, "\n");
904  if (i > 200) {
905  fprintf(stderr, "Seems to long, exiting...\n");
906  exit(42);
907  }
908  );
909  }
910  else {
911  break;
912  }
913  }
914 }
#define exit(code)
Definition: misc-local.h:54
static bool table_get_pivot(table_p tbl, var_t *pvar, int *prow)
Get the next pivot variable and row in @tbl, and store them in pvar and prow respectively.
static void table_apply_pivot(table_p tbl, var_t var, int row)
Apply pivot (var, row) in tbl.

References DEBUG, exit, fprintf(), table_apply_pivot(), table_fprint(), table_get_pivot(), and var_fprint.

Referenced by table_get_feasibility().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ table_set_obj()

static void table_set_obj ( table_p  tbl)
static

Initialize the objective function and value from constraints in tbl.

Definition at line 714 of file sc_simplex_feasibility.h.

715 {
716  constr_p obj = tbl->obj;
717  int i;
718  for (i = 0; i < tbl->nbconstrs; i++) {
719  constr_p constr = tbl->constrs[i];
720  constr_iadd(obj, constr);
721  }
722 }
static void constr_iadd(constr_p constr1, constr_p constr2)
Set constr1 to constr1 + constr2.

References constr_iadd(), table_s::constrs, table_s::nbconstrs, and table_s::obj.

Referenced by table_prepare().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vartbl_find()

static var_t vartbl_find ( vartbl_p  vartbl,
Variable  name 
)
static

Get the variable index associated to name, creating a new one if necessary.

Definition at line 1004 of file sc_simplex_feasibility.h.

1005 {
1006  int i;
1007  for (i = 0; i < vartbl->nbvars; i++) {
1008  if (vartbl->names[i] == name) {
1009  return i;
1010  }
1011  }
1012  vartbl->names[vartbl->nbvars] = name;
1013  return vartbl->nbvars++;
1014 }
Variable names[VAR_MAXNB]
int nbvars
number of variables

References vartbl_s::names, and vartbl_s::nbvars.

Referenced by vec_set_vecteur().

+ Here is the caller graph for this function:

◆ vartbl_init()

static void vartbl_init ( vartbl_p  vartbl)
static

Initialize @vartbl to an empty table.

Definition at line 990 of file sc_simplex_feasibility.h.

991 {
992  vartbl->nbvars = 0;
993 }

References vartbl_s::nbvars.

Referenced by table_init_set_systeme().

+ Here is the caller graph for this function:

◆ vec_append()

static void vec_append ( vec_p pvec,
var_t  var,
qval_t  coeff 
)
static

Add the element (var, coeff) to pvec.

Definition at line 182 of file sc_simplex_feasibility.h.

183 {
184  if (!qval_equal_i(coeff, 0, 1)) {
185  while (*pvec != VEC_NULL && (*pvec)->var > var) {
186  pvec = &(*pvec)->succ;
187  }
188  }
189  vec_append_atfirst(pvec, var, coeff);
190 }

References vec_s::coeff, qval_equal_i, vec_s::succ, vec_s::var, vec_append_atfirst(), and VEC_NULL.

Referenced by vec_set_vecteur().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vec_append_atfirst()

static void vec_append_atfirst ( vec_p pvec,
var_t  var,
qval_t  coeff 
)
static

Add the element (var, @coeff) to pvec, in first position.

var must be greater to any variable in pvec s.t. pvec remains consistent.

Definition at line 169 of file sc_simplex_feasibility.h.

170 {
171  assert(*pvec == VEC_NULL || (*pvec)->var < var);
172  vec_p hd = safe_malloc(sizeof(vec_s));
173  hd->var = var;
174  qval_init(hd->coeff); qval_set(hd->coeff, coeff);
175  hd->succ = *pvec;
176  *pvec = hd;
177 }

References assert, vec_s::coeff, qval_init, qval_set, safe_malloc(), vec_s::succ, vec_s::var, and VEC_NULL.

Referenced by table_addobjvars(), table_addofsvars(), table_addsignvars(), and vec_append().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vec_clear()

static void vec_clear ( vec_p pvec)
static

Free the space occupied by pvec.

Definition at line 130 of file sc_simplex_feasibility.h.

131 {
132  while (*pvec != VEC_NULL) {
133  vec_p succ = (*pvec)->succ;
134  qval_clear((*pvec)->coeff);
135  free(*pvec);
136  *pvec = succ;
137  }
138 }

References free(), qval_clear, vec_s::succ, and VEC_NULL.

Referenced by constr_clear(), vec_imul(), vec_set(), and vec_set_vecteur().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vec_fprint()

static int NOWUNUSED vec_fprint ( FILE *  stream,
vec_p  vec 
)
static

Output vec on stdio stream stream.

Definition at line 318 of file sc_simplex_feasibility.h.

319 {
320  if (vec == VEC_NULL) {
321  return fprintf(stream, "0");
322  }
323  else {
324  int c = qval_fprint(stream, vec->coeff);
325  c += fprintf(stream, " ");
326  c += var_fprint(stream, vec->var);
327  for (vec = vec->succ; vec != VEC_NULL; vec = vec->succ) {
328  if (qval_cmp_i(vec->coeff, 0, 1) > 0) {
329  c += fprintf(stream, " + ");
330  c += qval_fprint(stream, vec->coeff);
331  }
332  else {
333  c += fprintf(stream, " - ");
334  qval_neg(vec->coeff, vec->coeff);
335  c += qval_fprint(stream, vec->coeff);
336  qval_neg(vec->coeff, vec->coeff);
337  }
338  c += fprintf(stream, " ");
339  c += var_fprint(stream, vec->var);
340  }
341  return c;
342  }
343 }

References vec_s::coeff, fprintf(), qval_cmp_i, qval_fprint, qval_neg, vec_s::succ, vec_s::var, var_fprint, and VEC_NULL.

Referenced by constr_fprint().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vec_get_coeff()

static void vec_get_coeff ( qval_t  coeff,
vec_p  vec,
var_t  var 
)
static

Get the value associated to var in vec, and store it into coeff.

Definition at line 195 of file sc_simplex_feasibility.h.

196 {
197  while (vec != VEC_NULL && vec->var > var) {
198  vec = vec->succ;
199  }
200  if (vec != VEC_NULL && vec->var == var) {
201  qval_set(coeff, vec->coeff);
202  }
203  else {
204  qval_set_i(coeff, 0, 1);
205  }
206 }

References vec_s::coeff, qval_set, qval_set_i, vec_s::succ, vec_s::var, and VEC_NULL.

◆ vec_iadd()

static void vec_iadd ( vec_p pvec,
vec_p  vec 
)
static

Set pvec to pvec + vec.

Definition at line 211 of file sc_simplex_feasibility.h.

212 {
213  for (; vec != VEC_NULL; vec = vec->succ) {
214  var_t var = vec->var;
215  while (*pvec != VEC_NULL && (*pvec)->var > var) {
216  pvec = &(*pvec)->succ;
217  }
218  if (*pvec == VEC_NULL || (*pvec)->var < var) {
219  // a new variable is added
220  vec_p hd = safe_malloc(sizeof(vec_s));
221  hd->var = var;
222  qval_init(hd->coeff); qval_set(hd->coeff, vec->coeff);
223  hd->succ = *pvec;
224  *pvec = hd;
225  pvec = &(*pvec)->succ;
226  }
227  else {
228  // the variable is present in the original vector
229  assert((*pvec)->var == var);
230  qval_add((*pvec)->coeff, (*pvec)->coeff, vec->coeff);
231  if (qval_equal_i((*pvec)->coeff, 0, 1)) {
232  vec_p tl = (*pvec)->succ;
233  qval_clear((*pvec)->coeff);
234  free(*pvec);
235  *pvec = tl;
236  }
237  else {
238  pvec = &(*pvec)->succ;
239  }
240  }
241  }
242 }

References assert, vec_s::coeff, free(), qval_add, qval_clear, qval_equal_i, qval_init, qval_set, safe_malloc(), vec_s::succ, vec_s::var, and VEC_NULL.

Referenced by constr_iadd().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vec_iaddmul()

static void vec_iaddmul ( vec_p pvec,
qval_t  coeff,
vec_p  vec 
)
static

Set pvec to pvec + coeff times vec.

Definition at line 263 of file sc_simplex_feasibility.h.

264 {
265  if (qval_equal_i(coeff, 0, 1)) {
266  return;
267  }
268  else {
269  qval_t tmp;
270  qval_init(tmp);
271  for (; vec != VEC_NULL; vec = vec->succ) {
272  var_t var = vec->var;
273  while (*pvec != VEC_NULL && (*pvec)->var > var) {
274  pvec = &(*pvec)->succ;
275  }
276  if (*pvec == VEC_NULL || (*pvec)->var < var) {
277  // a new variable is added
278  vec_p hd = safe_malloc(sizeof(vec_s));
279  hd->var = var;
280  qval_init(hd->coeff); qval_mul(hd->coeff, coeff, vec->coeff);
281  hd->succ = *pvec;
282  *pvec = hd;
283  pvec = &(*pvec)->succ;
284  }
285  else {
286  // the variable is present in the original vector
287  assert((*pvec)->var == var);
288  qval_mul(tmp, coeff, vec->coeff);
289  qval_add((*pvec)->coeff, (*pvec)->coeff, tmp);
290  if (qval_equal_i((*pvec)->coeff, 0, 1)) {
291  vec_p tl = (*pvec)->succ;
292  qval_clear((*pvec)->coeff);
293  free(*pvec);
294  *pvec = tl;
295  }
296  else {
297  pvec = &(*pvec)->succ;
298  }
299  }
300  }
301  qval_clear(tmp);
302  }
303 }

References assert, vec_s::coeff, free(), qval_add, qval_clear, qval_equal_i, qval_init, qval_mul, safe_malloc(), vec_s::succ, vec_s::var, and VEC_NULL.

Referenced by constr_iaddmul().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vec_imul()

static void vec_imul ( vec_p pvec,
qval_t  coeff 
)
static

Set pvec to coeff times pvec.

Definition at line 247 of file sc_simplex_feasibility.h.

248 {
249  if (qval_equal_i(coeff, 0, 1)) {
250  vec_clear(pvec);
251  }
252  else {
253  vec_p vec;
254  for (vec = *pvec; vec != VEC_NULL; vec = vec->succ) {
255  qval_mul(vec->coeff, vec->coeff, coeff);
256  }
257  }
258 }

References vec_s::coeff, qval_equal_i, qval_mul, vec_s::succ, vec_clear(), and VEC_NULL.

Referenced by constr_imul().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vec_ineg()

static void vec_ineg ( vec_p  vec)
static

Set vec to -vec.

Definition at line 308 of file sc_simplex_feasibility.h.

309 {
310  for (; vec != VEC_NULL; vec = vec->succ) {
311  qval_neg(vec->coeff, vec->coeff);
312  }
313 }

References vec_s::coeff, qval_neg, vec_s::succ, and VEC_NULL.

Referenced by constr_makepos().

+ Here is the caller graph for this function:

◆ vec_set()

static void vec_set ( vec_p pvec,
vec_p  vec 
)
static

Copy vec into pvec.

Definition at line 143 of file sc_simplex_feasibility.h.

144 {
145  vec_clear(pvec);
146  bool first = true;
147  vec_p* last = NULL;
148  for (; vec != VEC_NULL; vec = vec->succ) {
149  *last = safe_malloc(sizeof(vec_s));
150  (*last)->var = vec->var;
151  qval_init((*last)->coeff);
152  qval_set((*last)->coeff, vec->coeff);
153  if (first) {
154  *pvec = *last;
155  first = false;
156  }
157  last = &(*last)->succ;
158  }
159  if (!first) {
160  *last = VEC_NULL;
161  }
162 }

References vec_s::coeff, qval_init, qval_set, safe_malloc(), vec_s::succ, vec_s::var, vec_clear(), and VEC_NULL.

Referenced by constr_set().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vec_set_vecteur()

static void vec_set_vecteur ( vartbl_t  vartbl,
vec_p pvec,
Pvecteur  vec 
)
static

Copy vec into pvec.

Definition at line 1019 of file sc_simplex_feasibility.h.

1020 {
1021  vec_clear(pvec);
1022  qval_t coeff;
1023  qval_init(coeff);
1024  for (; vec != NULL; vec = vec->succ) {
1025  if (vec->var != TCST) {
1026  var_t var = vartbl_find(vartbl, vec->var);
1027  qval_set_i(coeff, vec->val, 1);
1028  vec_append(pvec, var, coeff);
1029  }
1030  }
1031  qval_clear(coeff);
1032 }
static void vec_append(vec_p *pvec, var_t var, qval_t coeff)
Add the element (var, coeff) to pvec.
static var_t vartbl_find(vartbl_p vartbl, Variable name)
Get the variable index associated to name, creating a new one if necessary.

References qval_clear, qval_init, qval_set_i, Svecteur::succ, TCST, Svecteur::val, Svecteur::var, vartbl_find(), vec_append(), and vec_clear().

Referenced by constr_set_contrainte().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: