mathematical-optimization

Method for finding strictly greater-than-zero solution for Python's Scipy Linear Programing

给你一囗甜甜゛ 提交于 2019-12-20 02:58:26
问题 Scipy NNLS perform this: Solve argmin_x || Ax - b ||_2 for x>=0. What's the alternative way to do it if I seek strictly non-zero solution (i.e. x > 0 ) ? Here is my LP code using Scipy's NNLS: import numpy as np from numpy import array from scipy.optimize import nnls def by_nnls(A=None, B=None): """ Linear programming by NNLS """ #print "NOF row = ", A.shape[0] A = np.nan_to_num(A) B = np.nan_to_num(B) x, rnorm = nnls(A,B) x = x / x.sum() # print repr(x) return x B1 = array([ 22.133, 197.087,

Speeding up Math calculations in Java

拈花ヽ惹草 提交于 2019-12-19 13:42:12
问题 I have a neural network written in Java which uses a sigmoid transfer function defined as follows: private static double sigmoid(double x) { return 1 / (1 + Math.exp(-x)); } and this is called many times during training and computation using the network. Is there any way of speeding this up? It's not that it's slow, it's just that it is used a lot, so a small optimisation here would be a big overall gain. 回答1: For neural networks, you don't need the exact value of the sigmoid function. So you

data fitting an ellipse in 3D space

ぃ、小莉子 提交于 2019-12-19 11:39:08
问题 Forum I've got a set of data that apparently forms an ellipse in 3D space (not an ellipsoid, but a curve in 3D). Being inspired by following thread http://au.mathworks.com/matlabcentral/newsreader/view_thread/65773 and with the help from someone ,I manage to get the optimization code running and outputs a set of best parameters x (vector). However, when I try to use this x to replicate the ellipse,the outcomes is a strange straight line in the space. I have been stacked on this for days.,

Minimum finding for univariate nonlinear function in Java

倖福魔咒の 提交于 2019-12-19 09:28:14
问题 I'm looking for a simple way to accomplish in Java what MATLAB's fminsearch() does. I don't need to be as general as fminsearch, in my case I only want to find the minimum (function and parameter values at minimum) of a single-variable nonlinear function. I don't know the analytical expression of the function, but I can evaluate it easily. Do you know of a library that performs this, or of an easy algorithm I could re-implement? Note: I saw that apache's common-math seems to have something

Access all variables occurring in a pyomo constraint

偶尔善良 提交于 2019-12-19 04:51:32
问题 I am working on an algorithm in python that needs to modify concrete (mixed-integer nonlinear) pyomo models. In particular, I need to know which variables are present in a general algebraic constraint . E.g. for a constraint model.con1 = Constraint(expr=exp(model.x_1) + 2*model.x_2 <= 2) I would like to make a a query (like model.con1.variables ) which returns (a list of) the variables ( [model.x_1,model.x_2] ). In this documentation I find that for linear constraints, the parameter variables

Find positive solutions to underdetermined linear system of equations

大城市里の小女人 提交于 2019-12-19 03:31:14
问题 I'm a bit new to matlab so sorry if this is horribly simple. Consider a problem of the following: Find x_1, x_2, x_3 > 0 such that 67.5 = 60*x_1 + 90*x_2 + 120*x_3 and 60 = 30*x_1 + 120*x_2 + 90*x_3 In this case I want the solution 0 < x_3 < 3/7, x_2 = 7/20 - 4/10*x_3, and x_1 = 2/5 - 7/5*x_3 Is there a easy way to make Matlab solve such a problem for me? 回答1: The easy answer, since you just need non-negativity constraints on the parameters, is to use lsqnonneg. lsqlin is not needed at all

optimized grid for rectangular items

我的未来我决定 提交于 2019-12-19 03:22:48
问题 I have N rectangular items with an aspect ratio Aitem (X:Y). I have a rectangular display area with an aspect ratio Aview The items should be arranged in a table-like layout (i.e. r rows, c columns). what is the ideal grid rows x columns, so that individual items are largest? (rows * colums >= N, of course - i.e. there may be "unused" grid places). A simple algorithm could iterate over rows = 1..N, calculate the required number of columns, and keep the row/column pair with the largest items.

Knapsack algorithm restricted to N-element solution

丶灬走出姿态 提交于 2019-12-18 18:08:23
问题 This excerpt from the CRAN documentation for the adagio function knapsack() functions as expected -- it solves the knapsack problem with profit vector p , weight vector w , and capacity cap , selecting the subset of elements with maximum profit subject to the constraint that the total weight of selected elements does not exceed the capacity. library(adagio) p <- c(15, 100, 90, 60, 40, 15, 10, 1) w <- c( 2, 20, 20, 30, 40, 30, 60, 10) cap <- 102 (is <- knapsack(w, p, cap)) How can I add a

How to check NaN in gradients in Tensorflow when updating?

扶醉桌前 提交于 2019-12-18 17:07:14
问题 All, When you train a large model with large amount samples, some samples may be cause NaN gradient when parameter updating. And I want to find these samples out. And meanwhile I don't want this batch samples' gradient to update model's parameter, because it may be cause model's parameter being NaN. So dose anyone have good idea to deal with this problem? My code is like below: # Create an optimizer. params = tf.trainable_variables() opt = tf.train.AdamOptimizer(1e-3) gradients = tf.gradients

Connect nodes to maximize total edge weight

倖福魔咒の 提交于 2019-12-18 14:08:09
问题 I am working on a problem which could be reduced to a graph optimization problem as below. A set of colored nodes is given. They are all unconnected i.e. there is no edge in the graph. The edges are to be inserted between the nodes. A node can have only 4 edges at max. A table provides rules for profit contribution from the edges. Eg., An edge connecting red to red: profit is 10 An edge connecting red to blue: profit is 20 The total number of nodes is around 100. The total number of colors is