Task: Knapsack problem_Unbounded








VB: #define min(a, b) iif((a) < (b), (a), (b))

Dim As Single totalPeso, totalVolumen
Dim As Integer maxPanacea, maxIchor, maxGold, maxValor
Dim As Integer i, j ,k
Dim As Integer n(2)

Type Bounty
    articulo As String*7
    valor  As Integer
    peso As Single
    volumen As Single
End Type

Dim item(1 To 5) As Bounty => { _
("panacea", 3000, 0.3, 0.025), ("ichor", 1800,  0.2, 0.015), _
("gold",    2500, 2.0, 0.002), ("sack",     0, 25.0, 0.25 )}

maxPanacea = min(item(4).peso/item(1).peso, item(4).volumen/item(1).volumen)
maxIchor   = min(item(4).peso/item(2).peso, item(4).volumen/item(2).volumen)
maxGold    = min(item(4).peso/item(3).peso, item(4).volumen/item(3).volumen)

For i = 0 To maxPanacea
    For j = 0 To maxIchor
        For k = 0 To maxGold
            item(0).valor =  k*item(3).valor  + j*item(2).valor  + i*item(1).valor
            item(0).peso = k*item(3).peso + j*item(2).peso + i*item(1).peso
            item(0).volumen = k*item(3).volumen + j*item(2).volumen + i*item(1).volumen
            If item(0).peso > item(4).peso Or item(0).volumen > item(4).volumen Then
                Continue For
            End If
            If item(0).valor > maxValor Then
                maxValor = item(0).valor
                totalPeso = item(0).peso
                totalVolumen = item(0).volumen
                n(0) = i: n(1) = j: n(2) = k
            End If
        Next k
    Next j
Next i

Print "Maximum valor achievable is "; Str(maxValor)
Print "This is achieved by carrying "; Str(n(0));
Print " panacea, "; Str(n(1)); " ichor and "; Str(n(2)); " gold items."
Print "The peso to carry is "; Str(totalPeso);
Print " and the volume used is "; Str(totalVolumen)

Sleep



Fortran: PROGRAM KNAPSACK

  IMPLICIT NONE
 
  REAL :: totalWeight, totalVolume
  INTEGER :: maxPanacea, maxIchor, maxGold, maxValue = 0
  INTEGER :: i, j, k
  INTEGER :: n(3)  

  TYPE Bounty
    INTEGER :: value
    REAL :: weight
    REAL :: volume
  END TYPE Bounty

  TYPE(Bounty) :: panacea, ichor, gold, sack, current

  panacea = Bounty(3000, 0.3, 0.025)
  ichor   = Bounty(1800, 0.2, 0.015)
  gold    = Bounty(2500, 2.0, 0.002)
  sack    = Bounty(0, 25.0, 0.25)

  maxPanacea = MIN(sack%weight / panacea%weight, sack%volume / panacea%volume)
  maxIchor = MIN(sack%weight / ichor%weight, sack%volume / ichor%volume)
  maxGold = MIN(sack%weight / gold%weight, sack%volume / gold%volume)
  
  DO i = 0, maxPanacea
     DO j = 0, maxIchor
        Do k = 0, maxGold
           current%value = k * gold%value + j * ichor%value + i * panacea%value
           current%weight = k * gold%weight + j * ichor%weight + i * panacea%weight
           current%volume = k * gold%volume + j * ichor%volume + i * panacea%volume       
           IF (current%weight > sack%weight .OR. current%volume > sack%volume) CYCLE
           IF (current%value > maxValue) THEN
              maxValue = current%value
              totalWeight = current%weight
              totalVolume = current%volume
              n(1) = i ; n(2) = j ; n(3) = k
           END IF
        END DO  
     END DO
  END DO

  WRITE(*, "(A,I0)") "Maximum value achievable is ", maxValue
  WRITE(*, "(3(A,I0),A)") "This is achieved by carrying ", n(1), " panacea, ", n(2), " ichor and ", n(3), " gold items"
  WRITE(*, "(A,F4.1,A,F5.3)") "The weight to carry is ", totalWeight, " and the volume used is ", totalVolume
 
END PROGRAM KNAPSACK