Welcome, guest | Sign In | My Account | Store | Cart

This is a simple decision maker. It chooses the best option between N items. Each item can have M constraint values (non-zero). It is assumed each constraint has equal importance (weight).

Python, 62 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# decisionmaker.py
# FB - 201010155
# Choose the best item from N options.
# Each item can have M constraints (non-zero).
# All constraints assumed to have equal importance (weight).
import sys

def makeDecision(constraintsTable, constraintTypes):
    # calculate item values
    itemValues = []
    for i in range(n):
        itemValues.append(float(1))
        for j in range(m):
            if constraintTypes[j] == 0: # min value is better
                itemValues[i] /= constraintsTable[i][j]
            else: # max value is better
                itemValues[i] *= constraintsTable[i][j]

    # choose the best item
    maxIndex = 0
    maxValue = itemValues[0]
    for i in range(n):
        if itemValues[i] > maxValue:
            maxValue = itemValues[i]
            maxIndex = i

    return itemNames[maxIndex]

# MAIN
n = int(raw_input('Number of items: '))
if n < 2:
    sys.exit()
m = int(raw_input('Number of constraints for each item: '))
if m < 2:
    sys.exit()

constraintNames = []
constraintTypes = [] # min or max is better
for j in range(m):
    constraintName = raw_input('Constraint ' + str(j + 1) + ' name: ')
    constraintNames.append(constraintName)
    constraintType = int(raw_input('Lower(0) or Higher(1) is better: '))
    if constraintType < 0 or constraintType > 1:
        sys.exit()
    constraintTypes.append(constraintType)

itemNames = []
constraintsTable = []
for i in range(n):
    itemName = raw_input('Item ' + str(i + 1) + ' name: ')
    itemNames.append(itemName)

    constraints = []
    for j in range(m):
        constraint = float(raw_input(constraintNames[j] + ': '))
        if constraint == 0.0:
            print 'Constraint value cannot be 0!'
            sys.exit()
        constraints.append(constraint)
    constraintsTable.append(constraints)

print 'Best item decided: ' + makeDecision(constraintsTable, constraintTypes)

5 comments

FB36 (author) 13 years, 6 months ago  # | flag

This is a sample run:

Number of items: 2

Number of constraints for each item: 3

Constraint 1 name: price

Lower(0) or Higher(1) is better: 0

Constraint 2 name: speed

Lower(0) or Higher(1) is better: 1

Constraint 3 name: ram

Lower(0) or Higher(1) is better: 1

Item 1 name: laptop1

price: 500

speed: 1.5

ram: 4

Item 2 name: laptop2

price: 600

speed: 2

ram: 8

The best item decided: laptop2

FB36 (author) 13 years, 6 months ago  # | flag

It is also assumed that each constraint is a linear quantity.

Stephen Chappell 13 years, 6 months ago  # | flag

You might want to organize your code into functions if you want others to appreciate your work more.

FB36 (author) 13 years, 6 months ago  # | flag

Converted the decision making part to a function.

Also here is another test run w/ more items and constraints:

Number of items: 3

Number of constraints for each item: 4

Constraint 1 name: price

Lower(0) or Higher(1) is better: 0

Constraint 2 name: speed

Lower(0) or Higher(1) is better: 1

Constraint 3 name: ram

Lower(0) or Higher(1) is better: 1

Constraint 4 name: disk

Lower(0) or Higher(1) is better: 1

Item 1 name: laptop1

price: 500

speed: 1.5

ram: 4

disk: 320

Item 2 name: laptop2

price: 600

speed: 2

ram: 8

disk: 500

Item 3 name: laptop3

price: 700

speed: 2

ram: 6

disk: 750

Best item decided: laptop2

FB36 (author) 13 years, 5 months ago  # | flag

Actually I think there is a simple way to allow weights for each item: Just add another constraint called 'weight' (w/ 'higher is better' selected).

For example, that could be used to implement brand name importance in decisions. Like, if a laptop has a better-known brand name then it would have weight=2 otherwise weight=1 etc.

Created by FB36 on Fri, 15 Oct 2010 (MIT)
Python recipes (4591)
FB36's recipes (148)

Required Modules

  • (none specified)

Other Information and Tasks