forked from theogiraudon/Palma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode de base
89 lines (71 loc) · 1.63 KB
/
Code de base
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
#C'est en python 2.7 ...
# -*- coding: utf-8 -*-
"""
Éditeur de Spyder
Ceci est un script temporaire.
"""
import numpy as np
from cvxopt import matrix, solvers
import matplotlib.pyplot as plt
n=20
def mu(x) :
return 2*x
def nu(x) :
return 1
def cout(x,y):
return (x-y)**2
def integration(f,xmin,xmax):
s=0.
nbr=20
pas=(xmax-xmin)/float(nbr)
for i in range(nbr):
s+=f(xmin+pas*i)*pas
return s
def creerA(n):
A=[]
for i in range(n):
ligne=[0. for k in range(n*n)]
for j in range(n):
ligne[i*n+j]=1.
A.append(ligne)
for i in range(n-1):
ligne=[0. for k in range(n*n)]
for j in range(n):
ligne[i+j*n]=1.
A.append(ligne)
A=np.array(A)
return A
def creeraetb(a,b,n):
d=np.zeros((2*n-1))
for i in range(n):
d[i]=a[i]
for i in range(n-1):
d[i+n]=b[i]
return d
def solve(a,b,c,n):
A=matrix(creerA(n))
d=matrix(creeraetb(a,b,n))
c=matrix(c)
h=np.zeros((n*n))
h=matrix(h)
G=-np.eye((n*n))
G=matrix(G)
dims ={'l':G.size[0], 'q':[], 's':[]}
return solvers.conelp(c,G,h,dims,A,d)
def reassemblage(PI,n):
M=np.zeros((n,n))
for i in range(n):
for j in range(n):
M[i,j]=PI[j+i*n]
return M
a=np.zeros((n))
b=np.zeros((n))
for i in range(n):
a[i]=integration(mu,float(i)/n,(float(i)+1)/n)
b[i]=integration(nu,float(i)/n,(float(i)+1)/n)
c=np.zeros((n*n))
for i in range(n):
for j in range(n):
c[j+n*i]=cout((float(i)+1)/n,(float(j)+1)/n)
PI=solve(a,b,c,n)['x']
plt.imshow(reassemblage(PI,n))