python上的行和列限制

python上的行和列限制,python,list,conditional-statements,Python,List,Conditional Statements,我有一个需要修改的列表m 我需要每行的和大于A,每列的和小于B 我有类似的东西 x = 5 #or other number, not relevant rows = len(m) cols = len(m[0]) for r in range(rows): while sum(m[r]) < A: c = randint(0, cols-1) m[r][c] += x for c in range(cols): can

我有一个需要修改的列表
m

我需要每行的和大于
A
,每列的和小于
B

我有类似的东西

x = 5     #or other number, not relevant
rows = len(m)
cols = len(m[0])

for r in range(rows): 
    while sum(m[r]) < A:  
        c = randint(0, cols-1)
        m[r][c] += x 

for c in range(cols): 
    cant = sum([m[r][c] for r in range(rows)])
    while cant > B:
        r = randint(0, rows-1)
        if m[r][c] >= x:  #I don't want negatives
            m[r][c] -= x
我可能需要添加

这是为了生成遗传算法的随机初始种群,限制条件是使其成为可能的解决方案,我需要运行80次,以获得不同的可能解决方案

import numpy as np

val = B / len(m) # column sums <= B
assert val * len(m[0]) >= A # row sums >= A

# create array shaped like m, filled with val
arr = np.empty_like(m)
arr[:] = val
将numpy导入为np
val=B/len(m)#列和=A#行和>=A
#创建形状像m的数组,填充val
arr=np.空的(m)
arr[:]=val
我选择忽略
m
的原始内容-无论如何,在您的示例中都是零。

来自随机导入*
from random import *

m = [[0,0,0],
    [0,0,0]]
A = 20
B = 25

x = 1     #or other number, not relevant
rows = len(m)
cols = len(m[0])

def runner(list1, a1, b1, x1):
    list1_backup = list(list1)
    rows = len(list1)
    cols = len(list1[0])

    for r in range(rows): 
        while sum(list1[r]) <= a1:  
            c = randint(0, cols-1)
            list1[r][c] += x1

    for c in range(cols): 
        cant = sum([list1[r][c] for r in range(rows)])
        while cant >= b1:
            r = randint(0, rows-1)
            if list1[r][c] >= x1:  #I don't want negatives
                list1[r][c] -= x1
    good_a_int = 0
    for r in range(rows):
        test1 = sum(list1[r]) > a1
        good_a_int += 0 if test1 else 1

    if good_a_int == 0:
        return list1
    else:
        return runner(list1=list1_backup, a1=a1, b1=b1, x1=x1)

m2 = runner(m, A, B, x)
for row in m:
    print ','.join(map(lambda x: "{:>3}".format(x), row))
m=[[0,0,0], [0,0,0]] A=20 B=25 x=1#或其他数字,不相关 行=长度(m) cols=len(m[0]) def流道(列表1、a1、b1、x1): 列表1\u备份=列表(列表1) 行=len(列表1) cols=len(列表1[0]) 对于范围内的r(行): 当总和(列表1[r])=b1时: r=randint(0,第1行) 如果list1[r][c]>=x1:#我不想要底片 列表1[r][c]-=x1 好的\u a\u int=0 对于范围内的r(行): test1=sum(列表1[r])>a1 如果test1为1,则good_a_int+=0 如果good_a_int==0: 返回列表1 其他: 返回运行程序(列表1=列表1\u备份,a1=a1,b1=b1,x1=x1) m2=转轮(m,A,B,x) 对于以m为单位的行: 打印“,”.join(映射(lambda x:“{:>3}.”格式(x),行))
类似于这样的东西应该是关键:

import numpy
from scipy.optimize import linprog

A = 10
B = 20
m = 2
n = m * m

# the coefficients of a linear function to minimize.
# setting this to all ones minimizes the sum of all variable
# values in the matrix, which solves the problem, but see below.
c = numpy.ones(n)

# the constraint matrix.
# This is matrix-multiplied with the current solution candidate
# to form the left hand side of a set of normalized 
# linear inequality constraint equations, i.e.
#
# x_0 * A_ub[0][0] + x_1 * A_ub[0][1] <= b_0
# x_1 * A_ub[1][0] + x_1 * A_ub[1][1] <= b_1
# ...
A_ub = numpy.zeros((2 * m, n))

# row sums. Since the <= inequality is a fixed component,
# we just multiply everthing by (-1), i.e. we demand that
# the negative sums are smaller than the negative limit -A.
#
# Assign row ranges all at once, because numpy can do this.
for r in xrange(0, m):
    A_ub[r][r * m:(r + 1) * m] = -1

# We want that the sum of the x  in each (flattened)
# column is smaller than B
#
# The manual stepping for the column sums in row-major encoding
# is a little bit annoying here.
for r in xrange(0, m):
    for j in xrange(0, m):
        A_ub[r + m][r + m * j] = 1

# the actual upper limits for the normalized inequalities.
b_ub = [-A] * m + [B] * m

# hand the linear program to scipy
solution = linprog(c, A_ub=A_ub, b_ub=b_ub)

# bring the solution into the desired matrix form
print numpy.reshape(solution.x, (m, m))
导入numpy
从scipy.optimize导入linprog
A=10
B=20
m=2
n=m*m
#要最小化的线性函数的系数。
#将其设置为“所有1”可最小化所有变量的总和
#矩阵中的值,这解决了问题,但请参见下文。
c=整数(n)
#约束矩阵。
#该矩阵乘以当前候选解决方案
#形成一组规范化的
#线性不等式约束方程,即。
#

#x_0*A_ub[0][0]+x_1*A_ub[0][1]仅仅通过选择随机数创建这样一个矩阵可能需要很长时间。这个问题可以解决吗?如果A=100,B=1,那么没有矩阵符合条件。我可以控制A和B的值,让我们假设它是可解的:B你能提供一个给定输入和期望输出的小样本片段吗?对我来说,看起来像是一个带有
变量和
2m
不等式方程的线性程序。你可能会想要。将矩阵展开为
向量,并优化所有值的最小/最大和。虽然很好,但是看起来OP想要形成一个从遗传样本到约束的初始矩阵。统一矩阵在某种程度上违背了这一目的…使用.random.random更好,这对非平方矩阵也有效吗?我删除了它,因为
c
不是起始向量,而是包含目标函数的系数。如果将其随机化,则为变量赋予随机权重。至于非平方矩阵:它需要一些更新,但基本函数是相同的。
import numpy
from scipy.optimize import linprog

A = 10
B = 20
m = 2
n = m * m

# the coefficients of a linear function to minimize.
# setting this to all ones minimizes the sum of all variable
# values in the matrix, which solves the problem, but see below.
c = numpy.ones(n)

# the constraint matrix.
# This is matrix-multiplied with the current solution candidate
# to form the left hand side of a set of normalized 
# linear inequality constraint equations, i.e.
#
# x_0 * A_ub[0][0] + x_1 * A_ub[0][1] <= b_0
# x_1 * A_ub[1][0] + x_1 * A_ub[1][1] <= b_1
# ...
A_ub = numpy.zeros((2 * m, n))

# row sums. Since the <= inequality is a fixed component,
# we just multiply everthing by (-1), i.e. we demand that
# the negative sums are smaller than the negative limit -A.
#
# Assign row ranges all at once, because numpy can do this.
for r in xrange(0, m):
    A_ub[r][r * m:(r + 1) * m] = -1

# We want that the sum of the x  in each (flattened)
# column is smaller than B
#
# The manual stepping for the column sums in row-major encoding
# is a little bit annoying here.
for r in xrange(0, m):
    for j in xrange(0, m):
        A_ub[r + m][r + m * j] = 1

# the actual upper limits for the normalized inequalities.
b_ub = [-A] * m + [B] * m

# hand the linear program to scipy
solution = linprog(c, A_ub=A_ub, b_ub=b_ub)

# bring the solution into the desired matrix form
print numpy.reshape(solution.x, (m, m))