def pm_expand(constr_list): """ Description ----------- Expands functions which are implemented using partial minimization descriptions. constr_list: cvxpy_list of constraints. Arguments --------- constr_list: cvxpy_list of constraints. """ new_list = cvxpy_list([]) for c in constr_list: if(c.left.type == TREE and c.left.item.type == FUNCTION and c.left.item.expansion_type == PM): new_constr = transform(expand(c.left.item._pm_expand(c))) new_constr = pm_expand(new_constr._get_convex()) new_list += new_constr elif(c.left.type == EXPRESSION and c.right.type == SET and c.right.expansion_type == PM): new_constr = transform(expand(c.right._pm_expand(c))) new_constr = pm_expand(new_constr._get_convex()) new_list += new_constr else: new_list += cvxpy_list([c]) # Return new list return new_list
def pm_expand(constr_list): """ Description ----------- Expands functions which are implemented using partial minimization descriptions. constr_list: cvxpy_list of constraints. Arguments --------- constr_list: cvxpy_list of constraints. """ new_list = cvxpy_list([]) for c in constr_list: if (c.left.type == TREE and c.left.item.type == FUNCTION and c.left.item.expansion_type == PM): new_constr = transform(expand(c.left.item._pm_expand(c))) new_constr = pm_expand(new_constr._get_convex()) new_list += new_constr elif (c.left.type == EXPRESSION and c.right.type == SET and c.right.expansion_type == PM): new_constr = transform(expand(c.right._pm_expand(c))) new_constr = pm_expand(new_constr._get_convex()) new_list += new_constr else: new_list += cvxpy_list([c]) # Return new list return new_list
def STA(funfcn, Best, SE, Range, Iterations): alpha_max = 1 alpha_min = 1e-4 alpha = alpha_max beta = 1 gamma = 1 delta = 1 fc = 2 history = np.empty((Iterations, 1)) fBest = funfcn(Best[0]) # 用一种奇怪的方式调用矩阵中的数 for iter in range(Iterations): Best, fBest = expand(funfcn, Best, fBest, SE, Range, beta, gamma) Best, fBest = rotate(funfcn, Best, fBest, SE, Range, alpha, beta) Best, fBest = axesion(funfcn, Best, fBest, SE, Range, beta, delta) history[iter] = fBest alpha = alpha / fc if alpha > alpha_min else alpha_max return Best, fBest, history
def scp(p,bad_constr,sol): """ Description: Sequential convex programming algorithm. Solve program p and try to enforce equality on the bad constraints. Argument p: cvxpy_program is assumed to be in expanded and equivalent format. Argument bad_constr: List of nonconvex constraints, also in expanded and equivalent format. Argument sol: Solution of relaxation. """ # Quit if program is linear if(len(bad_constr) == 0): if(not p.options['quiet']): print 'Tightening not needed' return True # Get parameters tight_tol = p.options['SCP_ALG']['tight tol'] starting_lambda = p.options['SCP_ALG']['starting lambda'] max_scp_iter = p.options['SCP_ALG']['max scp iter'] lambda_multiplier = p.options['SCP_ALG']['lambda multiplier'] max_lambda = p.options['SCP_ALG']['max lambda'] top_residual = p.options['SCP_ALG']['top residual'] # Construct slacks slacks = [abs(c.left-c.right) for c in bad_constr] # Print header if(not p.options['quiet']): print 'Iter\t:', print 'Max Slack\t:', print 'Objective\t:', print 'Solver Status\t:', print 'Pres\t\t:', print 'Dres\t\t:', print 'Lambda Max/Min' # SCP Loop lam = starting_lambda*np.ones(len(slacks)) for i in range(0,max_scp_iter,1): # Calculate max slack max_slack = max(map(lambda x:x.get_value(), slacks)) # Quit if status is primal infeasible if(sol['status'] == 'primal infeasible'): if(not p.options['quiet']): print 'Unable to tighten: Problem became infeasible' return False # Check if dual infeasible if(sol['status'] == 'dual infeasible'): sol['status'] = 'dual inf' sol['primal infeasibility'] = np.NaN sol['dual infeasibility'] = np.NaN # Print values if(not p.options['quiet']): print '%d\t:' %i, print '%.3e\t:' %max_slack, print '%.3e\t:' %p.obj.get_value(), print ' '+sol['status']+'\t:', if(sol['primal infeasibility'] is not np.NaN): print '%.3e\t:' %sol['primal infeasibility'], else: print '%.3e\t\t:' %sol['primal infeasibility'], if(sol['dual infeasibility'] is not np.NaN): print '%.3e\t:' %sol['dual infeasibility'], else: print '%.3e\t\t:' %sol['dual infeasibility'], print '(%.1e,%.1e)' %(np.max(lam),np.min(lam)) # Quit if max slack is small if(max_slack < tight_tol and sol['status'] == 'optimal'): if(not p.options['quiet']): print 'Tightening successful' return True # Quit if residual is too large if(sol['primal infeasibility'] >= top_residual or sol['dual infeasibility'] >= top_residual): if(not p.options['quiet']): print 'Unable to tighten: Residuals are too large' return False # Linearize slacks linear_slacks = [] for c in bad_constr: fn = c.left.item args = c.left.children right = c.right line = fn._linearize(args,right) linear_slacks += [line] # Add linearized slacks to objective sum_lin_slacks = 0.0 for j in range(0,len(slacks),1): sum_lin_slacks += lam[j]*linear_slacks[j] if(p.action == MINIMIZE): new_obj = p.obj + sum_lin_slacks else: new_obj = p.obj - sum_lin_slacks new_t0, obj_constr = expand(new_obj) new_p = prog((p.action,new_t0), obj_constr+p.constr,[],p.options) # Solve new problem sol = solve_convex(new_p,'scp') # Update lambdas for j in range(0,len(slacks),1): if(slacks[j].get_value() >= tight_tol): if(lam[j] < max_lambda): lam[j] = lam[j]*lambda_multiplier # Maxiters reached if(not p.options['quiet']): print 'Unable to tighten: Maximum iterations reached' if(sol['status'] == 'optimal'): return True else: return False
#personal pronouns for letter in ["m", "t", "s", "n", "v"]: if term[0] == letter: pos[-1].append(["pers"]) if term == "ego": pos[-1].append(["pers"]) #Phase: Select final_list = [] report = [] #print pos ind = 0 for pts in pos: #pts = possible terms: ["ducet",[duco,ducere,...],[do,dare,...]] ind += 1 if ind < len(pos): nextword = pos[ind][0] else: nextword = "finish" [final, rep] = expand(pts, nextword) final_list.append(final) report.append(rep) #Phase: Format for col in range(len(final_list)): #dct compression stem = final_list[col][1].split(",") if len(stem) > 1 and len(stem[1]) >= 3: if stem[1][-3:] == "are": final_list[col][1] = stem[0] + "(1)" elif stem[1][-3:] == "ari": final_list[col][1] = stem[0] + "(1D)" elif not "-" in stem and stem[1][-2:] == "re" and [ stem[0][-2:], stem[1][-3] ] != ["eo", "i"]: root = stem[1][:-3] [two, three, four] = ["-" + stem[1][-3:], stem[2], stem[3]] if len(stem[2]) > len(root) and root == stem[2][:len(root)]: