forked from girureta/CBR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testp.py
83 lines (58 loc) · 1.66 KB
/
testp.py
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
import cbr
import random
import math
import copy
def testfunc(x,y):
return math.cos(x)+math.sin(y)
def testfunc2(x,y):
return (x*x)+(y*y)
def demo(numSamples):
print 'Generating '+str(numSamples)+' random cases'
newCases = []
for i in range(numSamples):
tCase=cbr.Case()
tCase.name='Case '+str(i)
tCase.data=[random.uniform(-5,5),random.uniform(-5,5)]
tCase.solution= testfunc(tCase.data[0],tCase.data[1])
newCases.append(tCase)
return newCases
def demoTest(numSamples):
newCases = []
a=-5
b=10
val=1.0/numSamples
for i in range(numSamples):
for j in range(numSamples):
tCase=cbr.Case()
tCase.name='Case '+str(i)
tCase.data=[a+((i*val)*b),a+((j*val)*b)]
tCase.solution= testfunc(tCase.data[0],tCase.data[1])
newCases.append(tCase)
return newCases
def dumpCases(fileName,cases):
f=open(fileName,'w')
for i in cases:
f.write(str(i.data[0])+' '+str(i.data[1])+' '+str(i.solution)+'\n')
f.close()
def dumpCases2(fileName,cases, numSamples):
f=open(fileName,'w')
num=1
for i in cases:
f.write(str(i.data[0])+' '+str(i.data[1])+' '+str(i.solution)+'\n')
if num==numSamples:
num=1
f.write('\n')
else:
num=num+1
f.close()
lLib=cbr.Library()
lLib.cases=demo(1)
testTarget=demoTest(20)
test = []
for i in testTarget:
cC=copy.deepcopy(i)
lLib.solveCase(cC)
test.append(cC)
print 'Lib contains '+str(len(lLib.cases))+' cases'
dumpCases2('test',test,20)
dumpCases2('testTarget',demoTest(20),20)