Nantes Université

Skip to content
Extraits de code Groupes Projets
Valider ff22ed1f rédigé par Marko Budinich's avatar Marko Budinich
Parcourir les fichiers

Merge branch 'pep8' into 'master'

Coding Conventions for Python Code

See merge request !1
parents d9b1a3cf e974fc51
Branches
Aucune étiquette associée trouvée
1 requête de fusion!1Coding Conventions for Python Code
......@@ -5,10 +5,12 @@ Created on Thu May 4 15:35:50 2017
Construct Ecosystem system
@author: mbudinich
"""
#import cobra
# import cobra
import numpy as np
from scipy.sparse import lil_matrix, find
#%%
# %%
class VlpProblem:
def __init__(self, B=None, a=None, b=None, l=None, s=None, P=None, Y=None, Z=None, c=None, opt_dir=None):
self.B = B
......@@ -22,15 +24,15 @@ class VlpProblem:
self.c = c
self.opt_dir = opt_dir
def to_file(self,filename=None): #VLP is 1 based, constraint numbering starts at 1!!
def to_file(self, filename=None): # VLP is 1 based, constraint numbering starts at 1!!
def getlen(obj):
return 0 if obj is None else len(obj)
if filename is None:
raise RuntimeError("No filename given")
if hasattr(self,'ub') and not hasattr(self,'s'):
if hasattr(self, 'ub') and not hasattr(self, 's'):
self.s = self.ub
if hasattr(self,'lb') and not hasattr(self,'l'):
if hasattr(self, 'lb') and not hasattr(self, 'l'):
self.l = self.lb
if self.opt_dir is None:
self.opt_dir = 1
......@@ -42,129 +44,138 @@ class VlpProblem:
# raise RuntimeError('Matrix B has no shape attribute')
# if not hasattr(self.P,'shape'):
# raise RuntimeError('Matrix P has no shape attribute')
(m,n) = self.B.shape
(q,p) = self.P.shape
(m, n) = self.B.shape
(q, p) = self.P.shape
if (n != p):
raise RuntimeError('B and P must have same number of columns')
[A_rows,A_cols,A_vals]=find(lil_matrix(self.B))
k=len(A_rows)
[P_rows,P_cols,P_vals]=find(lil_matrix(self.P))
k1=len(P_rows)
kstr=''
[A_rows, A_cols, A_vals] = find(lil_matrix(self.B))
k = len(A_rows)
[P_rows, P_cols, P_vals] = find(lil_matrix(self.P))
k1 = len(P_rows)
kstr = ''
if self.Y is not None and self.Y.shape[1] > 0:
[K_rows,K_cols,K_vals]=find(lil_matrix(self.Y))
k2=len(K_rows)
kstr=' cone {} {}'.format(self.Y.shape[1],k2)
[K_rows, K_cols, K_vals] = find(lil_matrix(self.Y))
k2 = len(K_rows)
kstr = ' cone {} {}'.format(self.Y.shape[1], k2)
elif self.Z is not None and self.Z.shape[1] > 0:
[K_rows,K_cols,K_vals] = find(lil_matrix(self.Z))
[K_rows, K_cols, K_vals] = find(lil_matrix(self.Z))
k2 = len(K_rows)
kstr=' dualcone {} {}'.format(self.Z.shape[1],k2)
kstr = ' dualcone {} {}'.format(self.Z.shape[1], k2)
else:
k2=0
opt_dir_str=''
if self.opt_dir==1:
k2 = 0
opt_dir_str = ''
if self.opt_dir == 1:
opt_dir_str = 'min'
elif self.opt_dir==-1:
elif self.opt_dir == -1:
opt_dir_str = 'max'
else:
raise RuntimeError('Invalid value for opt_dir: use -1 or 1 for maximitation and minimization')
raise RuntimeError(
'Invalid value for opt_dir: use -1 or 1 for maximitation and minimization')
try:
file = open(filename,'w')
file = open(filename, 'w')
except OSError as e:
print("OS error: {0}".format(e))
raise
#Write 'p', 'a', 'k' to file
file.write("p vlp {} {} {} {} {} {}{}\n".format(opt_dir_str,m,n,k,q,k1,kstr))
# Write 'p', 'a', 'k' to file
file.write("p vlp {} {} {} {} {} {}{}\n".format(
opt_dir_str, m, n, k, q, k1, kstr))
for i in list(range(k)):
file.write("a {} {} {}\n".format(A_rows[i]+1,A_cols[i]+1,A_vals[i]))
file.write("a {} {} {}\n".format(
A_rows[i] + 1, A_cols[i] + 1, A_vals[i]))
for i in list(range(k1)):
file.write("o {} {} {}\n".format(P_rows[i]+1,P_cols[i]+1,P_vals[i]))
file.write("o {} {} {}\n".format(
P_rows[i] + 1, P_cols[i] + 1, P_vals[i]))
for i in list(range(k2)):
file.write("k {} {} {}\n".format(K_rows[i]+1,K_cols[i]+1,K_vals[i]))
file.write("k {} {} {}\n".format(
K_rows[i] + 1, K_cols[i] + 1, K_vals[i]))
# duality parameter vector
if self.c is not None:
if(len(np.array(self.c).shape) != 1 ) or (len(self.c)!=q) :
if(len(np.array(self.c).shape) != 1) or (len(self.c) != q):
raise RuntimeError('c has wrong dimension')
for i in range(q):
file.write("k {} 0 {}\n".format(i+1,self.c[i]))
#Write row
file.write("k {} 0 {}\n".format(i + 1, self.c[i]))
# Write row
if (len(np.array(self.a).shape) > 1):
raise RuntimeError('a has wrong dimension')
if (len(np.array(self.b).shape) > 1):
raise RuntimeError('b has wrong dimension')
m1 = max(getlen(self.a),getlen(self.b))
m1 = max(getlen(self.a), getlen(self.b))
if self.a is None:
aa = -np.inf*np.ones((m1,1))
aa = -np.inf * np.ones((m1, 1))
else:
aa = self.a
if self.b is None:
bb = np.inf*np.ones((m1,1))
bb = np.inf * np.ones((m1, 1))
else:
bb = self.b
for i in list(range(m1)):
if aa[i] < bb[i]:
ch = 2*np.isfinite(aa[i]) + np.isfinite(bb[i])
ch = 2 * np.isfinite(aa[i]) + np.isfinite(bb[i])
if ch == 0:
file.write('i {} f \n'.format(i+1))
file.write('i {} f \n'.format(i + 1))
elif ch == 1:
file.write('i {} u {}\n'.format(i+1,bb[i]))
file.write('i {} u {}\n'.format(i + 1, bb[i]))
elif ch == 2:
file.write('i {} l {}\n'.format(i+1,aa[i]))
file.write('i {} l {}\n'.format(i + 1, aa[i]))
elif ch == 3:
file.write('i {} d {} {}\n' .format(i+1,aa[i],bb[i]))
file.write('i {} d {} {}\n' .format(i + 1, aa[i], bb[i]))
else:
raise RuntimeError("Bad ch switch for constrains bounds")
elif aa[i] == bb[i] and np.isfinite(aa[i]):
file.write('i %d s %g\n',i,aa[i])
file.write('i %d s %g\n', i, aa[i])
else:
raise RuntimeError('Invalid constrsaints: a[{}]={}, b[{}]={}'.format(i+1,aa[i],i,bb[i]))
#Write cols
raise RuntimeError(
'Invalid constrsaints: a[{}]={}, b[{}]={}'.format(i + 1, aa[i], i, bb[i]))
# Write cols
if self.l is None:
llb=-np.inf*np.ones((n,1))
llb = -np.inf * np.ones((n, 1))
else:
llb=self.l
llb = self.l
if self.s is None:
uub= np.inf*np.ones((n,1))
uub = np.inf * np.ones((n, 1))
else:
uub= self.s
uub = self.s
for j in range(n):
if llb[j] < uub[j]:
ch = 2*np.isfinite(llb[j]) + np.isfinite(uub[j])
ch = 2 * np.isfinite(llb[j]) + np.isfinite(uub[j])
if ch == 0:
file.write('j {} f \n'.format(j+1))
file.write('j {} f \n'.format(j + 1))
elif ch == 1:
file.write('j {} u {}\n'.format(j+1,uub[j]))
file.write('j {} u {}\n'.format(j + 1, uub[j]))
elif ch == 2:
file.write('j {} l {}\n'.format(j+1,llb[j]))
file.write('j {} l {}\n'.format(j + 1, llb[j]))
elif ch == 3:
file.write('j {} d {} {}\n' .format(j+1,llb[j],uub[j]))
file.write('j {} d {} {}\n' .format(j + 1, llb[j], uub[j]))
else:
raise RuntimeError("Bad ch switch for variable bounds")
elif llb[j] == uub[j] and np.isfinite(llb[j]):
file.write('i %d s %g\n',j+1,llb[j])
file.write('i %d s %g\n', j + 1, llb[j])
else:
raise RuntimeError('Invalid constrsaints: l[{}]={}, s[{}]={}'.format(j+1,llb[j],i,uub[j]))
raise RuntimeError('Invalid constrsaints: l[{}]={}, s[{}]={}'.format(
j + 1, llb[j], i, uub[j]))
file.write('e ')
file.close()
#%%
# %%
class EcosystemModel:
from scipy.sparse import lil_matrix, block_diag, eye
def __init__(self,model_array=None,metabolic_dict=None):
from scipy.sparse import lil_matrix, block_diag, eye
def __init__(self, model_array=None, metabolic_dict=None):
"""Instatiate the EcosystemModel object
model_array is an array of cobra models to connect
metabolic_dict is a dicctionary where its keys correspond to metabolites id's and their values to the name equivalence"""
self.models=model_array
self.models = model_array
self.metabolic_dict = metabolic_dict
self._pooldict = None
self.pool_ex_rxns = None
......@@ -178,57 +189,64 @@ class EcosystemModel:
def construct_ecosystem_pool(self):
"""Check all metabolites used in import/export exchanges and construct the pool compartment"""
pooldict=dict()
pooldict = dict()
for model in self.models:
for rxn_ex in model.exchanges:
for met_ex in rxn_ex.metabolites:
met_name = self.metabolic_dict[met_ex.id]
if met_name not in pooldict:
pooldict[met_name]=[(model,rxn_ex,rxn_ex.get_coefficient(met_ex.id))]
if met_name not in pooldict:
pooldict[met_name] = [
(model, rxn_ex, rxn_ex.get_coefficient(met_ex.id))]
else:
pooldict[met_name].append((model,rxn_ex,rxn_ex.get_coefficient(met_ex.id)))
self._pooldict = pooldict
pooldict[met_name].append(
(model, rxn_ex, rxn_ex.get_coefficient(met_ex.id)))
self._pooldict = pooldict
def populate_ecosystem_model(self):
"""Calculate the object attributes after pool construction"""
self.pool_ex_rxns = ["EX_{}:pool".format(key) for key in self._pooldict.keys()]
self.pool_ex_mets = ["{}:pool".format(key) for key in self._pooldict.keys()]
self.sysreactions = ["{}:{}".format(r.id,model.id) for model in self.models for r in model.reactions]
self.sysmetabolites = ["{}:{}".format(m.id,model.id) for model in self.models for m in model.metabolites]
self.pool_ex_rxns = ["EX_{}:pool".format(
key) for key in self._pooldict.keys()]
self.pool_ex_mets = ["{}:pool".format(
key) for key in self._pooldict.keys()]
self.sysreactions = ["{}:{}".format(
r.id, model.id) for model in self.models for r in model.reactions]
self.sysmetabolites = ["{}:{}".format(
m.id, model.id) for model in self.models for m in model.metabolites]
self.sysreactions.extend(self.pool_ex_rxns)
self.sysmetabolites.extend(self.pool_ex_mets)
array_form = [model.to_array_based_model() for model in self.models]
self.Ssigma = block_diag([model.S for model in array_form])
self.Ssigma = lil_matrix(block_diag([self.Ssigma,-eye(len(self.pool_ex_rxns))]))
self.Ssigma = lil_matrix(block_diag(
[self.Ssigma, -eye(len(self.pool_ex_rxns))]))
for met in self._pooldict.keys():
met_name = "{}:pool".format(met)
met_idx = self.sysmetabolites.index(met_name)
for model,reaction,coeff in self._pooldict[met]:
rxn_name = "{}:{}".format(reaction.id,model.id)
for model, reaction, coeff in self._pooldict[met]:
rxn_name = "{}:{}".format(reaction.id, model.id)
rxn_idx = self.sysreactions.index(rxn_name)
self.Ssigma[met_idx,rxn_idx]=-coeff
def add_comparment(self,model):
self.Ssigma[met_idx, rxn_idx] = -coeff
def add_comparment(self, model):
"""Utility function to add a new agent to models. Pretty ineficient, re-runs all the steps again for each addition"""
self.__init__(self.model_array.add(model),self.metabolic_dict)
self.__init__(self.model_array.add(model), self.metabolic_dict)
self.construct_ecosystem_pool()
self.populate_ecosystem_model()
@staticmethod
def get_common_mets(model_list):
"""Naive implementation of getting common exchange metabolites, using their id's"""
common_mets=dict()
common_mets = dict()
for model in model_list:
for rxn_ex in model.exchanges:
for met_ex in rxn_ex.metabolites:
if met_ex.id not in common_mets:
common_mets[met_ex.id]=dict([(model,rxn_ex)])
common_mets[met_ex.id] = dict([(model, rxn_ex)])
else:
common_mets[met_ex.id][model]=rxn_ex
common_mets[met_ex.id][model] = rxn_ex
return(common_mets)
#%%
# %%
#%%
# %%
......@@ -9,7 +9,7 @@ Created on Wed May 31 15:45:22 2017
# Example: MOLP with 2 objectives, simplest example
# min [x1 - x2; x1 + x2]
#
#
# 6 <= 2*x1 + x2
# 6 <= x1 + 2*x2
#
......@@ -17,14 +17,14 @@ Created on Wed May 31 15:45:22 2017
# x2 >= 0
import numpy as np
from benpy import solve as bensolve, vlpProblem, vlpSolution
#%%
from benpy import solve as bensolve, vlpProblem
# %%
vlp = vlpProblem()
vlp.B=np.matrix([[2,1],[1,2]]) # coefficient matrix
vlp.a=[6,6] # lower bounds
vlp.P=np.matrix([[1,-1],[1,1]]); # objective matrix
vlp.l=[0,0]; # lower variable bounds
vlp.B = np.matrix([[2, 1], [1, 2]]) # coefficient matrix
vlp.a = [6, 6] # lower bounds
vlp.P = np.matrix([[1, -1], [1, 1]]) # objective matrix
vlp.l = [0, 0] # lower variable bounds
vlp.default_options()
......@@ -33,6 +33,3 @@ vlp.to_string()
vlp.to_file('test01.vlp')
sol = bensolve(vlp)
......@@ -5,29 +5,29 @@ Created on Wed May 31 15:45:22 2017
Test VLP interfase with bensolve
@author: mbudinich
"""
#%%
from numpy import transpose, ones, zeros, eye, matrix, loadtxt, append, hstack, vstack, inf
# %%
from numpy import transpose, ones, zeros, eye, matrix, loadtxt, append, vstack, inf
from HelperClass import VlpProblem
#%%
# %%
# Example: MOLP with 2 objectives, simplest example
# min [x1 - x2; x1 + x2]
#
#
# 6 <= 2*x1 + x2
# 6 <= x1 + 2*x2
#
# x1 >= 0
# x2 >= 0
vlp = VlpProblem();
vlp = VlpProblem()
vlp.B=matrix([[2,1],[1,2]]) # coefficient matrix
vlp.a=[6,6] # lower bounds
vlp.P=matrix([[1,-1],[1,1]]) # objective matrix
vlp.l=[0,0] # lower variable bounds
vlp.B = matrix([[2, 1], [1, 2]]) # coefficient matrix
vlp.a = [6, 6] # lower bounds
vlp.P = matrix([[1, -1], [1, 1]]) # objective matrix
vlp.l = [0, 0] # lower variable bounds
vlp.to_file('ex01.vlp');
#%%
vlp.to_file('ex01.vlp')
# %%
# Example: MOLP with 2 objectives which is infeasible
# v-min [x1;x2]
......@@ -36,15 +36,15 @@ vlp.to_file('ex01.vlp');
# 0 <= x1 + 2*x2 <= 1
# 1 <= x1 + x2 <= 2
vlp = VlpProblem();
vlp = VlpProblem()
vlp.B=matrix([[3,1],[1,2],[1,1]])
vlp.b=[1, 1, 2]
vlp.a=[0, 0, 1]
vlp.P=matrix([[1,0],[0,1]])
vlp.B = matrix([[3, 1], [1, 2], [1, 1]])
vlp.b = [1, 1, 2]
vlp.a = [0, 0, 1]
vlp.P = matrix([[1, 0], [0, 1]])
vlp.to_file('ex02.vlp');
#%%
vlp.to_file('ex02.vlp')
# %%
# Example: MOLP with 2 objectives the upper image of which has no vertex
# v-min [x1;x2]
......@@ -52,14 +52,14 @@ vlp.to_file('ex02.vlp');
# 1 <= x1 + x2 + x3
# 1 <= x1 + x2 - x3
vlp = VlpProblem();
vlp = VlpProblem()
vlp.B=matrix([[1,1,1],[1,1,-1]])
vlp.a=[1,1]
vlp.P=matrix([[1,0,0], [0,1,0]])
vlp.B = matrix([[1, 1, 1], [1, 1, -1]])
vlp.a = [1, 1]
vlp.P = matrix([[1, 0, 0], [0, 1, 0]])
vlp.to_file('ex03.vlp');
#%%
vlp.to_file('ex03.vlp')
# %%
# Example: MOLP with 2 objectives, which is totally unbounded
# v-min [x1;x2]
......@@ -67,27 +67,28 @@ vlp.to_file('ex03.vlp');
# 1 <= x1 + x2 + x3
# 1 <= x1 + x2 + 2*x3
vlp = VlpProblem();
vlp = VlpProblem()
vlp.B=matrix([[1,1,1],[1,1,2]]);
vlp.a=[1,1]
vlp.P=matrix([[1,0,0], [0,1,0]]);
vlp.B = matrix([[1, 1, 1], [1, 1, 2]])
vlp.a = [1, 1]
vlp.P = matrix([[1, 0, 0], [0, 1, 0]])
vlp.to_file('ex04.vlp');
#%%
vlp.to_file('ex04.vlp')
# %%
# Example: VLP with q = 3 and 4 generating vectors of C
# see http://bensolve.org/demo.html
vlp = VlpProblem();
vlp = VlpProblem()
vlp.B=matrix([ones((1,3)).tolist()[0],[1,2,2],[2,2,1],[2,1,2]]) # coefficient matrix
vlp.a=[1,3/2,3/2,3/2]; # lhs of constraints
vlp.P=matrix([[1,0,1],[1,1,0],[0,1,1]]) # objective matrix
vlp.l=[0,0,0] # lower variable bounds
vlp.B = matrix([ones((1, 3)).tolist()[0], [1, 2, 2], [
2, 2, 1], [2, 1, 2]]) # coefficient matrix
vlp.a = [1, 3 / 2, 3 / 2, 3 / 2] # lhs of constraints
vlp.P = matrix([[1, 0, 1], [1, 1, 0], [0, 1, 1]]) # objective matrix
vlp.l = [0, 0, 0] # lower variable bounds
# generating vectors of ordering cone C
vlp.Y=transpose(matrix([[1,0,0], [0,1,0], [-1,0,2], [0,-1,2]]))
vlp.Y = transpose(matrix([[1, 0, 0], [0, 1, 0], [-1, 0, 2], [0, -1, 2]]))
# alternative variant: generating vectors of the dual of the ordering cone C
# vlp.Z=[2 2 1 ; 2 0 1 ; 0 0 1 ; 0 2 1]';
......@@ -95,59 +96,61 @@ vlp.Y=transpose(matrix([[1,0,0], [0,1,0], [-1,0,2], [0,-1,2]]))
# duality parameter vector (must belong to interior of C)
# if not given, it is computed automatically
# dual problem depends on c
vlp.c=[1,1,1]
vlp.c = [1, 1, 1]
vlp.to_file('ex05.vlp')
#%%
# %%
# Example: VLP with 2 objectives
# max [x1 - x2; x1 + x2]
#
# w.r.t. cone C ={(y1,y2) | 2 y1 -y2 >= 0, -y1 + 2 y2 >= 0}
#
# 1 <= x1 + x2 <= 2
#
# 1 <= x1 + x2 <= 2
#
# 0 <= x1 <= 1
# 0 <= x2
vlp = VlpProblem();
vlp = VlpProblem()
vlp.opt_dir=-1 # maximization
vlp.Z=matrix([[2,-1], [-1,2]]) # generators of dual of ordering cone
vlp.c=[1,1] # geometric duality parameter vector (belongs to interior of C)
vlp.opt_dir = -1 # maximization
vlp.Z = matrix([[2, -1], [-1, 2]]) # generators of dual of ordering cone
vlp.c = [1, 1] # geometric duality parameter vector (belongs to interior of C)
vlp.B=matrix([1,1]) # coefficient matrix
vlp.a=[1] # lhs
vlp.b=[2] # rhs
vlp.l=[0,0] # lower bounds
vlp.s=[1,inf] # upper bounds
vlp.P=matrix([[1,-1],[1,1]]) # objective matrix
vlp.B = matrix([1, 1]) # coefficient matrix
vlp.a = [1] # lhs
vlp.b = [2] # rhs
vlp.l = [0, 0] # lower bounds
vlp.s = [1, inf] # upper bounds
vlp.P = matrix([[1, -1], [1, 1]]) # objective matrix
vlp.to_file('ex06.vlp');
#%%
vlp.to_file('ex06.vlp')
# %%
# Example: MOLP with 3 objectives, 1211 constraints and 1143 variables
#
# Example (PL) in
#
#
# Shao, L., Ehrgott, M.: Approximately solving multiobjective linear programmes in objective space and
# an application in radiotherapy treatment planning. Math. Methods Oper. Res. 68(2), 257Ð276 (2008)
#
# enlarge epsilon in phase 2 and use primal simplex algorithm, for instance, run
# ./bensolve ex/ex07.vlp -m 2 -e 0.05 -l primal_simplex
vlp = VlpProblem();
vlp = VlpProblem()
vlp.B=loadtxt('example07.txt');
vlp.P = -1*matrix([append(zeros((1,1140)),[-1,0,0]),append(zeros((1,1140)),[0,-1,0]),append(zeros((1,1140)),[0,0,-1])])
vlp.l = append(zeros((1140,1)),[0,-45,0])
vlp.s = append(inf*ones((1140,1)),[17.07,12,90.64])
vlp.b = vstack((90.64*ones((67,1)),-85.3601*ones((67,1)),60*ones((37,1)),45*ones((8,1)),60*ones((46,1)),ones((986,1))))
vlp.B = loadtxt('example07.txt')
vlp.P = -1 * matrix([append(zeros((1, 1140)), [-1, 0, 0]), append(
zeros((1, 1140)), [0, -1, 0]), append(zeros((1, 1140)), [0, 0, -1])])
vlp.l = append(zeros((1140, 1)), [0, -45, 0])
vlp.s = append(inf * ones((1140, 1)), [17.07, 12, 90.64])
vlp.b = vstack((90.64 * ones((67, 1)), -85.3601 * ones((67, 1)), 60 *
ones((37, 1)), 45 * ones((8, 1)), 60 * ones((46, 1)), ones((986, 1))))
vlp.b = vlp.b.T.tolist()[0]
vlp.to_file('ex07.vlp');
#%%
vlp.to_file('ex07.vlp')
# %%
# Example: VLP with 2 objectives, which is unbounded (but not totally unbounded)
#
# its the solution consists of feasible points and feasible directions
# its the solution consists of feasible points and feasible directions
# min_C [x1;x2]
#
......@@ -155,22 +158,22 @@ vlp.to_file('ex07.vlp');
#
# duality parameter c is set to [0;1], which is an interior point of C
#
# 0 <= 3*x1 + x2
# 0 <= x1 + 2*x2
# 1 <= x1 + x2
# 0 <= 3*x1 + x2
# 0 <= x1 + 2*x2
# 1 <= x1 + x2
vlp = VlpProblem();
vlp = VlpProblem()
vlp.B=matrix([3,1],[1,2],[1,1])
vlp.a=[0, 0, 1]
vlp.P=[[1,0] , [0,1]];
vlp.B = matrix([3, 1], [1, 2], [1, 1])
vlp.a = [0, 0, 1]
vlp.P = [[1, 0], [0, 1]]
# generating vectors of ordering cone C
vlp.Y=matrix([[-1,3], [3/2,-1]])
vlp.c=[0,1]
vlp.Y = matrix([[-1, 3], [3 / 2, -1]])
vlp.c = [0, 1]
vlp.to_file('ex08.vlp');
#%%
vlp.to_file('ex08.vlp')
# %%
# VLP with 3 objectives, 4608 constrains and 36939 variables
#
# Example 6.6 in
......@@ -180,7 +183,7 @@ vlp.to_file('ex08.vlp');
# special options are necessary to run example:
#
# For instance, run
# ./bensolve ex/ex09.vlp -e 1e-2 -m 3 -L primal_simplex -l primal_simplex -p
# ./bensolve ex/ex09.vlp -e 1e-2 -m 3 -L primal_simplex -l primal_simplex -p
#
# (-e: set epsilon in Phase 2)
# (-m set message level to have more output on screen)
......@@ -196,11 +199,11 @@ vlp.to_file('ex08.vlp');
# (-a use dual Benson algorithm in Phase 2)
# (-p generate graphics files)
#vlp = VlpProblem();
#load('example09');
#vlp.c=[1,1,1]
#vlp.to_file('ex09.vlp');
#%%
# vlp = VlpProblem();
# load('example09');
# vlp.c=[1,1,1]
# vlp.to_file('ex09.vlp');
# %%
# The 'bensolvehedron', see the titlepage of the reference manual
#
# MOLP with q objectives, n=(q+2*m)^q variables and constraints
......@@ -213,36 +216,37 @@ vlp.to_file('ex08.vlp');
# add green color (!)
# adapt q and m to generate other (larger) problems
#q=3;
#m=2;
# q=3;
# m=2;
#n=(q+2*m)^q;
# n=(q+2*m)^q;
#vlp = VlpProblem();
# vlp = VlpProblem();
# feasible set is n dimensional hyper cube
#vlp.B=eye(n)
#vlp.a=zeros((n,1))
#vlp.b=ones((n,1))
# vlp.B=eye(n)
# vlp.a=zeros((n,1))
# vlp.b=ones((n,1))
# objective map
#P=zeros(n,q);
#for i in range(n):
# line = dec2base(i-1,q+2*m,q)-'0';
# line = line - (q+2*m-1)/2;
# P[i,:] = line
# P=zeros(n,q);
# for i in range(n):
# line = dec2base(i-1,q+2*m,q)-'0';
# line = line - (q+2*m-1)/2;
# P[i,:] = line
#vlp.P = transpose(P)
# vlp.P = transpose(P)
#vlp.to_file('ex10.vlp');
#%%
# vlp.to_file('ex10.vlp');
# %%
# Example: MOLP with q=5, unbounded,
# recession cone of upper image has 22 extreme directions (main effort in phase 1)
vlp = VlpProblem();
vlp = VlpProblem()
vlp.B=matrix([[1,1,1,1,1],[2,1,1,1,1],[1,2,1,1,1],[1,1,2,1,1],[1,1,1,2,1],[1,1,1,1,2],[2,2,1,1,1],[2,1,2,1,1],[2,1,1,2,1],[2,1,1,1,2],[1,2,2,1,1],[1,2,1,2,1],[1,2,1,1,2],[1,1,2,2,1],[1,1,2,1,2],[1,1,1,2,2],[2,2,2,1,1],[2,2,1,2,1],[2,2,1,1,2],[2,1,2,1,2],[2,1,1,2,2],[1,2,2,2,1],[1,2,1,2,2],[1,2,2,1,2],[1,2,2,2,1],[1,1,2,2,2],[1,2,2,2,2],[2,1,2,2,2],[2,2,1,2,2],[2,2,2,1,2],[2,2,2,2,1]])
vlp.a=append(1,zeros((30, 1)))
vlp.P=eye((5,5));
vlp.B = matrix([[1, 1, 1, 1, 1], [2, 1, 1, 1, 1], [1, 2, 1, 1, 1], [1, 1, 2, 1, 1], [1, 1, 1, 2, 1], [1, 1, 1, 1, 2], [2, 2, 1, 1, 1], [2, 1, 2, 1, 1], [2, 1, 1, 2, 1], [2, 1, 1, 1, 2], [1, 2, 2, 1, 1], [1, 2, 1, 2, 1], [1, 2, 1, 1, 2], [1, 1, 2, 2, 1], [1, 1, 2, 1, 2], [
1, 1, 1, 2, 2], [2, 2, 2, 1, 1], [2, 2, 1, 2, 1], [2, 2, 1, 1, 2], [2, 1, 2, 1, 2], [2, 1, 1, 2, 2], [1, 2, 2, 2, 1], [1, 2, 1, 2, 2], [1, 2, 2, 1, 2], [1, 2, 2, 2, 1], [1, 1, 2, 2, 2], [1, 2, 2, 2, 2], [2, 1, 2, 2, 2], [2, 2, 1, 2, 2], [2, 2, 2, 1, 2], [2, 2, 2, 2, 1]])
vlp.a = append(1, zeros((30, 1)))
vlp.P = eye((5, 5))
vlp.to_file('ex11.vlp');
\ No newline at end of file
vlp.to_file('ex11.vlp')
......@@ -2,20 +2,19 @@ from distutils.core import setup, Extension
from Cython.Build import cythonize
import numpy
ext = Extension("benpy",sources=["benpy.pyx",
"bensolve-mod/bslv_main.c",
"bensolve-mod/bslv_vlp.c",
"bensolve-mod/bslv_algs.c",
"bensolve-mod/bslv_lists.c",
"bensolve-mod/bslv_poly.c",
"bensolve-mod/bslv_lp.c"
],
libraries=['glpk','m'],
extra_compile_args=['-std=c99','-O3']
)
ext = Extension("benpy", sources=["benpy.pyx",
"bensolve-mod/bslv_main.c",
"bensolve-mod/bslv_vlp.c",
"bensolve-mod/bslv_algs.c",
"bensolve-mod/bslv_lists.c",
"bensolve-mod/bslv_poly.c",
"bensolve-mod/bslv_lp.c"
],
libraries=['glpk', 'm'],
extra_compile_args=['-std=c99', '-O3']
)
setup(
name="benpy",
ext_modules = cythonize([ext]),
include_dirs=[numpy.get_include()]
name="benpy",
ext_modules=cythonize([ext]),
include_dirs=[numpy.get_include()]
)
......@@ -2,7 +2,7 @@ import benpy
problem = benpy.vlpProblem()
problem.filename = "ex01.vlp"
problem.options={'message_level':3}
problem.options = {'message_level': 3}
sol = benpy.solve(problem)
problem2 = benpy.vlpProblem()
......
import benpy
problem = benpy.vlpProblem(filename = "ex01.vlp", options = {'message_level':3, 'solution':True, 'logfile':False})
problem = benpy.vlpProblem(filename="ex01.vlp", options={
'message_level': 3, 'solution': True, 'logfile': False})
sol = benpy.solve(problem)
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter