QUBO model + 2023 mathorcup competition question A

some nouns

Ising model, Ising model

QUBO model, Quadratic Unconstrained Binary Optimization, quadratic unconstrained binary optimization model

  • The value of the domain space of the spin variable is Ising空间, that is, the domain space is { − 1 , 1 } \{-1,1\}{ 1,1}

  • For binary variables, the value of its domain space is 布尔空间, that is, the domain space is { 0 , 1 } \{0,1\}{ 0,1}

HOBO, High Order Binary Optimization, high-order binary optimization problem

Combinatorial optimization problem, combinatorial optimization (Combinatorial Optimization, CO)

Quadratic Unconstrained Binary Optimization (QUBO) Model

Paper reference: Quantum bridge analytics I: a tutorial on formulating and using QUBO models | SpringerLink

Many combinatorial optimization problems can be reformulated as QUBOmodels, and different types of constraint relations can be embodied in "unconstrained" formulations in a very natural way using penalty functionsQUBO . In QUBOthe formula, using the penalty function can produce a more accurate model representation (this sentence is not well understood, it is translated).

The QUBO model is used to solve NP-hard problems, and the method used to design an exact solver that finds an "optimal" solution is basically impossible except for very small instances of the problem . The model uses modern metaheuristics to find a solution that is close to the optimal solution in a limited amount of time.

QUBO basic model

The simplest QUBOmodel can be expressed as:

min ⁡ y = x t Q x \min \quad y=\bold{x}^t \bold{Q} \bold{x} miny=xt Qx
wherex \bold{x}x is a vectorof binary decision variables,Q \bold{Q}Q ismatrix

Here the constant square matrix Q \mathrm{Q}Q is generally used in optimization problems, without loss of generality, usingsymmetric matricesorupper/lower triangular

Expand the above and rewrite it into a scalar form as follows:
min ⁡ ∑ Q ijxixj \min \sum Q_{ij} x_i x_jminQijxixj

由于 x i ∈ { 0 , 1 } x_i \in \{0,1\} xi{ 0,1 } belongs to binary variables, there isxi = xi 2 x_i = x_i^2xi=xi2, so for a more general QUBOmodel it is expressed as follows:

min ⁡ ∑ ( i , j ) ∈ E Q i j x i x j + ∑ i ∈ X c i x i \min \sum_{(i, j) \in \mathcal{E}} Q_{i j} x_i x_j+\sum_{i \in \mathcal{X}} c_i x_i min(i,j)EQijxixj+iXcixi
in,

  • x i ∈ { 0 , 1 } , i ∈ X : = { 1 , … , X } x_i \in\{0,1\}, i \in \mathcal{X}:=\{1, \ldots, X\} xi{ 0,1},iX:={ 1,,X } is a binary decision variable, andE : = { ( i , j ) ∣ i , j ∈ X , i ≠ j } \mathcal{E}:=\{(i, j) \mid i, j \in \ mathcal{X}, i \neq j\}E:={(i,j)i,jX,i=j}
  • Q i j ∈ R , ( i , j ) ∈ E Q_{i j} \in \mathbb{R},(i, j) \in \mathcal{E} QijR,(i,j)E , isQUBOthe quadratic term coefficient of the objective function.
  • c i ∈ R , i ∈ X c_i \in \mathbb{R}, i \in \mathcal{X} ciR,iX , isQUBOthe coefficient of the first-order (linear) term of the objective function.

QUBOThe problem can be represented equivalently using Isingthe model, through the variable yi = 1 − 2 xi y_i = 1-2x_iyi=12x _iTransformation, QUBOthe decision variable domain space { 0 , 1 } \{ 0,1 \} in the original model{ 0,1 } is mapped toIsingthe model decision variable domain space{ − 1 , + 1 } \{ -1 , +1 \}{ 1,+1}。转变后的Ising模型表述如下:
min ⁡ ∑ ( i , j ) ∈ E J i j y i y j + ∑ i ∈ X h i y i , J i j = − 1 4 Q i j , h i = − 1 2 ( c i + ∑ j ∈ X Q i j ) , \begin{aligned} & \min \sum_{(i, j) \in \mathcal{E}} J_{i j} y_{i} y_{j}+\sum_{i \in \mathcal{X}} h_{i} y_{i}, \\ & J_{i j}=-\frac{1}{4} Q_{i j}\quad, h_{i}=-\frac{1}{2}\left(c_{i}+\sum_{j \in \mathcal{X}} Q_{i j}\right), \end{aligned} min(i,j)EJijyiyj+iXhiyi,Jij=41Qij,hi=21 ci+jXQij ,
其中, y i ∈ { − 1 , 1 } , i ∈ X y_{i} \in\{-1,1\}, i \in \mathcal{X} yi{ 1,1},iX

A small case in the paper

Optimization problem: Minimizing a quadratic objective function yy of binary variablesy
m i n y = − 5 x 1 − 3 x 2 − 8 x 3 − 6 x 4 + 4 x 1 x 2 + 8 x 1 x 3 + 2 x 2 x 3 + 10 x 3 x 4 min \quad y=-5 x_1-3 x_2-8 x_3-6 x_4+4 x_1 x_2+8 x_1 x_3+2 x_2 x_3+10 x_3 x_4 miny=- 5 x13x _28 x36x _4+4x _1x2+8 x1x3+2x _2x3+10x3x4
where the variable xi ∈ { 0 , 1 } x_i \in \{ 0 , 1 \}xi{ 0,1 } . It has alinear part(− 5 x 1 − 3 x 2 − 8 x 3 − 6 x 4 -5 x_1-3 x_2-8 x_3-6 x_4- 5 x13x _28 x36x _4) sum one quadratic part ( 4 x 1 x 2 + 8 x 1 x 3 + 2 x 2 x 3 + 10 x 3 x 4 4 x_1 x_2+8 x_1 x_3+2 x_2 x_3+10 x_3 x_44x _1x2+8 x1x3+2x _2x3+10x3x4), since the binary variable xj x_jxjSatisfy xj = xj 2 x_j=x_j^2xj=xj2, so the linear part can be written as:

− 5 x 1 2 − 3 x 2 2 − 8 x 3 2 − 6 x 4 2 -5 x_1^2-3 x_2^2-8 x_3^2-6 x_4^2 - 5 x123x _228 x326x _42
Based on this, the minimization objective function of the optimization model is written in matrix form:

m i n y = [ x 1 x 2 x 3 x 4 ] [ − 5 2 4 0 2 − 3 1 0 4 1 − 8 5 0 0 5 − 6 ] [ x 1 x 2 x 3 x 4 ] = x t Q x min \quad y=\left[\begin{array}{llll} x_1 & x_2 & x_3 & x_4 \end{array}\right]\left[\begin{array}{cccc} -5 & 2 & 4 & 0 \\ 2 & -3 & 1 & 0 \\ 4 & 1 & -8 & 5 \\ 0 & 0 & 5 & -6 \end{array}\right]\left[\begin{array}{l} x_1 \\ x_2 \\ x_3 \\ x_4 \end{array}\right] =\bold{x}^t \bold{Q} \bold{x} miny=[x1x2x3x4] 5240231041850056 x1x2x3x4 =xt Qx

Due to the small size of the problem, exhaustion ( 2 4 = 16 2^4=1624=16 possibilities) Find the optimal solution to this problem:y = − 11 , x 1 = x 4 = 1 , x 2 = x 3 = 0 ) y=-11,x_1=x_4=1,x_2=x_3= 0)y=11,x1=x4=1,x2=x3=0)

Use the library in python wildqat( pip install wildqat, there is an error in this method, there is a problem, because it depends on python3.6the version, the higher version of the python library is not compatible, there is a problem with the installation)

import wildqat as wq
a = wq.opt()
a.qubo = [[-5,2,4,0],
		  [2,-3,1,0],
    	  [4,1,-8,5],
		  [0,0,5,-6]]
   a.sa()

There is a problem with this library. The optimal solution is not correct, but it can find a close to optimal solution. There may be a problem with the parameter setting.

使用PyQUBO库:[2103.01708] PyQUBO: Python Library for Mapping Combinatorial Optimization Problems to QUBO Form (arxiv.org)

Relevant code:

from pyqubo import Binary
import neal

# 定义哈密顿量
   x1, x2, x3, x4 = Binary("x1"), Binary("x2"), Binary("x3"), Binary("x4")
H = -5 * x1 - 3 * x2 - 8 * x3 - 6 * x4 + 4 * x1 * x2 + 8 * x1 * x3 + 2 * x2 * x3 + 10 * x3 * x4

# 编译哈密顿量得到一个模型
model = H.compile()

# 调用'to_qubo()'获取QUBO系数
# 其中,offset表示下面目标函数中的常数值
# qubo是系数,字典dict类型,{('x2', 'x2'): -3.0,...}表示其系数
qubo, offset = model.to_qubo()

print(qubo)
print(offset)

# 求解qubo模型
# 将Qubo模型输出为BinaryQuadraticModel,BQM来求解
bqm = model.to_bqm()

# 定义采样器,这里使用模拟退火采样器
sa = neal.SimulatedAnnealingSampler()

# 进行采样,由于该采样器机制,需要设置高一点的采样个数,这样能够确保能够获得最优值
# 对应的设置越高,时间花费越久。
sampleset = sa.sample(bqm, num_reads=10)
decoded_samples = model.decode_sampleset(sampleset)

# 将采样出来的结果按照目标函数进行排序,求出最佳(小)的采样结果
best_sample = min(decoded_samples, key=lambda x: x.energy)

# 输出采样结果
print(f"采样取值:{
       
       best_sample.sample},对应的结果为:{
       
       best_sample.energy}")

Create a QUBO model with known penalties

The general QUBO model requires variables to be binary and not contain any constraints. So if you want to actually solve some problems with constraints, you need to introduce quadratic penalties into the objective function .

classical constraints Equivalent penalty
x + y ≤ 1 x+y \leq 1 x+y1 P ( x y ) P(x y) P ( x y )
x + y ≥ 1 x + y \geq 1x+y1 P ( 1 − x − y + x y ) P(1-x-y+x y) P(1xy+xy)
x + y = 1 x+y=1 x+y=1 P ( 1 − x − y + 2 x y ) P(1-x-y+2 x y) P(1xy+2xy)
x ≤ y x \leq y xy P ( x − x y ) P(x-x y) P(xxy)
x 1 + x 2 + x 3 ≤ 1 x_1+x_2+x_3 \leq 1 x1+x2+x31 P ( x 1 x 2 + x 1 x 3 + x 2 x 3 ) P\left(x_1 x_2+x_1 x_3+x_2 x_3\right) P(x1x2+x1x3+x2x3)
x = y x=y x=y P ( x + y − 2 x y ) P(x+y-2 x y) P(x+y2xy)

Note here that P ( xy ) P(xy)P ( x y ) represents the scalarPPP times( x ∗ y ) (x*y)(xy ) , that is,P ∗ ( x ∗ y ) P*(x*y)P(xy ) , the others are similar.

Among them, the function PPP is calculated as a positive scalar penalty value, and this value must be calculated to be large enough. Because, inQUBO, its constraints are implemented by the optimizer, therefore, the rules specified by the penalty item are: for thethe minimization problem, if it is a feasible solution, that is, the constraint condition is satisfied, the corresponding penalty item is equal to zero; A row solution, that is, a solution that does not satisfy the constraints, has a penalty term equal to some positive penalty amount. forPPWhether the value of P is reasonable can be calculated by solving the solution of the model to calculate the added penalty function. If it is 0, it means thatPPThe P value effect is OK.

Too large a penalty value can hinder the solution process, because the penalty term will overwhelm the original objective function information, making it difficult to distinguish the quality of a solution. On the other hand, too small a penalty can jeopardize the search for a viable solution. Therefore, how to determine the penalty value needs to be considered and designed.

There are also related skills for the design of constraint items, you can check the information.

solve

Many combinatorial optimization problems can be reformulated as QUBOmodels and can be solved using methods such as simulated annealing , quantum annealing, and quantum approximate optimization algorithms ( ). QAOABoth are modern metaheuristic search methods.

quantum annealing

For many proper nouns and principle details, please refer to: D-Wave System Documentation or check the information. For example 纠缠(entanglement), 耦合器(coupler), 本征谱(Eigenspectrum), 哈密顿量(Hamiltonian), 本征态(eigenstates), 初始哈密顿量(Initial Hamiltonian), 最终哈密顿量(Final Hamiltonian), 隧穿哈密顿量 (tunneling Hamiltonian), 问题哈密顿量 (problem Hamiltonian).

Quantum annealing is the process of finding the ground state (lowest energy state) based on the natural behavior of coupled qubits . The process of quantum annealing can use the time-varying Hamiltonian H ( s ) \mathcal{H}(s)H(s)来表示:
H ( s ) = A ( s ) H I − B ( s ) H P \mathcal{H}(s)=A(s) H_{I}-B(s) H_{P} H(s)=A(s)HIB(s)HP
Among them, A ( s ) A(s)A ( s ) andB ( s ) B(s)B ( s ) is the annealing path function, according to the normalized annealing times = t / tas = t / t_{a}s=t/ta, among which ta t_{a}taIndicates the total annealing time. This design ensures that:

  • This A ( 0 ) = 1 A(0)=1A(0)=1 andB ( 0 ) = 0 B(0)=0B(0)=0 H ( s ) \mathcal{H}(s) H ( s ) represents the initial stateHI H_IHI, defined by the user.
  • This A ( 1 ) = 0 A(1)=0A(1)=0 andB ( 1 ) = 0 B(1)=0B(1)=0 H ( s ) \mathcal{H}(s) H ( s ) represents the state HP after annealingH_PHP, which is also called the problem Hamiltonian ( problem Hamiltonian), is the minimum energy state.

Then when designing, the design of the initial state can be simply designed according to the problem, for example:
HI = ∑ i σ ix H_{I}=\sum_{i} \sigma_{i}^{x}HI=ipix
Among them, σ ix \sigma_{i}^{x}pixIndicates the iiPauli − x Pauli-x for i qubitsPaulix operator (Pauli− x -xx operator).

And the problem Hamiltonian, that is, HP H_PHP设计可通过最优化的目标函数给出:
H P = ∑ i , j J i j σ i z ⋅ σ j z + ∑ i h i σ i z H_{P}=\sum_{i, j} J_{i j} \sigma_{i}^{z} \cdot \sigma_{j}^{z}+\sum_{i} h_{i} \sigma_{i}^{z} HP=i,jJijσizσjz+ihiσiz
其中, σ i z \sigma_{i}^{z} σiz表示第 i i i 个量子位的 P a u l i − z Pauli-z Pauliz 算子(泡利 − z -z z 算子)。

量子退火器首先初始化量子位的叠加态使得 H ( 0 ) = H I \mathcal{H}(0)=H_{I} H(0)=HI,然后在退火时间内操作量子位耦合,使 H ( s ) \mathcal{H}(s) H ( s ) towardsHP H_{P}HPAfter development, after annealing, it is in the eigenstate (eigenstate) of problem Hamiltonian. According to the adiabatic theorem of quantum computing, if the annealing time is long enough, the time-varying Hamiltonian H ( s ) \mathcal{H}(s)H ( s ) will always maintain the ground state, that is, the optimal function (H ( s ) \mathcal{H}(s)the solution of H ( s ) ).

simulated annealing

Simulated Annealing (SA) is a general global optimization algorithm used to find the optimal solution in the search space. Its basic principle is to simulate the annealing process in physics, and control the probability of accepting suboptimal solutions during the search process through temperature parameters, so as to avoid falling into local optimal solutions. The basic flow of the simulated annealing algorithm is as follows:

  1. Initialize a solution x 0 x_0x0and an initial temperature T 0 T_0T0

  2. At this temperature, iterate, each iteration doing the following:

    a. From the current solution xxRandomly select a new solution x ′ x’in the neighborhood of xx

    b. Calculate x ′ x'x objective function valuef ( x ′ ) f(x')f(x )andxxThe objective function value of x f ( x ) f(x)f(x)

    c. Judge whether to accept the new solution according to the Metropolis criterion, that is, if f ( x ′ ) < f ( x ) f(x')<f(x)f(x)<f ( x ) , then with probability1 11 acceptsx 'x'x as the new solution; otherwise, with the probabilitye − Δ f / T e^{-\Delta f/T}eΔ f / T acceptsx ′ x’x,其中 Δ f = f ( x ′ ) − f ( x ) \Delta f=f(x')-f(x) f _=f(x)f(x)

  3. Update temperature TTT , and repeat step 2 until the temperature meets the stop condition

where the temperature TTT is a parameter that decreases over time, controlling the probability that the algorithm accepts a suboptimal solution.

From the above algorithm, it can be seen that the iterative effect is affected by three parameters: initial temperature, cooling rate and termination temperature.

  • The initial temperature is usually set to a higher value to ensure that some poor solutions can be accepted in the early stage of the search, so that there is a greater probability of jumping out of the local optimal solution.

  • The termination temperature is usually set to a small value. When the temperature drops to the termination temperature, the search stops, and the solution obtained at this time is the optimal solution obtained by the algorithm.

  • The cooling rate is usually set to a constant less than 1. The slower the cooling rate, the longer the search time, but the wider the search range, and the greater the probability of finding the global optimal solution.

Therefore, the three parameters of initial temperature, cooling rate and termination temperature need to be set reasonably so as to quickly reach the optimal solution.

Here is an introduction to the samplers in the dwave-samplersSimulatedAnnealingSampler library and the corresponding parameters.

In this library, the initial and final temperatures are calculated by 1 TK \frac{1}{TK}TK1Convert to β 1 \beta_1b1and β 0 \beta_0b0(where KKK is the Boltz constant), by passing in the parameterbeta_range = [ β 0 , β 1 ] \text{beta\_range} =[\beta_0,\beta_1]beta_range=[ b0,b1]传入。而降温速率一般通过beta_schedule_typenum_sweeps来设置:对于beta_schedule_type参数,有三种方式:

  • “linear”:线性,在 [ β 0 , β 1 ] [\beta_0,\beta_1] [β0,β1]中线性获取num_sweeps个采样点作为每次更新温度的数值
  • “geometric”,几何,在 [ β 0 , β 1 ] [\beta_0,\beta_1] [β0,β1]中通过np.geomspace(*beta_range, num=num_betas)获取num_sweeps个采样点作为每次更新温度的数值。
  • “custom”:用户自定义

量子近似优化算法(QAOA)

量子近似优化算法可以使用QPanda库,这部分不是很了解,有时间后面补充。

高阶问题(HOBO)

主要解决的问题是:对于QUBO模型中高于二次项的目标函数该如何求解问题

参考论文:[2001.00658] Compressed Quadratization of Higher Order Binary Optimization Problems (arxiv.org)

在不同论文中有不同的叫法:

  • Higher Order Binary Optimization (HOBO):高阶二进制优化
  • termwise quadratization quadratization: according to the square of the term, this is to convert the monomial into quadratically. That is xi = xi 2 , wherexi ∈ 0 , 1 x_i = x_i^2, \quad where \quad x_i \in{0,1}xi=xi2,wherexi0,1

Compressive quadraticization of high-order binary optimization problems: For now, there is a method for reducing the order of high-order optimization problems using Rosenberg polynomials , which reduces the number of terms in a Boolean space by introducing an additional variable times, this method will eventually lead to a very sparse final Boolean space matrix, and the problem size will become larger. Then in the paper, a reduction method that operates directly in the original Boolean space is proposed without adding additional variables.

There is one in the paper that I didn't understand:

insert image description here

feel the xx hereX is wrong, it should bezzz , then the meaning you want to express should be, forzzz belongs to the value of Boolean space, you can increase the variableyyy , such that whenyyWhen y takes aspecific valueh ( z , y takes a specific value) h(z,y_{takes a specific value})h(z,ytake a specific value) is equivalent tof ( z ) f(z)f ( z ) , due tohhThe h function is not sure, it just means that the function can be found to satisfy the condition of equivalence, then make another limitation, that is, whenyyWhen y takes thisspecific value, it happens to beh ( z , y takes a specific value) h(z,y_{takes a specific value})h(z,ytake a specific value) to the minimum value,

Then according to the greedy idea, it is easy to understand, when the time comes to minimize f ( z ) f(z)f ( z ) is equivalent to minimizingh ( z , y ) h(z,y)h(z,y)

Based on this, there are equivalents:

insert image description here

This is more difficult to understand, but think of xi ∈ { 0 , 1 } x_i \in \{ 0 , 1 \}xi{ 0,1 } , and here y is a combination of many, andyi ∈ { 0 , 1 } y_i \in \{ 0, 1\}yi{ 0,1 } is easier to list exhaustively, andLinear and quadratic reformulations of nonlinear optimization problems in binary variablesit is proved in the paper that for the highest numberddd , the desired variableyyThe number of y is⌈ log ⁡ d ⌉ − 1 \lceil\log d\rceil-1logd1 . (I feel that there is a problem with the expression of the formula)

HOBOThere are two problems with this substitution method. For details, please refer to the paper. We did not use variables to replace the descending order, so we did not study this carefully.

Question A of mathorcup in 2023

mathematical definition

noun mathematical expression
Loan amount LLL , fixed value, is 1000000
Interest income rate I I I , a fixed value, is 8%
Pass rate matrix, TTT Among them, T i , j T_{i,j}Ti,jIndicates the iiThe jjth of the i credit score cardpass rate of j thresholds
Bad debt ratio matrix, HHH Among them, H i , j H_{i,j}Hi,jIndicates the iiThe jjth of the i credit score cardBad debt rate of j thresholds
total pass rate P P P
Total Bad Debt Rate Q Q Q
Collection space, specifically explained in the article X \mathcal{X} X
Constraints λ \lambdal
final income H H H

Question one

For problem one, we define decision variables xi , j x_{i,j}xi,jIt is used to represent:
xi , j = { 0 , do not choose the j-th threshold of the i-th credit scorecard 1 , choose the j-th threshold of the i-th credit scorecard x_{i,j}=\left\{ \ begin{array}{c} 0 \quad , do not select the \j\th threshold of the \i\th credit scorecard \\ 1 \quad , select the \j\th threshold of the \i\th credit scorecard \\ \end{array} \right.xi,j={ 0,Unselect the jth threshold of the ith credit scorecard    1,Select the j -th     threshold of the i -th credit scorecard
Then for the decision variable xi , j x_{i,j}xi,jCorresponding pass rate P i , j P_{i,j}Pi,jand damage rate Q i , j Q_{i,j}Qi,j 有:
P i , j = x i , j   ∗   T i , j Q i , j = x i , j   ∗   H i , j P_{i,j} = x_{i,j} \ * \ T_{i,j} \\ Q_{i,j} = x_{i,j} \ * \ H_{i,j} Pi,j=xi,j  Ti,jQi,j=xi,j  Hi,j
Then the decision variable xi , j x_{i,j}xi,jThe corresponding final income H i , j H_{i,j}Hi,j
H i , j = L   ∗   I   ∗   P i , j   ∗   ( 1 − Q i , j )   −   L   ∗   P i , j   ∗   Q i , j H_{i,j} = L \ * \ I \ * \ P_{i,j} \ * \ (1-Q_{i,j}) \ - \ L \ * \ P_{i,j} \ * \ Q_{i,j} Hi,j=L  I  Pi,j  (1Qi,j)  L  Pi,j  Qi,j
Consider the whole, which is the final income HHH
H = ∑ ( i , j ) ∈ X H i , j = ∑ i = 1 100 ∑ j = 1 10 [ L   ∗   I   ∗   P i , j   ∗   ( 1 − Q i , j )   −   L   ∗   P i , j   ∗   Q i , j ] = L   ∗   I   ∗ ∑ i = 1 100 ∑ j = 1 10 P i , j − L ∗ ( I + 1 ) ∗ ∑ i = 1 100 ∑ j = 1 10 ( P i , j   ∗   Q i , j ) = L   ∗   I   ∗ ∑ i = 1 100 ∑ j = 1 10 ( T i , j   ∗   x i , j ) − L ∗ ( I + 1 ) ∗ ∑ i = 1 100 ∑ j = 1 10 ( T i , j   ∗   H i , j   ∗   x i , j 2 ) H = \sum_{ {(i, j) \in \mathcal{X}}} H_{i,j} \\ = \sum_{i=1}^{100} \sum_{j=1}^{10} [{L \ * \ I \ * \ P_{i,j} \ * \ (1-Q_{i,j}) \ - \ L \ * \ P_{i,j} \ * \ Q_{i,j}}] \\ = L \ * \ I \ * \sum_{i=1}^{100} \sum_{j=1}^{10}P_{i,j} - L*(I+1)*\sum_{i=1}^{100} \sum_{j=1}^{10}(P_{i,j} \ * \ Q_{i,j}) \\ = L \ * \ I \ * \sum_{i=1}^{100} \sum_{j=1}^{10}(T_{i,j} \ * \ x_{i,j}) - L*(I+1)*\sum_{i=1}^{100} \sum_{j=1}^{10}(T_{i,j} \ * \ H_{i,j} \ * \ x_{i,j}^2) H=(i,j)XHi,j=i=1100j=110[L  I  Pi,j  (1Qi,j)  L  Pi,j  Qi,j]=L  I i=1100j=110Pi,jL(I+1)i=1100j=110(Pi,j  Qi,j)=L  I i=1100j=110(Ti,j  xi,j)L(I+1)i=1100j=110(Ti,j  Hi,j  xi,j2)
for the final income, we consider minimizing the objective function− H -HH,即
m i n   − H = L ∗ ( I + 1 ) ∗ ∑ i = 1 100 ∑ j = 1 10 ( T i , j   ∗   H i , j   ∗   x i , j 2 ) − L   ∗   I   ∗ ∑ i = 1 100 ∑ j = 1 10 ( T i , j   ∗   x i , j ) min \ -H = L*(I+1)*\sum_{i=1}^{100} \sum_{j=1}^{10}(T_{i,j} \ * \ H_{i,j} \ * \ x_{i,j}^2) - L \ * \ I \ * \sum_{i=1}^{100} \sum_{j=1}^{10}(T_{i,j} \ * \ x_{i,j}) min H=L(I+1)i=1100j=110(Ti,j  Hi,j  xi,j2)L  I i=1100j=110(Ti,j  xi,j)
Consider constraints: since only one scorecard threshold can be selected in the whole, so in the decision variablexi , j x_{i,j}xi,jOnly one of them is 1, namely:
∑ ( i , j ) ∈ X xi , j = ∑ i = 1 100 ∑ j = 1 10 xi , j = 1 \sum_{ {( i, j) \in \mathcal{X }}} x_{i,j}= \sum_{i=1}^{100} \sum_{j=1}^{10} x_{i,j} = 1(i,j)Xxi,j=i=1100j=110xi,j=1

Here X \mathcal{X}The X space is defined as:X : = { ( i , j ) ∣ i ∈ { 1 , 2 , . . . , 100 } , j ∈ { 1 , 2 , . . . , 10 } } \mathcal{X}:= \{(i, j) \mid i \in \{1,2,...,100\}, j \in \{1,2,...,10 \} \}X:={(i,j)i{ 1,2,...,100},j{ 1,2,...,10}}

综上所述:
m i n   − H = L ∗ ( I + 1 ) ∗ ∑ i = 1 100 ∑ j = 1 10 ( T i , j   ∗   H i , j   ∗   x i , j 2 ) − L   ∗   I   ∗ ∑ i = 1 100 ∑ j = 1 10 ( T i , j   ∗   x i , j ) s . t . ∑ i = 1 100 ∑ j = 1 10 x i , j = 1 min \ -H = L*(I+1)*\sum_{i=1}^{100} \sum_{j=1}^{10}(T_{i,j} \ * \ H_{i,j} \ * \ x_{i,j}^2) - L \ * \ I \ * \sum_{i=1}^{100} \sum_{j=1}^{10}(T_{i,j} \ * \ x_{i,j}) \\ s.t. \quad \sum_{i=1}^{100} \sum_{j=1}^{10} x_{i,j} = 1 min H=L(I+1)i=1100j=110(Ti,j  Hi,j  xi,j2)L  I i=1100j=110(Ti,j  xi,j)s.t.i=1100j=110xi,j=1
通过添加约束项 λ \lambda λ Rewrite the above quadratic constrained binary optimization model into a quadratic unconstrained binary optimization model (QUBO):
min y = L ∗ ( I + 1 ) ∗ ∑ i = 1 100 ∑ j = 1 10 ( T i , j ∗ H i , j ∗ xi , j 2 ) − L ∗ I ∗ ∑ i = 1 100 ∑ j = 1 10 ( T i , j ∗ xi , j ) + λ ∗ ( ∑ i = 1 100 ∑ j = 1 10 xi , j − 1 ) 2 min \ \ y = L*(I+1)*\sum_{i=1}^{100} \sum_{j=1}^{10}(T_{i,j} \ * \ H_{i,j} \ * \ x_{i,j}^2) - L \ * \ I \ * \sum_{i=1}^{100} \sum_{j=1}^{10}(T_ {i,j} \ * \ x_{i,j}) + \lambda * (\sum_{i=1}^{100} \sum_{j=1}^{10} x_{i,j} - 1 )^2min y  =L(I+1)i=1100j=110(Ti,j  Hi,j  xi,j2)L  I i=1100j=110(Ti,j  xi,j)+l(i=1100j=110xi,j1)2

For the enumeration violence code implementation of question 1 :

import time
import pandas as pd

if __name__ == '__main__':
    # 定义一些变量
    L = 1000000  # 贷款资金
    I = 0.08  # 利息收入率

    df = pd.read_csv('./data/附件1:data_100.csv', header=0)

    # 定义最大的数
    max_value = -1

    # 定义最大数字的索引下标
    max_i = max_j = -1

    start = time.time()

    for i in range(1, 101):
        for j in range(0, 10):

            # 总通过率
            P = df[f"t_{
      
      i}"].iloc[j]

            # 总坏账率
            Q = df[f"h_{
      
      i}"].iloc[j]

            # 此时的最终收入
            temp_value = L * I * P * (1 - Q) - L * P * Q

            # print(f"选卡[{i},{j + 1}],对应最终收入{temp_value},最大收入:{max_value}")

            if temp_value > max_value:
                max_value = temp_value
                max_i = i
                max_j = j

    end = time.time()
    print(f"暴力时间:{
      
      end - start} s")

    print(f"最大值:{
      
      max_value}")
    print(f"对应的选取卡1:第{
      
      max_i}{
      
      max_j + 1}个阈值")

Use QUBOthe model + simulated annealing algorithm to achieve:

import time
from pyqubo import Array, Constraint
from dwave.samplers import SimulatedAnnealingSampler

import numpy as np
from pyqubo import Placeholder


# 在字典中找到value的key
def get_key(dict, value):
    return [k for k, v in dict.items() if v == value]


# 主函数,问题一的主要求解函数
def main():
    # 定义一些变量
    L = 1000000  # 贷款资金
    I = 0.08  # 利息收入率

    # 读取数据
    data = np.genfromtxt('./data/附件1:data_100.csv', delimiter=',', skip_header=1)

    # 获取T矩阵和H矩阵
    T = data[:, ::2]
    H = data[:, 1::2]

    # 定义二进制决策变量,10 * 100
    x = Array.create('x', shape=(10, 100), vartype='BINARY')

    # 定义惩罚项M
    M = Placeholder('M')
    M = 100000

    # 定义哈密顿量,也就是对应的目标函数
    # 注意这里不是总通过率和总坏账率,是中间变量,将最终决策目标的连加符号放到里面整出来的
    P = np.sum(np.multiply(x, T))
    Q = np.sum(np.multiply(x, H))
    H = - (L * I * P * (1 - Q) - L * P * Q) + M * Constraint((np.sum(x) - 1) ** 2, label='sum(x_i_j) = 1')

    # 编译哈密顿量得到一个模型
    model = H.compile()

    # 将Qubo模型输出为BinaryQuadraticModel,BQM来求解
    bqm = model.to_bqm()

    # 记录开始退火时间
    start = time.time()

    # 模拟退火
    sa = SimulatedAnnealingSampler()
    sampleset = sa.sample(bqm, seed=666, beta_range=[10e-10, 50], num_sweeps=10000, beta_schedule_type='geometric',num_reads=10)


    # 对数据进行筛选,对选取的数据进行选取最优的
    decoded_samples = model.decode_sampleset(sampleset)  # 将上述采样最好的num_reads组数据变为模型可读的样本数据
    best_sample = min(decoded_samples, key=lambda x: x.energy)  # 将能量值最低的样本统计出来,表示BQM的最优解

    end = time.time()
    print(f"退火时间花费:{
      
      end - start} s")

    # 统计决策变量为1的所有数据
    data_1_list = get_key(best_sample.sample, 1)

    print(f"对应的取1的决策变量有:{
      
      data_1_list}(注意是索引,从0开始),对应的能量为(最大最终收入):{
      
      - best_sample.energy}")
    
    
if __name__ == '__main__':
    main()

For problem 1, although brute force exhaustion QUBOis faster than the running time of model + simulated annealing algorithm, but considering the time complexity of the algorithm: the card has mmm sheets, the threshold selection hasnnn _

  • Brute-forcing algorithm, due to loops, is O ( mn ) O(mn)O ( mn )
  • QUBOThe model + simulated annealing algorithm is a modern meta-heuristic optimization algorithm. When there is a lot of data, especially on NP problems, the larger the scale, the faster its operation speed is.

question two

For problem two, we consider two understandings of the QUBO model:

  • Define decision variables xi , j , k x_{i,j,k}xi,j,kIndicate whether to choose: choose the ii for the first credit score cardi threshold, the second credit score card choosejjThere are j thresholds, and the third credit score card selectskkthk thresholds.
  • A discovery mainly derived from the process of violent exhaustion, the threshold selection of the first two credit score cards is fixed, and the third credit score card is definedWhether i thresholds are selected as decision variablesxi x_ixi, after determining the optimal threshold of the third credit score card, similarly determine the optimal threshold of the other two credit cards. After several iterations, the threshold determined by this method is the best combination of thresholds in this problem.

See the following for details:

SHOWER

Define decision variables xi , j , k x_{i,j,k}xi,j,kUsed to represent:
xi , j , k = { 0 , not selected: select the i threshold for the first credit score card, select the j threshold for the second credit score card, and select the k threshold for the third credit score card Threshold 1 , choose: the first credit score card chooses the i-th threshold, the second credit score card chooses the j-th threshold, and the third credit score card chooses the k-th threshold x_{i,j,k}=\ left\{ \begin{array}{c} 0 \quad , do not select: \ The first credit score card selects the \i \th threshold, the second credit score card selects the \jth threshold, and the third Select the \k\th threshold \\ 1 \quad for the credit score card, select: \The first credit score card selects the \i\th threshold, the second credit score card selects the \j\th threshold, and the third Credit score card selects the \k\th threshold\\ \end{array} \right.xi,j,k={ 0,do not choose: Select the i -th threshold for the first credit score card, the j -th threshold for the second credit score card , and the k -th threshold for the third credit score card      1,select: Select the i -th threshold for the first credit score card, the j -th threshold for the second credit score card , and the k -th       threshold for the third credit score card
Then for decision variables xi , j , k x_{i,j,k}xi,j,kCorresponding pass rate P i , j , k P_{i,j,k}Pi,j,kand damage rate Q i , j , k Q_{i,j,k}Qi,j,k 有:
P i , j , k = x i , j , k   ∗   T 1 , i   ∗   T 2 , j   ∗   T 3 , k Q i , j , k = x i , j , k   ∗   1 3   ∗   ( H 1 , i   +   H 2 , j   +   H 3 , k ) P_{i,j,k} = x_{i,j,k} \ * \ T_{1,i} \ * \ T_{2,j} \ * \ T_{3,k} \\ Q_{i,j,k} = x_{i,j,k} \ * \ \frac{1}{3} \ * \ (H_{1,i} \ + \ H_{2,j} \ + \ H_{3,k}) Pi,j,k=xi,j,k  T1,i  T2,j  T3,kQi,j,k=xi,j,k  31  (H1,i + H2,j + H3,k)
于是决策变量 x i , j , k x_{i,j,k} xi,j,k 对应的最终收入 H i , j , k H_{i,j,k} Hi,j,k
H i , j , k = L   ∗   I   ∗   P i , j , k   ∗   ( 1 − Q i , j , k )   −   L   ∗   P i , j , k   ∗   Q i , j , k H_{i,j,k} = L \ * \ I \ * \ P_{i,j,k} \ * \ (1-Q_{i,j,k}) \ - \ L \ * \ P_{i,j,k} \ * \ Q_{i,j,k} Hi,j,k=L  I  Pi,j,k  (1Qi,j,k)  L  Pi,j,k  Qi,j,k
考虑整体,也就是最终收入 H H H
H = ∑ ( i , j , k ) ∈ X H i , j , k = ∑ i = 1 10 ∑ j = 1 10 ∑ k = 1 10   [ L   ∗   I   ∗   P i , j , k   ∗   ( 1 − Q i , j , k )   −   L   ∗   P i , j , k   ∗   Q i , j , k ] = L   ∗   I   ∗ ∑ i = 1 10 ∑ j = 1 10 ∑ k = 1 10 P i , j , k − L ∗ ( I + 1 ) ∗ ∑ i = 1 10 ∑ j = 1 10 ∑ k = 1 10 ( P i , j , k   ∗   Q i , j , k ) = L   ∗   I   ∗ ∑ i = 1 10 ∑ j = 1 10 ∑ k = 1 10 [ ( T 1 , i ∗ T 2 , j ∗ T 3 , k )   ∗   x i , j , k ] − L ∗ ( I + 1 ) 3 ∗ ∑ i = 1 10 ∑ j = 1 10 ∑ k = 1 10 [ ( H 1 , i + H 2 , j + H 3 , k ) ∗ T 1 , i ∗ T 2 , j ∗ T 3 , k ∗ x i , j , k 2 ] H = \sum_{ {(i, j, k) \in \mathcal{X}}} H_{i,j,k} \\ = \sum_{i=1}^{10} \sum_{j=1}^{10} \sum_{k=1}^{10} \ [ {L \ * \ I \ * \ P_{i,j,k} \ * \ (1-Q_{i,j,k}) \ - \ L \ * \ P_{i,j,k} \ * \ Q_{i,j,k}} ]\\ = L \ * \ I \ * \sum_{i=1}^{10} \sum_{j=1}^{10} \sum_{k=1}^{10}P_{i,j,k} - L*(I+1)*\sum_{i=1}^{10} \sum_{j=1}^{10} \sum_{k=1}^{10}(P_{i,j,k} \ * \ Q_{i,j,k}) \\ = L \ * \ I \ * \sum_{i=1}^{10} \sum_{j=1}^{10} \sum_{k=1}^{10} [( T_{1,i} * T_{2,j} * T_{3,k} ) \ * \ x_{i,j,k}] \\ -\frac {L*(I+1)}{3} * \sum_{i=1}^{10} \sum_{j=1}^{10} \sum_{k=1}^{10} [(H_{1,i} + H_{2,j} + H_{3,k}) * T_{1,i} * T_{2,j} * T_{3,k} * x_{i,j,k}^2] H=(i,j,k)XHi,j,k=i=110j=110k=110 [L  I  Pi,j,k  (1Qi,j,k)  L  Pi,j,k  Qi,j,k]=L  I i=110j=110k=110Pi,j,kL(I+1)i=110j=110k=110(Pi,j,k  Qi,j,k)=L  I i=110j=110k=110[(T1,iT2,jT3,k)  xi,j,k]3L(I+1)i=110j=110k=110[(H1,i+H2,j+H3,k)T1,iT2,jT3,kxi,j,k2]
Also consider minimizing the objective function− H -HH , the constraint condition of the decision variable at this time is: (only one option is selected)
∑ ( i , j , k ) ∈ X xi , j , k = ∑ i = 1 10 ∑ j = 1 10 ∑ k = 1 10 xi , j , k = 1 \sum_{ {(i, j, k) \in \mathcal{X}}} x_{i,j,k}= \sum_{i=1}^{10} \sum_{ j=1}^{10} \sum_{k=1}^{10} x_{i,j,k} = 1(i,j,k)Xxi,j,k=i=110j=110k=110xi,j,k=1

Here X \mathcal{X}The X space is defined as:X : = { ( i , j , k ) ∣ i , j , k ∈ { 1 , 2 , . . . , 10 } } \mathcal{X}:=\{(i, j,k ) \mid i,j,k \in \{1,2,...,10\} \}X:={(i,j,k)i,j,k{ 1,2,...,10}}

综上所述:
m i n   − H = L   ∗   I   ∗ ∑ i = 1 10 ∑ j = 1 10 ∑ k = 1 10 [ ( T 1 , i ∗ T 2 , j ∗ T 3 , k )   ∗   x i , j , k ] − L ∗ ( I + 1 ) 3 ∗ ∑ i = 1 10 ∑ j = 1 10 ∑ k = 1 10 [ ( H 1 , i + H 2 , j + H 3 , k ) ∗ T 1 , i ∗ T 2 , j ∗ T 3 , k ∗ x i , j , k 2 ] s . t . ∑ i = 1 10 ∑ j = 1 10 ∑ k = 1 10 x i , j , k = 1 min \ -H = L \ * \ I \ * \sum_{i=1}^{10} \sum_{j=1}^{10} \sum_{k=1}^{10} [( T_{1,i} * T_{2,j} * T_{3,k} ) \ * \ x_{i,j,k}] \\ -\frac {L*(I+1)}{3} * \sum_{i=1}^{10} \sum_{j=1}^{10} \sum_{k=1}^{10} [(H_{1,i} + H_{2,j} + H_{3,k}) * T_{1,i} * T_{2,j} * T_{3,k} * x_{i,j,k}^2] \\ s.t. \quad \sum_{i=1}^{10} \sum_{j=1}^{10} \sum_{k=1}^{10} x_{i,j,k} = 1 min H=L  I i=110j=110k=110[(T1,iT2,jT3,k)  xi,j,k]3L(I+1)i=110j=110k=110[(H1,i+H2,j+H3,k)T1,iT2,jT3,kxi,j,k2]s.t.i=110j=110k=110xi,j,k=1
By adding the constraint termλ \lambdaλ Rewrite the above quadratic constrained binary optimization model into a quadratic unconstrained binary optimization model (QUBO):
min y = L ∗ I ∗ ∑ i = 1 10 ∑ j = 1 10 ∑ k = 1 10 [ ( T 1 , i ∗ T 2 , j ∗ T 3 , k ) ∗ xi , j , k ] − L ∗ ( I + 1 ) 3 ∗ ∑ i = 1 10 ∑ j = 1 10 ∑ k = 1 10 [ ( H 1 , i + H 2 , j + H 3 , k ) ∗ T 1 , i ∗ T 2 , j ∗ T 3 , k ∗ xi , j , k 2 ] + λ ∗ ( ∑ i = 1 10 ∑ j = 1 10 ∑ k = 1 10 xi , j , k − 1 ) 2 min \ \ y = L \ * \ I \ * \sum_{i=1}^{10} \sum_{j=1}^{10} \sum_{k= 1}^{10} [( T_{1,i} * T_{2,j} * T_{3,k} ) \ * \ x_{i,j,k}] \\ -\frac {L*( I+1)}{3} * \sum_{i=1}^{10} \sum_{j=1}^{10} \sum_{k=1}^{10} [(H_{1,i} + H_{2,j} + H_{3,k}) * T_{1,i} * T_{2,j} * T_{3,k} * x_{i,j,k}^2] \\ + \lambda * (\sum_{i=1}^{10} \sum_{j=1}^{10} \sum_{k=1}^{10} x_{i,j,k} - 1)^ 2min y  =L  I i=110j=110k=110[(T1,iT2,jT3,k)  xi,j,k]3L(I+1)i=110j=110k=110[(H1,i+H2,j+H3,k)T1,iT2,jT3,kxi,j,k2]+ l(i=110j=110k=110xi,j,k1)2

In the process of code implementation, due to the need to calculate the total pass rate T 1 , i ∗ T 2 , j ∗ T 3 , k T_{1,i} \ * \ T_{2,j} \ * \ T_{3,k}T1,i  T2,j  T3,kAnd the total bad debt rate 1 3 ∗ ( H 1 , i + H 2 , j + H 3 , k ) \frac{1}{3} * (H_{1,i} + H_{2,j} + H_{3 ,k})31(H1,i+H2,j+H3,k) . numpyIf the efficiency of using the for loop is relatively slow, the broadcast mechanism in the libraryis used here

The code is implemented as follows:

import time
from pyqubo import Array, Constraint
from dwave.samplers import SimulatedAnnealingSampler
import numpy as np
from pyqubo import Placeholder


# 在字典中找到value的key
def get_key(dict, value):
    return [k for k, v in dict.items() if v == value]


def main():
    # 定义一些变量
    L = 1000000  # 贷款资金
    I = 0.08  # 利息收入率

    # 表示选取的卡号
    card1, card2, card3 = 1, 2, 3

    # 读取数据
    data = np.genfromtxt('../data/附件1:data_100.csv', delimiter=',', skip_header=1)

    # 获取T矩阵和H矩阵
    T = data[:, ::2]
    H = data[:, 1::2]

    # 定义二进制决策变量,10 * 100
    x = Array.create('x', shape=(10, 10, 10), vartype='BINARY')

    # 定义惩罚项M
    M = Placeholder('M')
    M = 30000

    # 定义哈密顿量,也就是对应的目标函数
    # 注意这里不是总通过率和总坏账率,是中间变量,将最终决策目标的连加符号放到里面整出来的
    T1 = T[:, card1 - 1]
    T2 = T[:, card2 - 1]
    T3 = T[:, card3 - 1]

    H1 = H[:, card1 - 1]
    H2 = H[:, card2 - 1]
    H3 = H[:, card3 - 1]

    # 计算三种组合后的总通过率和总坏账率,通过广播机制来实现
    T = T1[:, None, None] * T2[None, :, None] * T3[None, None, :]
    H = (H1[:, None, None] + H2[None, :, None] + H3[None, None, :]) / 3

    P = np.sum(np.multiply(x, T))
    Q = np.sum(np.multiply(x, H))
    H = - (L * I * P * (1 - Q) - L * P * Q) + M * Constraint((np.sum(x) - 1) ** 2, label='sum(x_i_j) = 1')

    # 编译哈密顿量得到一个模型
    model = H.compile()

    # 将Qubo模型输出为BinaryQuadraticModel,BQM来求解
    bqm = model.to_bqm()

    print("开始模拟退火")

    start = time.time()
    sa = SimulatedAnnealingSampler()
    sampleset = sa.sample(bqm, seed=888, beta_range=[10e-12, 60], beta_schedule_type='geometric', num_reads=50)

    # 对数据进行筛选
    decoded_samples = model.decode_sampleset(sampleset)  # 将上述采样最好的num_reads组数据变为模型可读的样本数据
    best_sample = min(decoded_samples, key=lambda x: x.energy)  # 将能量值最低的样本统计出来,表示BQM的最优解

    # print(f"验证约束条件M:{best_sample.constraints()}")
    end = time.time()
    print(f"退火时间:{
      
      end - start} s")

    # print(best_sample.sample)

    # 统计决策变量为1的所有数据
    data_1_list = get_key(best_sample.sample, 1)

    print(f"对应的取1的决策变量有:{
      
      data_1_list}(表示[第一张卡的阈值][第二张卡的阈值][第三张卡的阈值],注意是索引,从0开始),对应的能量为{
      
      -best_sample.energy}")


if __name__ == '__main__':
    main()

enumeration violence

The corresponding enumeration violence code is as follows:

import pandas as pd

if __name__ == '__main__':
    # 定义一些变量
    L = 1000000  # 贷款资金
    I = 0.08  # 利息收入率

    # 选取第几列的信用卡
    card1 = 1
    card2 = 2
    card3 = 3

    df = pd.read_csv('../data/附件1:data_100.csv', header=0)
    # 定义最大的数
    max_value = -1

    # 定义最大数字的索引下标
    max_i = max_j = max_k = -1

    for i in range(0, 10):
        for j in range(0, 10):
            for k in range(0, 10):
                # 总通过率
                P = df[f"t_{
      
      card1}"].iloc[i] * df[f"t_{
      
      card2}"].iloc[j] * df[f"t_{
      
      card3}"].iloc[k]

                # 总坏账率
                Q = (df[f"h_{
      
      card1}"].iloc[i] + df[f"h_{
      
      card2}"].iloc[j] + df[f"h_{
      
      card3}"].iloc[k]) / 3

                # 此时的最终收入
                temp_value = L * I * P * (1 - Q) - L * P * Q

                # print(f"选卡1阈值:[{i + 1}],选卡1阈值:[{j + 1}],选卡1阈值:[{k + 1}],对应最终收入{temp_value},之前的最大收入:{max_value}")

                if temp_value > max_value:
                    max_value = temp_value
                    max_i = i
                    max_j = j
                    max_k = k

        print(f"第{
      
      i + 1}个对应的最大值:{
      
      max_value},三个阈值选取为:{
      
      max_i + 1},{
      
      max_j + 1},{
      
      max_k + 1},其中,阈值编号[1-10]")

    print(f"最大值:{
      
      max_value},三个阈值选取为:{
      
      max_i + 1},{
      
      max_j + 1},{
      
      max_k + 1},其中,阈值编号[1-10]")

Greedy + QUBO

During the running process, I found a rule that is very helpful to the third question: You can print out the above decision-making process (for loop) to see:

For the first cycle, its decision process is: [1,1,2]-->[2,1,2]-->[3,1,2]-->[3,1,2]-->[3,1,2]-->[6,1,2]-->[7,1,2]-->[8,1,2]-->[8,1,2]-->[8,1,2]. The results printed here are relatively small, and the rules may not be obvious. But easy to spot:

For the decision-making process of three scoring credit cards, if the threshold selection of the first and second scoring credit cards is fixed, and the threshold selection of the third scoring credit card is considered, the optimal credit scoring card threshold is selected at this time. Then fix the threshold selection of the first and third scoring credit cards, and consider the threshold selection of the second scoring credit card, and select the optimal credit scoring card threshold at this time. Then fix the threshold selection of the second and third scoring credit cards, and consider the threshold selection of the first scoring credit card, and select the optimal credit scoring card threshold at this time. Based on this non-stop iteration, the final threshold selection is the optimal threshold combination. (Consider to prove it).

insert image description here

Based on this process, we can convert the original 10 × 10 × 10 10 \times 10 \times 1010×10×The solution of 10 decision variables is converted to iteration times × 10 iteration times\times 10iterations×Questions for 10 decision variables. This is a very important finding for question three. For question three, it was originally1000 × 990 × 980 1000 \times 990 \times 9801000×990×The 980 decision variable problem is converted toiterations × 980 iterations\times 980iterations×980 decision variable scale questions.

question three

At the beginning we considered: design decision variables xi , j x_{i,j}xi,jIt is used to represent:
xi , j = { 0 , do not choose the j-th threshold of the i-th credit scorecard 1 , choose the j-th threshold of the i-th credit scorecard x_{i,j}=\left\{ \ begin{array}{c} 0 \quad , do not select the \j\th threshold of the \i\th credit scorecard \\ 1 \quad , select the \j\th threshold of the \i\th credit scorecard \\ \end{array} \right.xi,j={ 0,Unselect the jth threshold of the ith credit scorecard    1,Select the j -th     threshold of the i -th credit scorecard
Without considering the constraints, the overall pass rate PPP and damage rateQQQ满足:
P = ∏ ( i , j ) ∈ X ( x i , j ∗ T i , j + 1 − x i , j ) = ∏ ( i , j ) ∈ X ( T i , j x i , j ) Q = 1 3 ∗ ∑ ( i , j ) ∈ X ( H i , j ∗ x i , j ) P =\prod_{(i,j) \in \mathcal{X}}(x_{i,j}*T_{i,j} +1-x_{i,j}) = \prod_{(i,j) \in \mathcal{X}}(T_{i,j}^{x_{i,j}}) \\ Q = \frac{1}{3} * \sum_{(i,j) \in \mathcal{X} }( H_{i,j} * x_{i,j}) P=(i,j)X(xi,jTi,j+1xi,j)=(i,j)X(Ti,jxi,j)Q=31(i,j)X(Hi,jxi,j)
corresponding to the final incomeHHH有:
H = L   ∗   I   ∗   P   ∗   ( 1 − Q )   −   L   ∗   P   ∗   Q H = L \ * \ I \ * \ P \ * \ (1-Q) \ - \ L \ * \ P \ * \ Q H=L  I  P  (1Q)  L  P  Q
can be found that if useP = ∏ ( i , j ) ∈ X ( xi , j ∗ T i , j + 1 − xi , j ) P =\prod_{(i,j) \in \mathcal{X}} (x_{i,j}*T_{i,j} +1-x_{i,j})P=(i,j)X(xi,jTi,j+1xi,j) of this formula, in its objective function, the coefficient of the highest sub-term of the decision variable is1001 10011001 times. Converted toHOBO HOBOThe H OBO model needs to be downgraded, which feels very complicated. Not consider.

如果使用 P = ∏ ( i , j ) ∈ X ( T i , j x i , j ) P = \prod_{(i,j) \in \mathcal{X}}(T_{i,j}^{x_{i,j}}) P=(i,j)X(Ti,jxi,j) formula, and then consider the idea of ​​using polynomial approximation (Polynomial approximation), expand the exponential term using polynomials, such as Taylor series expansion:eax = ∑ n = 0 ∞ ( ax ) nn ! e^{ax} = \sum_ {n=0}^{\infty} \frac{(ax)^n}{n!}eax=n=0n!(ax)n, keeping the first-order term, namely: eax ≈ 1 + axe^{ax} \approx 1+axeax1+ax . _ In other words, the exponential term can be transformed into a linear term of a polynomial function:
P = ∏ ( i , j ) ∈ X ( T i , jxi , j ) = exp { ∑ ( i , j ) ∈ X [ xi , j ∗ ln ( T i , j ) ] } ≈ 1 + ∑ ( i , j ) ∈ X [ xi , j ∗ ln ( T i , j ) ] P = \prod_{(i,j) \in \mathcal{X}} (T_{i,j}^{x_{i,j}}) = exp \{ \sum_{(i,j) \in \mathcal{X}} [x_{i,j}* ln(T_{i ,j})] \} \approx 1 + \sum_{(i,j) \in \mathcal{X}} [x_{i,j}* ln(T_{i,j})]P=(i,j)X(Ti,jxi,j)=exp{ (i,j)X[xi,jln(Ti,j)]}1+(i,j)X[xi,jln(Ti,j)]
Then based on this theory, the original objective function can be transformed into the objective function of the highest quadratic term. But this is also a problem, because the decision variablexi , j ∈ { 0 , 1 } x_{i,j} \in \{ 0,1 \}xi,j{ 0,1 } is a discrete variable, then using the Taylor-level expansion may not be equivalent, and it will not be considered.

There is a problem in all the above considerations, that is, how to solve the overall pass rate PPP expression. If the PPcan beP is converted into an expression of a primary item, then it is very simple to solve the entire problem. Then redefine the decision variablesxi , j , k , l , m , n x_{i,j,k,l,m,n}xi,j,k,l,m,nUsed to decide whether to choose: Section iii credit score card selectionlll threshold value,jjthSelect themmth of j credit score cardsmth threshold,kkthk credit score card selectionnnn thresholds. That is:
xi , j , k , l , m , n = { 0 , do not choose 1 , choose x_{i,j,k,l,m,n}=\left\{ \begin{array}{c} 0 \quad, do not choose \\ 1 \quad, choose \end{array} \right.xi,j,k,l,m,n={ 0,do not choose1,choose
Then for decision variables xi , j , k , l , m , n x_{i,j,k,l,m,n}xi,j,k,l,m,nCorresponding pass rate P i , j , k , l , m , n P_{i,j,k,l,m,n}Pi,j,k,l,m,nand damage rate Q i , j , k , l , m , n Q_{i,j,k,l,m,n}Qi,j,k,l,m,n 有:
P i , j , k , l , m , n = x i , j , k , l , m , n   ∗   T i , l   ∗   T j , m   ∗   T k , n Q i , j , k , l , m , n = 1 3 ∗ x i , j , k , l , m , n   ∗   ( H i , l + H j , m + H k , n ) P_{i,j,k,l,m,n} = x_{i,j,k,l,m,n} \ * \ T_{i,l} \ * \ T_{j,m} \ * \ T_{k,n} \\ Q_{i,j,k,l,m,n} = \frac{1}{3} * x_{i,j,k,l,m,n} \ * \ (H_{i,l} + H_{j,m}+H_{k,n}) Pi,j,k,l,m,n=xi,j,k,l,m,n  Ti,l  Tj,m  Tk,nQi,j,k,l,m,n=31xi,j,k,l,m,n  (Hi,l+Hj,m+Hk,n)
into the final income formula:
H = ∑ ( i , j , k , l , m , n ) ∈ XH i , j , k , l , m , n = ∑ i = 1 100 ∑ j = 1 100 ∑ k = 1 100 ∑ l = 1 10 ∑ m = 1 10 ∑ n = 1 10 [ L ∗ I ∗ P i , j , k , l , m , n ∗ ( 1 − Q i , j , k , l , m , n ) − L ∗ P i , j , k , l , m , n ∗ Q i , j , k , l , m , n ] H = \sum_{ {(i, j,k,l, m , n) \in \mathcal{X}}} H_{i,j,k,l,m,n} \\ = \sum_{i=1}^{100} \sum_{j=1}^{100} \sum_{k=1}^{100} \sum_{l=1}^{10} \sum_{m=1}^{10} \sum_{n=1}^{10} [{L \ * \ I \ * \ P_{i,j,k,l,m,n} \ * \ (1-Q_{i,j,k,l,m,n}) \ - \ L \ * \ P_{i, j,k,l,m,n} \ * \ Q_{i,j,k,l,m,n} }] \\H=(i,j,k,l,m,n)XHi,j,k,l,m,n=i=1100j=1100k=1100l=110m=110n=110[L  I  Pi,j,k,l,m,n  (1Qi,j,k,l,m,n)  L  Pi,j,k,l,m,n  Qi,j,k,l,m,n]
If this method does not consider the constraints, then the quantum number is 100 × 100 × 100 × 10 × 10 × 10 100 \times 100 \times 100 \times 10 \times 10 \times 10100×100×100×10×10×10 , the memory is not enough, it can't run this.

Consider violence

Consider using a loop to brute force it to find the best combination. This takes a long time to carry out, so consider the idea of ​​​​multi-threading + divide and conquer against it. The code is as follows:

import time
import pandas as pd
from threading import Thread

# 定义一些变量
L = 1000000  # 贷款资金
I = 0.08  # 利息收入率

df = pd.read_csv('../data/附件1:data_100.csv', header=0)

# 全局变量,用于存储所有线程中的最大值
max_value = -1
# 定义最大数字的索引下标
max_i = max_j = max_k = max_m = max_n = max_l = -1


def computer_result(begin, end):
    # 使用全局变量
    global max_value, max_i, max_j, max_k, max_m, max_n, max_l

    for i in range(begin, end):
        for j in range(i + 1, 101):
            for k in range(j + 1, 101):
                for m in range(0, 10):
                    for n in range(0, 10):
                        for l in range(0, 10):
                            # 总通过率
                            P = df[f"t_{
      
      i}"].iloc[m] * df[f"t_{
      
      j}"].iloc[n] * df[f"t_{
      
      k}"].iloc[l]

                            # 总坏账率
                            Q = (df[f"h_{
      
      i}"].iloc[m] + df[f"h_{
      
      j}"].iloc[n] + df[f"h_{
      
      k}"].iloc[l]) / 3

                            # 此时的最终收入
                            temp_value = L * I * P * (1 - Q) - L * P * Q

                            if temp_value > max_value:
                                max_value, max_i, max_j, max_k, max_m, max_n, max_l = temp_value, i, j, k, m, n, l


if __name__ == '__main__':

    start = time.time()
    print(start)

    # 创建线程并启动它们
    threads = [Thread(target=computer_result, args=(i, i + 1)) for i in range(1, 101)]
    for thread in threads:
        thread.start()

    # 等待所有线程执行完毕
    for thread in threads:
        thread.join()

    end = time.time()
    print(f"时间花费:{
      
      end - start} s")
    print(f"最大值:{
      
      max_value},对应的选取卡1:第{
      
      max_i}{
      
      max_m + 1}个阈值,"
          f"对应的选取卡2:第{
      
      max_j}{
      
      max_n + 1}个阈值,"
          f"对应的选取卡3:第{
      
      max_k}{
      
      max_l + 1}个阈值,其中,卡取值[1-100],阈值取值[1-10]")

I feel that the code can still be optimized, but I don't want to spend time on it.

Consider violence + greed

It is mainly the greedy law found in question 2, so this efficiency is relatively high. Then similarly use the following iterative algorithm:

insert image description here

Then the main problem is how to find the optimal credit score card threshold after fixing the thresholds of the two scoring credit cards? Either brute force, or use QUBOthe model of problem one.

The violent solution is as follows:

import pandas as pd

if __name__ == '__main__':
    # 定义一些变量
    L = 1000000  # 贷款资金
    I = 0.08  # 利息收入率

    # 迭代次数
    Iter = 10

    # 选取第几列的信用卡,初始化最开始选择的数据
    card1, threshold1, card2, threshold2, card3, threshold3 = 1, 0, 2, 0, 3, 0

    # 定义最大的数
    max_value = -1
    # 读取数据
    df = pd.read_csv('../data/附件1:data_100.csv', header=0)

    for iter in range(Iter):  # 迭代次数,其实这里还可以根据几次迭代算出的总值来提前结束迭代
        # 设置更新迭代,不使用临时变量
        card1, threshold1, card2, threshold2, card3, threshold3 = card2, threshold2, card3, threshold3, card1, threshold1

        # 从固定的信用卡1和信用卡2外数据中选一个,构建可以选择的卡的集合
        choose_list = {
    
    i for i in range(1, 101) if i not in (card1, card2)}

        # 获取第一张卡和第二张卡的通过率和坏账率,否则在循环里重复获取了
        t1 = df[f"t_{
      
      card1}"].iloc[threshold1]
        t2 = df[f"t_{
      
      card2}"].iloc[threshold2]
        h1 = df[f"h_{
      
      card1}"].iloc[threshold1]
        h2 = df[f"h_{
      
      card2}"].iloc[threshold2]

        # 固定第一张卡,和第二张卡,然后找到最佳的第三张卡
        for c3 in choose_list:
            for th3 in range(0, 10):
                # 总通过率
                P = t1 * t2 * df[f"t_{
      
      c3}"].iloc[th3]

                # 总坏账率
                Q = (h1 + h2 + df[f"h_{
      
      c3}"].iloc[th3]) / 3

                # 此时的最终收入
                temp_value = L * I * P * (1 - Q) - L * P * Q

                if temp_value > max_value:
                    max_value = temp_value
                    card3 = c3
                    threshold3 = th3

    print(f"最大值:{
      
      max_value},卡选择:{
      
      card1}_{
      
      threshold1 + 1},{
      
      card2}_{
      
      threshold2 + 1},{
      
      card3}_{
      
      threshold3 + 1}")

Greedy + QUBO

code:

import pandas as pd
import time
import re
from pyqubo import Array, Constraint
from dwave.samplers import SimulatedAnnealingSampler
import numpy as np
from pyqubo import Placeholder

# 在字典中找到value的key
def get_key(dict, value):
    return [k for k, v in dict.items() if v == value]


def main():
    # 定义一些变量
    L = 1000000  # 贷款资金
    I = 0.08  # 利息收入率

    # 迭代次数
    Iter = 10

    # 选取第几列的信用卡,初始化最开始选择的数据
    card1, threshold1, card2, threshold2, card3, threshold3 = 1, 0, 2, 0, 3, 0

    # 定义最大的数
    max_value = -1
    # 读取数据
    df = pd.read_csv('../data/附件1:data_100.csv', header=0)

    for iter in range(Iter):  # 迭代次数,其实这里还可以根据几次迭代算出的总值来提前结束迭代
        # 设置更新迭代,不使用临时变量
        card1, threshold1, card2, threshold2, card3, threshold3 = card2, threshold2, card3, threshold3, card1, threshold1

        # 获取第一张卡和第二张卡的通过率和坏账率
        t1 = df[f"t_{
      
      card1}"].iloc[threshold1]
        t2 = df[f"t_{
      
      card2}"].iloc[threshold2]
        h1 = df[f"h_{
      
      card1}"].iloc[threshold1]
        h2 = df[f"h_{
      
      card2}"].iloc[threshold2]

        # 获取第三张卡能够选取的通过率与坏账率的矩阵

        choose_index = [i for i in range(1, 101) if i not in (card1, card2)]
        card3_data = df.drop([f"t_{
      
      card1}", f"h_{
      
      card1}", f"t_{
      
      card2}", f"h_{
      
      card2}"], axis=1).values

        # 获取T矩阵和H矩阵
        T = card3_data[:, ::2]
        H = card3_data[:, 1::2]

        # 定义二进制决策变量,10 * 98
        x = Array.create('x', shape=(10, 98), vartype='BINARY')
        # 定义惩罚项M
        M = Placeholder('M')
        M = 50000

        # 定义哈密顿量,也就是对应的目标函数
        P = t1 * t2 * np.sum(np.multiply(x, T))
        Q = (h1 + h2 + np.sum(np.multiply(x, H))) / 3
        H = - (L * I * P * (1 - Q) - L * P * Q) + M * Constraint((np.sum(x) - 1) ** 2, label='sum(x_i_j) = 1')

        # 编译哈密顿量得到一个模型
        model = H.compile()
        bqm = model.to_bqm()
        # 记录开始退火时间
        start = time.time()

        #
        sa = SimulatedAnnealingSampler()

        sampleset = sa.sample(bqm, seed=666, beta_range=[10e-10, 50], num_sweeps=999, beta_schedule_type='geometric',
                              num_reads=50)

        # 对数据进行筛选,对选取的数据进行选取最优的
        decoded_samples = model.decode_sampleset(sampleset)  # 将上述采样最好的num_reads组数据变为模型可读的样本数据
        best_sample = min(decoded_samples, key=lambda x: x.energy)  # 将能量值最低的样本统计出来,表示BQM的最优解

        end = time.time()
        # print(f"退火时间花费:{end - start} s")

        # 统计决策变量为1的所有数据,并对第一个数据进行拆解,获得对应的下标
        data_1_list = get_key(best_sample.sample, 1)
        index_i_j = re.findall(r'\d+', data_1_list[0])
        index_i = int(index_i_j[0])

        if max_value < - best_sample.energy:
            card3 = choose_index[int(index_i_j[1])]
            threshold3 = index_i
            max_value = - best_sample.energy

        print(
            f"第{
      
      iter + 1}次迭代,最大值:{
      
      max_value},卡选择:{
      
      card1}_{
      
      threshold1 + 1},{
      
      card2}_{
      
      threshold2 + 1},{
      
      card3}_{
      
      threshold3 + 1}")

    print(f"最大值:{
      
      max_value},卡选择:{
      
      card1}_{
      
      threshold1 + 1},{
      
      card2}_{
      
      threshold2 + 1},{
      
      card3}_{
      
      threshold3 + 1}")


if __name__ == '__main__':
    start = time.time()
    main()
    end = time.time()
    print(f"时间花费: {
      
      end - start}s")

Guess you like

Origin blog.csdn.net/weixin_41012765/article/details/130249552