The Gurobi Solver

Gurobi offer a high performance solver with Linear Programming capabilities and a nice Python interface here. This page contains a number of examples of how it can be used to solve some of the puzzles published by the Sunday Times and the New Scientist in the UK.

Example 1 – Love Hearts

This puzzle was published in the Sunday Times in 2004 and is notable because the answer given was incorrect. The puzzle is repeated here for convenience:

Bobby has made a Valentine card for his girlfriend Chin-We. He drew rows of identical touching circles in a triangular formation as shown below but with more rows.

He then placed a red heart in as many circles as possible without any of these forming equilateral triangles. He continued by placing pink hearts in some of the other circles and white circles in the remainder.

When he counted the number of hearts of different colours (in no particular order) he obtained three consecutive integers.

How many red hearts are on the card?

The following program solves this problem for n rows. It creates a binary variable for each circle and then seeks to maximise the number of such circles included subject to constraints that prevent the included circles forming any equilateral triangles.

The performance of the Gurobi solver allows us to find the maximum number of red hearts we can add without forming equilateral triangles for up to 14 rows.

rows

9

10

11

12

13

14

red hearts

17

20

22

25

28

31

variables

45

55

66

78

91

105

constraints

330

495

715

1001

1365

1820

The ‘consecutive integer’ condition on the three colours of hearts limits which of these solutions solves the teaser and gives the answer as 22 red hearts. The ‘official’ answer given by the Sunday Times was 16!

Example 2 – Quad Spoiling

This example was published in the New Scientist edition number 1225 on 30th October 1980. It is covered in some detail here.

New Scientist Enigma Number 82 – Quad Spoilingby Stephen Ainley
There are a lot of quadrilaterals in the figure, 492 in fact, I reckon. I do not count ‘crossed quadrilaterals’, like ABCD, only simple ones, like ABED, ABFD, and so on. Now imagine the figure composed of 63 matches. My question is: what is the smallest number of matches you need to remove so as to spoil all 492 quadrilaterals? A quadrilateral is spoilt, of course, when one or more matches is missing from its boundary.
Here is a solution using the Python interface to the Gurobi solver. It creates a binary variable for each individual match, enumerates every quadrilateral and establishes a list of the matches on the boundary of each of them. The aim is to minimise the number variables that are true (missing matches) whilst ensuring that the sum of the variables for each quadrilateral is greater than zero (every quadrilateral has at least one missing match). This type of problem is often referred to as a Minimum Hitting Sum.

Python

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

# New Scientist Enigma Number 82

#

# From New Scientist #1225, 30th October 19#80

#

# 30th October 1980 by Stephen Ainley

#

# Quad-Spoiling

#

# /\

# / \

# A +----+ B

# /\ / \

# / \ / \

# +----+-----+

# /\ / \ / \

# / \ / \ / \

# +----+-----+-----+

# /\ / \ / \ / \

# / \ / \ / \ / \

# +----+-----+-----+-----+

# /\ / \ / \ / \ / \

# / \ / \ / \ / \ / \

# +----+-----+-----+-----+-----+ E

# /\ / \ / \ / \ / \ / \

# / \ / \ / \ / \ / \ / \

# +----+-----+-----+-----+-----+-----+ F

# C D

#

# There are a lot of quadrilaterals in the figure, 492 in fact, I reckon.

# I do not count 'crossed quadrilaterals', like ABCD, only simple ones,

# like ABED, ABFD, and so on.

#

# Now imagine the figure composed of 63 matches. My question is: what is

# the smallest number of matches you need to remove so as to spoil all

# 492 quadrilaterals? A quadrilateral is spoilt, of course, when one or