# Python dynamic programming algorithm for virtual machine deployment

Time：2021-10-18

## statement

This article is a personal humble opinion. It only provides reference, not necessarily correct. All leaders can express their own opinions.

## Title Description

Considering that in virtual machine deployment, resource providers usually want to maximize their own revenue. Now there is a host with X CPUs and Y GB of memory. Users can apply to the resource provider for virtual machine resources by making their own quotation, for example, paying w yuan for a virtual machine with a CPU and B GB of memory. Please design an algorithm so that resource providers can reasonably arrange virtual machines to maximize their benefits.
Input:
n x y
2 4 200
4 2 150

Note: n indicates that there are n user quotation applications, and the host computer has x CPUs and Y GB of memory;
The following N lines represent the number of CPU and memory requested by the user and the amount quoted by the user.

## Algorithmic thought

In order to find the global optimal solution, the idea of dynamic programming is adopted. Finding the best interest is the final problem. The sub problem of the best interest can be regarded as the maximum amount of each user that has been quoted, and the number of CPUs and memory required by it can be added to the total demand, which can be compared with the number of CPUs and memory capacity provided. After solving the problem of users with the largest quotation at present, the next largest quotation can be regarded as a sub problem, but the allocated CPU and memory capacity need to be subtracted. Repeat this process until the CPU and memory capacity can not meet the requirements of any user, and the optimal solution will be obtained.

## test result

Operation results:

source code

``````import sys
Print ("please enter the number of users applying for virtual machine, number of CPUs, memory capacity:")
A = list (map (int, input(). Split())) # uses array a to store the number of users participating in the quotation, the number of CPUs to be stored in the cloud and the capacity
A1 = a [0] # stores the number of users. How many lines of data should be entered
A2 = a [1] # number of CPUs stored
A3 = a [2] # storage capacity
b = []
cpu_num=0
size_num=0
money=0

B1 = [0] * A1 # array B1 stores user Quotes
P1 = [0] * A1 # array P1 record the position of quotation amount

for i in range(a1):
Print ("please enter the number of CPUs and memory capacity quotation of user I + 1:")
b.append(list(map(int, input().split())))

for k in range(a1):
b1[k] = b[k][2]
p1[k] = k

for i in range(0,a1-1):
for j in range(1,a1-i):
if b1[j]>b1[j-1]:
temp=b1[j-1]
b1[j-1]=b1[j]
b1[j]=temp
temp=p1[j-1]
p1[j-1]=p1[j]
p1[j]=temp
def Fun(i):
global cpu_num,size_num,money
cpu_num=cpu_num+b[p1[i]][0]
size_num=size_num+b[p1[i]][1]
money=money+b[p1[i]][2]

if cpu_num>a2 or size_num>a3:
money=money-b[p1[i]][2]
cpu_num=cpu_num-b[p1[i]][0]
size_num=size_num-b[p1[i]][1]

for i in range(a1):
Fun(i)
Print ("maximize revenue:", money)``````

This is the end of this article on Python dynamic planning for virtual machine deployment. For more information about Python virtual machine deployment, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!

## [vulfocus range reproduction] Nostromo remote command execution (cve-2019-16278)

Vulnerability introduction Nostromo nhttpd is an open source web server. Nostromo has defects in verifying URL security, resulting in directory traversal. Anyone can traverse any file in the system. Therefore, a remote unauthenticated attacker can force the server to point to a shell file such as / bin / sh, thereby executing arbitrary commands. Impact […]