Hi nlopt commonity, I'm new at python and currently i want to solve a non linear optimization problem.
I've done it using fmincon in MATLAB yet my other codes are written in python and thus I also want to solve the problem using python to improve connectivity between my codes. I try to solve the optimization using SciPy module, but with no fruition. And in that circumstance, I bump into nlopt. I largerly understand how the nlopt works but running my code, i still bump with "nlopt invalid argument". I don't know where my code goes wrong, and decides to ask nlopt commonity I hope you can help me to solve this problem. My python code that include optimization problem is attached. Thank you -- ===================================================== Taufiq Suryo Nugroho, PhD Research Assistant Institut Teknologi Bandung ===================================================== email : taufiq.su...@gmail.com <tausn_su...@yahoo.com>
# -*- coding: utf-8 -*- """ Created on Wed Apr 8 18:37:47 2020 @author: Taufiq Nugroho """ import numpy as np import nlopt #import data p_a_ij = np.load('p_a_ij.npy') num_od = 4 num_path = 4 num_link = 10 ob_link_flow = np.array([84,70,95,61]) ob_index = np.arange (7,8) tij_est = np.ones (num_od) #create function to calculate objective function and def Est_link_flow (tij_est,p_a_ij,num_path,num_od,num_link): temp_tij_r = np.zeros ([num_od * num_path, num_link]) for nl in range (0,num_link): pc = 0 #pc = path counter for od in range (0,num_od): for p in range (0,num_path): temp_tij_r [pc+p,nl] = tij_est [od] * p_a_ij [pc+p,nl] pc = pc + num_path est_flow = np.sum (temp_tij_r,1) return est_flow def Objective_f (tij_est,grad): if grad.size > 0: [] S_Tij = np.sum ([tij_est * np.log(tij_est) - tij_est],1) S_Tij = float (S_Tij) return S_Tij def Constraints (tij_est,grad,ob_link_flow,est_flow_hand,ob_index): if grad.size > 0: [] est_flow = est_flow_hand (tij_est) est_flow_indexed = est_flow[ob_index] ceq = ob_link_flow - est_flow_indexed return ceq est_link_flow_hand = lambda tij_est: Est_link_flow(tij_est, p_a_ij, num_path, num_od, num_link) est_link_flow = est_link_flow_hand (tij_est) #optimization process tij_0 = np.array([1,1,1,1]) opt = nlopt.opt(nlopt.LN_COBYLA,4) opt.set_min_objective(Objective_f) opt.set_lower_bounds(np.zeros(len(tij_est))) opt.add_equality_constraint(lambda tij_est,grad: Constraints(tij_est,grad,ob_link_flow,est_link_flow_hand,ob_index)) opt.set_xtol_rel(1e-4) xopt = opt.optimize(tij_est) minf = opt.last_optimum_value()
_______________________________________________ NLopt-discuss mailing list NLopt-discuss@ab-initio.mit.edu http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/nlopt-discuss