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

195

196

197

198

199

200

201

202

203

from __future__ import division 

from builtins import range 

import numpy as np 

from gpkit import Variable, Model 

from copy import copy 

 

import unittest 

from gpkit.tests.helpers import run_tests 

 

from robust.twoterm_approximation import TwoTermApproximation 

from robust.testing.models import gp_test_model 

 

class TestTwoTermApproximation(unittest.TestCase): 

def test_equivalent_twoterm_model(self): 

gpmodel = gp_test_model() 

equivalent_constraints = [] 

for c in gpmodel.flat(constraintsets=False): 

equivalent_constraints += TwoTermApproximation.two_term_equivalent_posynomial(c.as_posyslt1()[0], 0, [], True)[1] 

twoterm_gpmodel = Model(gpmodel.cost, [equivalent_constraints], gpmodel.substitutions) 

self.assertAlmostEqual(gpmodel.solve(verbosity=0)['cost'],twoterm_gpmodel.solve(verbosity=0)['cost']) 

 

def test_check_if_permutation_exists(self): 

for _ in range(10): 

number_of_monomials = int(np.random.rand()*15) + 3 

number_of_permutations = TwoTermApproximation.total_number_of_permutations(number_of_monomials) 

 

number_of_gp_variables = int(np.random.rand()*20) + 1 

 

m = [np.random.rand()*10 for _ in range(number_of_monomials)] 

 

for j in range(number_of_monomials): 

for i in range(number_of_gp_variables): 

x = Variable('x_%s' % i) 

m[j] *= x**(np.random.rand()*10 - 5) 

 

p = sum(m) 

 

permutation_list = list(range(0, number_of_monomials)) 

list_of_permutations = [] 

list_of_posynomials = [] 

 

counter = 0 

 

while counter < min(100, int(np.floor(number_of_permutations/2))): 

temp = copy(permutation_list) 

np.random.shuffle(temp) 

 

if TwoTermApproximation.check_if_permutation_exists(list_of_permutations, temp): 

continue 

else: 

list_of_permutations.append(temp) 

_, data_constraints = TwoTermApproximation.two_term_equivalent_posynomial(p, 1, temp, False) 

data_posynomial = [constraint.as_posyslt1()[0]*Variable("z^%s_%s" % (i, 1)) 

for i, constraint in enumerate(data_constraints)] 

list_of_posynomials.append(list(data_posynomial)) 

counter += 1 

 

# counter = 0 

# 

# while counter < min(100, int(np.floor(number_of_permutations/2))): 

# temp = copy(permutation_list) 

# np.random.shuffle(temp) 

# 

# flag_one = TwoTermApproximation.check_if_permutation_exists(list_of_permutations, temp) 

# _, data_constraints = TwoTermApproximation.two_term_equivalent_posynomial(p, 1, temp, False) 

# data_posynomial = [constraint.as_posyslt1()[0]*Variable("z^%s_%s" % (i, 1)) 

# for i, constraint in enumerate(data_constraints)] 

# flag_two = list(data_posynomial) in list_of_posynomials 

# 

# assert (flag_one == flag_two) 

# counter += 1 

 

 

def test_bad_relations(self): 

for _ in range(30): 

number_of_monomials = int(20*np.random.random()) + 3 

number_of_gp_variables = int(np.random.rand()*10) + 1 

number_of_additional_uncertain_variables = int(np.random.rand()*5) + 1 

vector_to_choose_from_pos_only = [0, 0, 1, 0, 0, 0, 0, 0, 1, 0] 

 

m = [np.random.rand()*10 for _ in range(number_of_monomials)] 

p_uncertain_vars = [] 

relations = {} 

sizes = {} 

neg_pos_neutral_powers = [] 

 

for j in range(number_of_monomials): 

for i in range(number_of_gp_variables): 

x = Variable('x_%s' % i) 

m[j] *= x**(np.random.rand()*10 - 5) 

 

number_of_elements_in_relation = min(number_of_monomials, 

int(number_of_monomials*np.random.rand()+2)) 

all_elements = [] 

 

for _ in range(number_of_elements_in_relation): 

element = np.random.choice(list(range(0, number_of_monomials))) 

while element in all_elements: 

element = np.random.choice(list(range(0, number_of_monomials))) 

all_elements.append(element) 

 

element_map = {} 

number_of_element_map_elements = min(number_of_monomials - 1, 

int(number_of_monomials*np.random.rand()+2)) 

for _ in range(number_of_element_map_elements): 

element_map_element = int(np.random.rand()*number_of_monomials) 

while element_map_element in element_map or element_map_element == element: 

element_map_element = int(np.random.rand()*number_of_monomials) 

 

size = int(number_of_monomials*np.random.rand())+1 

element_map[element_map_element] = size 

try: 

relations[element_map_element][element] = size 

except: 

relations[element_map_element] = {element: size} 

 

if element in relations: 

relations[element].update(element_map) 

else: 

relations[element] = element_map 

 

relations_copy = {} 

for key in list(relations.keys()): 

relations_copy[key] = copy(relations[key]) 

sizes[key] = len(relations[key]) 

 

counter = 0 

while relations_copy: 

keys = list(relations_copy.keys()) 

for key in keys: 

if not relations_copy[key]: 

del relations_copy[key] 

continue 

 

u = Variable('u_%s' % counter, np.random.random(), pr=100*np.random.random()) 

counter += 1 

p_uncertain_vars.append(u.key) 

 

el_pow = np.random.choice([-1, 1]) 

m[key] *= u**(np.random.rand()*5*el_pow) 

 

element_keys = list(relations_copy[key].keys()) 

for element_key in element_keys: 

m[element_key] *= u**(-np.random.rand()*5*el_pow) 

 

relations_copy[key][element_key] -= 1 

if relations_copy[key][element_key] == 0: 

del relations_copy[key][element_key] 

 

relations_copy[element_key][key] -= 1 

if relations_copy[element_key][key] == 0: 

del relations_copy[element_key][key] 

 

for i in range(number_of_additional_uncertain_variables): 

u = Variable('u_%s' % counter, np.random.random(), pr=100*np.random.random()) 

counter += 1 

p_uncertain_vars.append(u.key) 

el_pow = np.random.choice([-1, 1]) 

neg_pos_neutral_powers.append([el_pow*vector_to_choose_from_pos_only[int(10*np.random.random())] 

for _ in range(number_of_monomials)]) 

for j in range(number_of_monomials): 

m[j] *= u**(np.random.rand()*5*neg_pos_neutral_powers[i][j]) 

 

p = sum(m) 

monomials = p.chop() 

 

actual_relations, actual_sizes = TwoTermApproximation.bad_relations(p) 

 

keys = list(actual_relations.keys()) 

 

actual_relations_mons = {} 

actual_sizes_mons = {} 

for key in keys: 

internal_map = actual_relations[key] 

map_keys = list(internal_map.keys()) 

map_mons = {} 

for map_key in map_keys: 

map_mons[monomials[map_key]] = internal_map[map_key] 

actual_relations_mons[monomials[key]] = map_mons 

actual_sizes_mons[monomials[key]] = actual_sizes[key] 

 

keys = list(relations.keys()) 

relations_mons = {} 

sizes_mons = {} 

for key in keys: 

internal_map = relations[key] 

map_keys = list(internal_map.keys()) 

map_mons = {} 

for map_key in map_keys: 

map_mons[m[map_key]] = internal_map[map_key] 

relations_mons[m[key]] = map_mons 

sizes_mons[m[key]] = sizes[key] 

 

self.assertEqual(actual_relations_mons, relations_mons) 

self.assertEqual(sizes_mons, actual_sizes_mons) 

 

TESTS = [TestTwoTermApproximation] 

 

def test(): 

run_tests(TESTS) 

 

if __name__ == "__main__": 

test()