def _get_solution_static(self): p = self.plate k = p.linprog_vect_k if self.verbose: print 'Vector k:', k C = p.linprog_static_matrix if self.verbose: print 'Matrix C:', C A_eq = np.vstack([np.hstack([C, -C, np.zeros_like(C), np.reshape(-k, (len(k), 1))]), np.hstack([np.identity(C.shape[1]), np.identity(C.shape[1]), np.identity(C.shape[1]), np.reshape(np.zeros(C.shape[1]), (C.shape[1], 1))]), ]) c = np.zeros(C.shape[1] * 3 + 1) c[-1] = -1. b_eq = np.hstack([np.zeros(C.shape[0]), np.ones(C.shape[1])]) print 'Simplex method input assembled succesfully.' print 'Dimensions of vector c:', c.shape print 'Dimensions of matrix A_eq:', A_eq.shape print 'Dimensions of vector b_eq:', b_eq.shape if self.verbose: print 'Vector c:', c print 'Matrix A_eq:', A_eq print 'Vector b_eq:', b_eq sol = lp_solve(f=c, a=A_eq, b=b_eq, e=np.zeros_like(b_eq)) return sol
def _get_dual_solution(self): p = self.plate w_dim = len(p.unsupported_node_nos) b_ub = np.hstack([p.linprog_vect_c, np.zeros(2 * w_dim)]) if self.verbose: print 'Vector b_ub:', b_ub k = p.linprog_vect_k if self.verbose: print 'Vector k:', k B = p.linprog_matrix_B if self.verbose: print 'Matrix B:', B A_eq = np.vstack([np.hstack([np.identity(B.shape[0]), -np.identity(B.shape[0]), -B, B]), np.hstack([np.zeros(B.shape[0]), np.zeros(B.shape[0]), k, -k]), ]) A_ub = np.transpose(A_eq) c = np.zeros(A_ub.shape[1]) c[-1] = -1. print 'Simplex method input assembled succesfully.' print 'Dimensions of vector c:', c.shape print 'Dimensions of matrix A_ub:', A_ub.shape print 'Dimensions of vector b_ub:', b_ub.shape if self.verbose: print 'Vector c:', c print 'Matrix A_ub:', A_ub print 'Vector b_ub:', b_ub sol = sol = lp_solve(f=c, a=A_ub, b=b_ub, e=-np.ones_like(b_ub)) return sol
def _get_solution(self): for nd in self.plate.nodes_with_ref: if nd.w != 0.: nd.reset_traits(['w']) p = self.plate unn = p.unsupported_node_nos w_dim = len(unn) c = np.hstack([p.linprog_vect_c, np.zeros(2 * w_dim)]) if self.verbose: print 'Vector c:', c k = p.linprog_vect_k if self.verbose: print 'Vector k:', k B = p.linprog_matrix_B if self.verbose: print 'Matrix B:', B A_eq = np.vstack([np.hstack([np.identity(B.shape[0]), -np.identity(B.shape[0]), -B, B]), np.hstack([np.zeros(B.shape[0]), np.zeros(B.shape[0]), k, -k]), ]) b_eq = np.zeros(A_eq.shape[0]) b_eq[-1] = 1. print 'Simplex method input assembled succesfully.' print 'Dimensions of vector c:', c.shape print 'Dimensions of matrix A_eq:', A_eq.shape print 'Dimensions of vector b_eq:', b_eq.shape if self.verbose: print 'Vector c:', c print 'Matrix A_eq:', A_eq print 'Vector b_eq:', b_eq sol = lp_solve(f=c, a=A_eq, b=b_eq, e=np.zeros_like(b_eq)) w_plus = np.array(sol[1][-2 * w_dim:-w_dim]) w_minus = np.array(sol[1][-w_dim:]) w = w_plus - w_minus for i in range(len(w)): p.nodes_with_ref[unn[i]].w = w[i] if self.verbose: print 'Resulting w:', w return sol
def solve(self): ''' Attempts to solve the game. This should be called after self.width , self.height , self.num_known_horiz_constraints , self.num_known_vert_constraints , self.horiz_constraints and self.vert_constraints were filled in. Returns a two-dimensional array containing the rows of the boolean values with the solution. ''' lp_solve_params = self._calc_params_obj() lp_solve_ret = lp_solve( lp_solve_params.f_vector, lp_solve_params.a_matrix, lp_solve_params.b_vector, lp_solve_params.e_vector, lp_solve_params.lower_bounds_vector, lp_solve_params.upper_bounds_vector, lp_solve_params.xint_vector ) flat_sol = lp_solve_ret[1] if (len(flat_sol) == 0): raise "Could not find a solution for this puzzle." width = self.width height = self.height return \ [ [flat_sol[y*width+x] for x in range(width)] for y in range(height) ]
def main(): while True: value = raw_input( "Entre com valores para Tarefas e Máquinas separados:\n") newvalue = value.split() if len(newvalue) == 2 and int(newvalue[0]) > 0 and int( newvalue[1]) > 0: break print( "A entrada deve conter dois valores validos maior que 0 para Tarefas e Maquinas \n" ) #quit() nTarefas = int(newvalue[0]) mMaquinas = int(newvalue[1]) ## ------------------------------------------------------------------------------- ## Rece as horas de cada tarefa ## ------------------------------------------------------------------------------- list_h_tasks = [] for i in range(int(nTarefas)): while True: horas = int( raw_input("Entre com as horas para cada tarefa " + str(i + 1) + ":\n")) if horas > 0: list_h_tasks.append(horas) break print("A horas das tarefas devem ser maior que 0\n") ## ------------------------------------------------------------------------------- ## Recebe custo e tempo máximo para cada máquina ## ------------------------------------------------------------------------------- list_custos = [] # lista de custo para cada máquina list_h_maquinas = [] # lista de horas para cada máquina while True: for i in range(int(mMaquinas)): listn = raw_input( "Entre com os custos e tempo máximo para cada máquina " + str(i + 1) + ":\n") newList = listn.split() list_custos.append(int(newList[0])) list_h_maquinas.append(int(newList[1])) if sum(list_h_maquinas) >= sum(list_h_tasks): break print("Os valores devem ser maior que 0 para funcionar\n") ## ------------------------------------------------------------------------------- ## Leia o custo e o tempo máximo de cada máquin ## loop externo controlado por máquinas e loop interno controlado por Tarefas ## ------------------------------------------------------------------------------- list_nt_maquinas = [] ## Lista para o número de tarefas de cada máquina list_t_maquinas = [] ## Lista de tarefas por cada máquina for i in range(int(mMaquinas)): while True: soma1 = int( raw_input( "Por favor, insira o número de tarefas atribuídas à máquina " + str(i + 1) + ":\n")) if soma1 > 0: break print("Os valores não devem ser mais do que o número da tarefa\n") ## Read the number o tasks assigned to a machine list_nt_maquinas.append(soma1) list_t_maquinas.append([]) for j in range(int(list_nt_maquinas[i])): ## Read the tasks assigned to a Machine number = raw_input("Entre com as tarrefas " + str(j + 1) + " Por máquina " + str(i + 1) + ":\n") list_t_maquinas[i].append(number) #-------------------------------------------------------------------------------------- #For para criar o vetor f de coeficientes para a função objetivo #-------------------------------------------------------------------------------------- list_f = [] for i in range(int(mMaquinas)): for j in range(int(nTarefas)): x = -int(list_custos[i]) list_f.append(x) #--------------------------------------------------------------------------------------- # Conjunto de For para criar a matriz com n x m representando as restrições lineares #--------------------------------------------------------------------------------------- list_a = [] for i in range(int(mMaquinas) + int(nTarefas)): list_a.append([]) for j in range(int(mMaquinas) * int(nTarefas)): list_a[i].append(0) for i in range(int(mMaquinas)): for j in range(int(nTarefas)): list_a[i][j + nTarefas * i] = 1 for i in range(int(nTarefas)): for j in range((int(mMaquinas))): list_a[i + int(mMaquinas)][i + nTarefas * j] = 1 #--------------------------------------------------------------------------------------- # For para criar o vetor de variáveis inteiras. Pode ser omitido ou vazio e # vetor n de limites inferiores não negativos podendo ser vazio ou omitido #--------------------------------------------------------------------------------------- list_xint = [] list_vlb = [] for i in range(int(mMaquinas) * int(nTarefas)): list_xint.append(i + 1) list_vlb.append(0) #--------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------- #vetor m que determina o sentido das desigualdades: # e(i) <-1 ==> menor # e(i) = 0 ==> igual # e(i) > 1 ==> maior #--------------------------------------------------------------------------------------- list_b = [] list_e = [] for i in range(int(mMaquinas)): list_b.append(int(list_h_maquinas[i])) list_e.append(-1) for i in range(int(nTarefas)): list_b.append(int(list_h_tasks[i])) list_e.append(0) #--------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------- # print para vertificação do preenchimento dos campos no lpsolve # print ("f: ") # print (list_f) # print ("\na: ") # print (list_a) # print ("\nb: ") # print (list_b) # print ("\ne: ") # print (list_e) # print ("\nvlb: ") # print (list_vlb) # print ("\nxint: ") # print (list_xint) #--------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------- # construção e preenchimento do dos campos do lpsolve [obj, x, duas] = lp_solve(list_f, list_a, list_b, list_e, list_vlb, None, None, 0) resul = (0.0 if obj == 0 else obj * -1) #--------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------- #imprimindo o resultado count = 0 for i in range(int(mMaquinas * nTarefas)): if count == nTarefas: print("") count = 0 if isinstance(x, list): print(x[i]), else: print(x), #print (x [i]), #print (x), count += 1 print("") print resul
# Test and Demonstrate the use of lp_solve from lp_solve import * # Example 1 from the lp_solve distribution f = [-1, 2] A = [[2, 1], [-4, 4]] b = [5, 5] e = [-1, -1] xint = [1, 2] [v,x,duals] = lp_solve(f,A,b,e,None,None,xint) print v print x # Example 2 f = [50, 100] A = [[10, 5],[4, 10],[1, 1.5]] b = [2500, 2000, 450] e = [-1, -1, -1] [v,x,duals] = lp_solve(f,A,b,e) print v print x # Example 3 f = [-40, -36] vub = [8, 10] A = [[5, 3]] b = [45]
def solve(self): return lp_solve(self.optfunc, self.lhs, self.rhs, self.rel, None, None, None, 1, 0)
# Test and Demonstrate the use of lp_solve from lp_solve import * # Example 1 from the lp_solve distribution f = [-1, 2] A = [[2, 1], [-4, 4]] b = [5, 5] e = [-1, -1] xint = [1, 2] [v, x, duals] = lp_solve(f, A, b, e, None, None, xint) print v print x # Example 2 f = [50, 100] A = [[10, 5], [4, 10], [1, 1.5]] b = [2500, 2000, 450] e = [-1, -1, -1] [v, x, duals] = lp_solve(f, A, b, e) print v print x # Example 3 f = [-40, -36] vub = [8, 10] A = [[5, 3]] b = [45]
''' Created on 24.05.2012 @author: stefan ''' from lp_solve import * f=[30,40] A=[[25,75],[60,60],[68,34]] b=[450,480,476] x= lp_solve(f,A,b,[1,1,1],None, None, None, 1, 0) print x