简体   繁体   English

生成满足线性约束的随机点

[英]Generate random points satisfying linear constraints

In my problem, I have a vector x of len N. Where each element x i,j is the price of the product i in the country j.在我的问题中,我有一个长度为 N 的向量x 。其中每个元素x i,j是产品 i 在国家 j 的价格。 Let's say that I have 100 products and 20 countries, so N=100x20=2000.假设我有 100 种产品和 20 个国家/地区,所以 N=100x20=2000。

The solution of X is subject to a set of linear constraints. X的解受到一组线性约束。 For instance, minimum/maximum price for each product and maximum difference allowed for the same product between countries.例如,每种产品的最低/最高价格以及同一产品在国家之间允许的最大差异。 Therefore, I can define the constraints as a matrix Ax<=b因此,我可以将约束定义为矩阵Ax<=b

I guess the problem would be like sampling points from a space bounded by hyperplanes defined by the constraints.我猜这个问题就像从由约束定义的超平面限定的空间中采样点一样。

Assuming that the problem has multiple feasible solutions.假设问题有多种可行解。 How can I generate random points (solutions of the vector x ) that satisfy the constraints?如何生成满足约束的随机点(向量x的解)? Or there is any library that could help me with that?或者有任何图书馆可以帮助我吗?

I tried with https://github.com/python-constraint/python-constraint , but it seems that because the number of solutions is very large, the algorithm gets stuck at some point or takes a long time to return the solution.我尝试使用https://github.com/python-constraint/python-constraint ,但似乎因为解决方案的数量非常多,算法会在某个点卡住或需要很长时间才能返回解决方案。

Maybe I'm missing something, or you simplified a bit too much your actual use case.也许我遗漏了一些东西,或者你简化了你的实际用例。 But for the case as stated there's no need for Constraint Programming:但是对于上述情况,不需要约束编程:

  • You have min_price , max_price , and max_diff for each product (I'm assuming max_diff <= max_price - min_price )每个产品都有min_pricemax_pricemax_diff (我假设max_diff <= max_price - min_price
  • So the actual minimum price you can set will be anywhere between min_price and max_price - max_diff .因此,您可以设置的实际最低价格将介于min_pricemax_price - max_diff之间。 Let's say you set it at random in that range假设您在该范围内随机设置
  • Accordingly the actual maximum price will be actual_min + max_diff因此,实际最高价格将是actual_min + max_diff
  • Now the price of that product for each country will simply be a value between actual_min and actual_max .现在,每个国家/地区的该产品的价格将只是介于actual_minactual_max之间的值。

I implemented this in a 3-steps process: create (random) data for the product (you will skip this one);我在一个 3 步过程中实现了这一点:为产品创建(随机)数据(你将跳过这个); compute the actual min/max values;计算实际的最小值/最大值; and finally assign the prices for each product/country.最后分配每个产品/国家的价格。 At about 1300 solutions per second on my old i5 windows notebook for 100 products and 20 countries, it is even not so slow as one could have expected在我的旧 i5 windows 笔记本上每秒大约 1300 个解决方案,适用于 100 种产品和 20 个国家/地区,它甚至没有预期的那么慢

from dataclasses import dataclass
from random import choices, randint

@dataclass
class Product:
    min_price : int
    max_price : int
    max_diff : int
    actual_min : int = 0
    actual_max : int = 0

class Prices():
    def __init__(self, no_products, no_countries):
        self.products = {}
        for i in range(no_products):
            min_price = randint(100,200)
            max_price = min_price + randint(200,300)
            max_diff = randint(10,max_price - min_price)
            self.products[i] = Product(min_price, max_price, max_diff)
        self.countries = [c for c in range(no_countries)]
        self.prices = []

    def calc_actuals(self):
        for p in self.products.values():
            p.actual_min = randint(p.min_price, p.max_price - p.max_diff)
            p.actual_max = p.actual_min + p.max_diff

    def calc_prices(self):
        self.prices = []
        for p in self.products.values():
            self.prices.append([*choices(range(p.actual_min, p.actual_max+1),k=len(self.countries))])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM