Created
June 5, 2013 13:28
-
-
Save demoth/5713839 to your computer and use it in GitHub Desktop.
7 версия
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
''' | |
Оптимизация рейтинга факультета | |
Даниил Бубнов 10.03.2013 | |
ОПТИМИЗАЦИЯ РЕЙТИНГА С АППРОКСИМАЦИЕЙ МОДУЛЯ | |
''' | |
################################################################## | |
# Читаем данные из файла | |
import csv | |
M = list(csv.reader(open('data.csv'))) | |
M = M[1:] | |
# Читаем названия переменных | |
names = list(csv.reader(open('data_names.csv'))) | |
# Задаём переменные от x_1 до x_41 | |
n=41; | |
for i in (range(n)): | |
var('x_'+str(i+1)); | |
# максимумы у других факультетов | |
var('K_1mc K_2mc K_3mc K_4mc K_5mc K_6mc K_7mc K_8mc \ | |
K_9mc K_10mc K_11mc K_12mc K_13mc K_14mc K_15mc') | |
# промежуточные переменные | |
var('K_1m K_2m K_3m K_4m K_5m K_6m K_7m K_8m \ | |
K_9m K_10m K_11m K_12m K_13m K_14m K_15m') | |
# параметр сглаживания | |
a = 1/100000 | |
# аппроксимация модуля | |
ma = (a + x^2)^(1/2) | |
# выражение максимума через модуль | |
var('xm1 xm2') | |
xmax=1/2*(ma(xm1-xm2) + xm1 + xm2) | |
# Задаём константы | |
C_1 = 1/4 | |
C_2 = 1/10 #0.2 | |
C_3 = 10 | |
C_4 = 1/5 | |
C_5 = 37/100 | |
C_6 = 1/2 | |
C_7 = 1/2 | |
C_8 = 1/10 | |
C_9 = 1 | |
C_10 = 3/10 | |
C_11 = 1/3 | |
C_12 = 1/2 | |
C_13 = 1/5 | |
C_14 = 0 | |
C_15 = 0 | |
C_16 = 1/2 | |
V_1 = 3/4 | |
V_2 = 3/20 | |
V_3 = 1/10 | |
V_4 = 1/5 | |
V_5 = 1/5 | |
V_6 = 3/10 | |
V_7 = 3/10 | |
#V_1 = 1/4 | |
#V_1 = 1/4 | |
#V_1 = 1/4 | |
#V_1 = 1/4 | |
V_8 = 2/5 | |
V_9 = 1/10 | |
V_10 = 1/5 | |
V_11 = 3/10 | |
V_12 = 1/4 | |
V_13 = 3/10 | |
V_14 = 1/10 | |
V_15 = 35/100 | |
W_1 = 35/100 | |
W_2 = 1/10 | |
#W_13 = 1/10 | |
W_3 = 3/10 | |
W_4 = 3/20 | |
# Задаём функции | |
B = x_14 + C_1 * x_15 + C_2 * x_16 | |
K_1 = (x_1 + C_16*x_2 + C_4*x_4 + C_5*(x_9 + C_16*x_10))/B | |
K_2 = (x_3 + C_6*x_5)/B | |
K_3 = (x_6 + C_7 * x_7 + C_5*(x_11+ C_7*x_12))/B | |
K_4 = x_33/B | |
K_5 = x_32/B | |
K_6 = x_34/B | |
K_7 = x_35/B | |
#K_131 = x_38 | |
#K_132 = x_39 | |
#K_133 = x_40/B | |
#K_134 = x_41/B | |
K_8 = B | |
K_9 = (x_17 + C_11*x_18)/B | |
K_10 = x_22 / B | |
K_11 = (x_20 + C_9*x_23 + C_10*(x_21 + C_9*x_24))/B | |
K_12 = x_25/B | |
K_13 = x_26/B | |
K_14 = (x_27 + C_12*x_28 + C_13 * (x_29 + C_12*x_30))/B | |
K_15 = (x_36 + C_3*x_37)/B | |
K_1m = xmax.subs({xm1:K_1, xm2:K_1mc}) | |
K_2m = xmax.subs({xm1:K_2, xm2:K_2mc}) | |
K_3m = xmax.subs({xm1:K_3, xm2:K_3mc}) | |
K_4m = xmax.subs({xm1:K_4, xm2:K_4mc}) | |
K_5m = xmax.subs({xm1:K_5, xm2:K_5mc}) | |
K_6m = xmax.subs({xm1:K_6, xm2:K_6mc}) | |
K_7m = xmax.subs({xm1:K_7, xm2:K_7mc}) | |
K_8m = xmax.subs({xm1:K_8, xm2:K_8mc}) | |
K_9m = xmax.subs({xm1:K_9, xm2:K_9mc}) | |
K_10m = xmax.subs({xm1:K_10, xm2:K_10mc}) | |
K_11m = xmax.subs({xm1:K_11, xm2:K_11mc}) | |
K_12m = xmax.subs({xm1:K_12, xm2:K_12mc}) | |
K_13m = xmax.subs({xm1:K_13, xm2:K_13mc}) | |
K_14m = xmax.subs({xm1:K_14, xm2:K_14mc}) | |
K_15m = xmax.subs({xm1:K_15, xm2:K_15mc}) | |
# 1.1 интеллектуальный потенциал | |
L_1 = K_1 * V_1 / K_1m + K_2 * V_2 / K_2m + K_3 *\ | |
V_3 / K_3m | |
# 1.2 материальная и информационная база | |
L_2 = K_4 * V_4 / K_4m + K_5 * V_5 / K_5m + K_6 *\ | |
V_6 / K_6m + K_7 * V_7 / K_7m | |
# 2.1 подготовка кадров | |
L_3 = K_8 * V_8 / K_8m + K_9 * V_9 / K_9m + K_10 *\ | |
V_10 / K_10m + K_11 * V_11 / K_11m | |
# 2.2 производство и апробация знаний и технологий | |
L_4 = K_12 * V_12 / K_12m + K_13 * V_13 / K_13m + K_14 *\ | |
V_14 / K_14m + K_15 * V_15 / K_15m | |
# потенциал | |
R_1 = L_1 * W_1 + L_2 * W_2 | |
# активность | |
R_2 = L_3 * W_3 + L_4 * W_4 | |
# рейтинг | |
R = R_2 + R_2/R_1*L_1 * W_1 + L_2 * W_2 # + Ks_13 * V_13 | |
def get_data(fac = 4, subs = {}): | |
''' | |
subs = {'3':{x_1:2}} | |
''' | |
d = {} | |
for i in range(n): | |
d[var('x_'+str(i+1))] = float(M[fac][i+2]) | |
d.update(subs.get(fac, {})) | |
return d | |
# ищем максимальный показатель в группе (среди всех факультетов) | |
def max_K(subs = {},fac=None): | |
''' | |
возвращает словарь с числами (максимальными элементами), | |
который можно вставлять в функцию с помощью .subs(). | |
subs - словарь словарей для замены значений | |
''' | |
r = { | |
'K_1mc' : 0.0, | |
'K_2mc' : 0.0, | |
'K_3mc' : 0.0, | |
'K_4mc' : 0.0, | |
'K_5mc' : 0.0, | |
'K_6mc' : 0.0, | |
'K_7mc' : 0.0, | |
'K_8mc' : 0.0, | |
'K_9mc' : 0.0, | |
'K_10mc' : 0.0, | |
'K_11mc' : 0.0, | |
'K_12mc' : 0.0, | |
'K_13mc' : 0.0, | |
'K_14mc' : 0.0, | |
'K_15mc' : 0.0 } | |
for j in range(len(M)): | |
if j == fac: | |
continue | |
d = get_data(j, subs) | |
for k,v in r.items(): | |
temp_max = eval(k[:-2]).subs(d) | |
if (temp_max > r[k]): | |
r[k] = temp_max | |
result = { var(i) : r[i] for i in r.keys() } | |
return result | |
def eval_K(fac=1): | |
''' | |
Для оптимизации в магистерской работе | |
''' | |
k_m = max_K(); | |
Kas = ['K_1','K_2','K_3','K_4','K_5','K_6','K_7','K_8','K_9',\ | |
'K_10','K_11','K_12','K_13','K_14','K_15'] | |
result = {} | |
for k in Kas: | |
d = get_data(fac) | |
result[k] = eval(k).subs(d)/k_m[eval(k+'m')] | |
return result | |
def print_evals_K(): | |
''' | |
Запишем все данные в файл | |
''' | |
some_file = open('K_faculty.txt','w') | |
for i in range(len(M)-1): | |
k = eval_K(i) | |
some_file.write(M[i + 1][1]) | |
some_file.write('\n') | |
for key,value in k.items(): | |
some_file.write("'" + str(key) + "': \t " + str(value) + "\n") | |
some_file.write('\n') | |
def compute_all_ratings(): | |
""" | |
Возвращает список туплов с именами факультетов и значением рейтинга. | |
""" | |
results = [] | |
for fac in range(len(M)): | |
results.append( (M[fac][1], R.subs(get_data(fac)).subs(max_K()) ) ) | |
results = sorted(results, key = lambda x:x[1], reverse=True) | |
return results | |
def tex_all_ratings(ratings): | |
""" | |
Печатает табличку в \longtable. | |
ratings[i][0] - имя факультета, | |
ratings[i][1] - значение рейтинга. | |
""" | |
with open('results/rating_all.tex','w') as f: | |
f.write('\\begin{longtable}{|c|l|l|}') | |
f.write('\\hline\n\\textbf{Место} & \\textbf{Факультет} & \\textbf{Рейтинг} \\\\\n\\hline \n') | |
for i in range(len(ratings)): | |
string = str(i+1) + ' & ' + str(ratings[i][0]) + ' & ' + str(ratings[i][1].n(digits=4)) + ' \\\\\n\\hline\n' | |
f.write(string) | |
f.write('\\end{longtable}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment