Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

  

""" 

GLPK/exact Backend (simplex method in exact rational arithmetic) 

  

AUTHORS: 

  

- Matthias Koeppe (2016-03) 

""" 

  

############################################################################## 

# Copyright (C) 2016 Matthias Koeppe 

# Distributed under the terms of the GNU General Public License (GPL) 

# The full text of the GPL is available at: 

# http://www.gnu.org/licenses/ 

############################################################################## 

from __future__ import absolute_import 

  

cdef class GLPKExactBackend(GLPKBackend): 

  

""" 

MIP Backend that runs the GLPK solver in exact rational simplex mode. 

  

The only access to data is via double-precision floats, however. It 

reconstructs rationals from doubles and also provides results 

as doubles. 

  

There is no support for integer variables. 

  

TESTS: 

  

General backend testsuite:: 

  

sage: p = MixedIntegerLinearProgram(solver="GLPK/exact") 

sage: TestSuite(p.get_backend()).run(skip="_test_pickling") 

""" 

def __cinit__(self, maximization = True): 

""" 

Constructor 

  

EXAMPLES:: 

  

sage: p = MixedIntegerLinearProgram(solver="GLPK/exact") 

""" 

# inherited __cinit__ is called automatically 

import sage.numerical.backends.glpk_backend as glpk_backend 

self.solver_parameter(glpk_backend.glp_simplex_or_intopt, glpk_backend.glp_exact_simplex_only) 

  

cpdef int add_variable(self, lower_bound=0.0, upper_bound=None, binary=False, continuous=False, integer=False, obj=0.0, name=None) except -1: 

""" 

Add a variable. 

  

This amounts to adding a new column to the matrix. By default, 

the variable is both nonnegative and real. 

  

In this backend, variables are always continuous (real). 

If integer variables are requested via the parameters 

``binary`` and ``integer``, an error will be raised. 

  

INPUT: 

  

- ``lower_bound`` - the lower bound of the variable (default: 0) 

  

- ``upper_bound`` - the upper bound of the variable (default: ``None``) 

  

- ``binary`` - ``True`` if the variable is binary (default: ``False``). 

  

- ``continuous`` - ``True`` if the variable is continuous (default: ``True``). 

  

- ``integer`` - ``True`` if the variable is integer (default: ``False``). 

  

- ``obj`` - (optional) coefficient of this variable in the objective function (default: 0.0) 

  

- ``name`` - an optional name for the newly added variable (default: ``None``). 

  

OUTPUT: The index of the newly created variable 

  

EXAMPLES:: 

  

sage: from sage.numerical.backends.generic_backend import get_solver 

sage: p = get_solver(solver = "GLPK/exact") 

sage: p.ncols() 

0 

sage: p.add_variable() 

0 

sage: p.ncols() 

1 

sage: p.add_variable() 

1 

sage: p.add_variable(lower_bound=-2.0) 

2 

sage: p.add_variable(continuous=True) 

3 

sage: p.add_variable(name='x',obj=1.0) 

4 

sage: p.objective_coefficient(4) 

1.0 

  

TESTS:: 

  

sage: p.add_variable(integer=True) 

Traceback (most recent call last): 

... 

ValueError: GLPK/exact only supports continuous variables 

sage: p.add_variable(binary=True) 

Traceback (most recent call last): 

... 

ValueError: GLPK/exact only supports continuous variables 

""" 

if binary or integer: 

raise ValueError("GLPK/exact only supports continuous variables") 

return GLPKBackend.add_variable(self, lower_bound, upper_bound, binary, continuous, 

integer, obj, name) 

  

cpdef int add_variables(self, int number, lower_bound=0.0, upper_bound=None, binary=False, continuous=False, integer=False, obj=0.0, names=None) except -1: 

""" 

Add ``number`` variables. 

  

This amounts to adding new columns to the matrix. By default, 

the variables are both nonnegative and real. 

  

In this backend, variables are always continuous (real). 

If integer variables are requested via the parameters 

``binary`` and ``integer``, an error will be raised. 

  

INPUT: 

  

- ``n`` - the number of new variables (must be > 0) 

  

- ``lower_bound`` - the lower bound of the variable (default: 0) 

  

- ``upper_bound`` - the upper bound of the variable (default: ``None``) 

  

- ``binary`` - ``True`` if the variable is binary (default: ``False``). 

  

- ``continuous`` - ``True`` if the variable is binary (default: ``True``). 

  

- ``integer`` - ``True`` if the variable is binary (default: ``False``). 

  

- ``obj`` - (optional) coefficient of all variables in the objective function (default: 0.0) 

  

- ``names`` - optional list of names (default: ``None``) 

  

OUTPUT: The index of the variable created last. 

  

EXAMPLES:: 

  

sage: from sage.numerical.backends.generic_backend import get_solver 

sage: p = get_solver(solver = "GLPK/exact") 

sage: p.ncols() 

0 

sage: p.add_variables(5) 

4 

sage: p.ncols() 

5 

sage: p.add_variables(2, lower_bound=-2.0, obj=42.0, names=['a','b']) 

6 

""" 

if binary or integer: 

raise ValueError("GLPK/exact only supports continuous variables") 

return GLPKBackend.add_variables(self, number, lower_bound, upper_bound, binary, continuous, 

integer, obj, names) 

  

cpdef set_variable_type(self, int variable, int vtype): 

""" 

Set the type of a variable. 

  

In this backend, variables are always continuous (real). 

If integer or binary variables are requested via the parameter 

``vtype``, an error will be raised. 

  

INPUT: 

  

- ``variable`` (integer) -- the variable's id 

  

- ``vtype`` (integer) : 

  

* 1 Integer 

* 0 Binary 

* -1 Real 

  

EXAMPLES:: 

  

sage: from sage.numerical.backends.generic_backend import get_solver 

sage: p = get_solver(solver = "GLPK/exact") 

sage: p.add_variables(5) 

4 

sage: p.set_variable_type(3, -1) 

sage: p.set_variable_type(3, -2) 

Traceback (most recent call last): 

... 

ValueError: ... 

""" 

if vtype != -1: 

raise ValueError("GLPK/exact only supports continuous variables")