Skip to main content
added 216 characters in body
Source Link
george2079
  • 39.1k
  • 1
  • 44
  • 111
a = {{5, 3}, {1, 2}}
b = {{1, 1}, {1, 0}}
c = {{1, 1}, {1, 1}}

some functions to check the criteria:

c1[result_] := 
 And @@ Table[ 
   c[[i, 1]] >= Total@result[[All, i]] >= c[[i, 2]]  , {i, 
    Length@result[[1]]}]
c2[result_] := 
 And @@ Table[ 
   b[[i, 1]] >= Total@result[[i]] >= b[[i, 2]]  , {i, Length@result}]

and calculate the score:

score[result_] := Total@MapThread[ Dot, { a, result }]

check:

result={{1,0},{0,1}}
c1[result] (*True*)
c2[result] (*True*)
score[result, a] (* 7 *)

now for this small problem NMaximise gets the solution.. ( same approach as here https://mathematica.stackexchange.com/a/88226/2079 with different constraints )

create an result array:

result = Array[ v , {2, 2}]
unknowns = Flatten@result

NMaximize[{score[result], 
  c1[result] && c2[result] && 
   And @@ (0 <= # <= 1 & /@ Flatten[result])} , {v1, v2, v3, 
 unknowns v4}, Integers ]

{7., {v1v[1,1] -> 1, v2v[1,2] -> 0, v3v[2,1] -> 0, v4v[2,2] -> 1}}

result /. %[[2]]

{{1, 0}, {0, 1}}

a = {{5, 3}, {1, 2}}
b = {{1, 1}, {1, 0}}
c = {{1, 1}, {1, 1}}

some functions to check the criteria:

c1[result_] := 
 And @@ Table[ 
   c[[i, 1]] >= Total@result[[All, i]] >= c[[i, 2]]  , {i, 
    Length@result[[1]]}]
c2[result_] := 
 And @@ Table[ 
   b[[i, 1]] >= Total@result[[i]] >= b[[i, 2]]  , {i, Length@result}]

and calculate the score:

score[result_] := Total@MapThread[ Dot, { a, result }]

now for this small problem NMaximise gets the solution.. ( same approach as here https://mathematica.stackexchange.com/a/88226/2079 with different constraints )

NMaximize[{score[result], 
  c1[result] && c2[result] && 
   And @@ (0 <= # <= 1 & /@ Flatten[result])} , {v1, v2, v3, 
  v4}, Integers ]

{7., {v1 -> 1, v2 -> 0, v3 -> 0, v4 -> 1}}

result /. %[[2]]

{{1, 0}, {0, 1}}

a = {{5, 3}, {1, 2}}
b = {{1, 1}, {1, 0}}
c = {{1, 1}, {1, 1}}

some functions to check the criteria:

c1[result_] := 
 And @@ Table[ 
   c[[i, 1]] >= Total@result[[All, i]] >= c[[i, 2]]  , {i, 
    Length@result[[1]]}]
c2[result_] := 
 And @@ Table[ 
   b[[i, 1]] >= Total@result[[i]] >= b[[i, 2]]  , {i, Length@result}]

and calculate the score:

score[result_] := Total@MapThread[ Dot, { a, result }]

check:

result={{1,0},{0,1}}
c1[result] (*True*)
c2[result] (*True*)
score[result, a] (* 7 *)

now for this small problem NMaximise gets the solution.. ( same approach as here https://mathematica.stackexchange.com/a/88226/2079 with different constraints )

create an result array:

result = Array[ v , {2, 2}]
unknowns = Flatten@result

NMaximize[{score[result], 
  c1[result] && c2[result] && 
   And @@ (0 <= # <= 1 & /@ Flatten[result])} , unknowns , Integers ]

{7., {v[1,1] -> 1, v[1,2] -> 0, v[2,1] -> 0, v[2,2] -> 1}}

result /. %[[2]]

{{1, 0}, {0, 1}}

added 105 characters in body
Source Link
george2079
  • 39.1k
  • 1
  • 44
  • 111
a = {{5, 3}, {1, 2}}
b = {{1, 1}, {1, 0}}
c = {{1, 1}, {1, 1}}

some functions to check the criteria:

c1[result_] := 
 And @@ Table[ 
   c[[i, 1]] >= Total@result[[All, i]] >= c[[i, 2]]  , {i, 
    Length@result[[1]]}]
c2[result_] := 
 And @@ Table[ 
   b[[i, 1]] >= Total@result[[i]] >= b[[i, 2]]  , {i, Length@result}]

and calculate the score:

score[result_] := Total@MapThread[ Dot, { a, result }]

now for this small problem NMaximise gets the solution.. ( same approach as here https://mathematica.stackexchange.com/a/88226/2079 with different constraints )

NMaximize[{score[result], 
  c1[result] && c2[result] && 
   And @@ (0 <= # <= 1 & /@ Flatten[result])} , {v1, v2, v3, 
  v4}, Integers ]

{7., {v1 -> 1, v2 -> 0, v3 -> 0, v4 -> 1}}

result /. %[[2]]

{{1, 0}, {0, 1}}

a = {{5, 3}, {1, 2}}
b = {{1, 1}, {1, 0}}
c = {{1, 1}, {1, 1}}

some functions to check the criteria:

c1[result_] := 
 And @@ Table[ 
   c[[i, 1]] >= Total@result[[All, i]] >= c[[i, 2]]  , {i, 
    Length@result[[1]]}]
c2[result_] := 
 And @@ Table[ 
   b[[i, 1]] >= Total@result[[i]] >= b[[i, 2]]  , {i, Length@result}]

and calculate the score:

score[result_] := Total@MapThread[ Dot, { a, result }]

now for this small problem NMaximise gets the solution..

NMaximize[{score[result], 
  c1[result] && c2[result] && 
   And @@ (0 <= # <= 1 & /@ Flatten[result])} , {v1, v2, v3, 
  v4}, Integers ]

{7., {v1 -> 1, v2 -> 0, v3 -> 0, v4 -> 1}}

result /. %[[2]]

{{1, 0}, {0, 1}}

a = {{5, 3}, {1, 2}}
b = {{1, 1}, {1, 0}}
c = {{1, 1}, {1, 1}}

some functions to check the criteria:

c1[result_] := 
 And @@ Table[ 
   c[[i, 1]] >= Total@result[[All, i]] >= c[[i, 2]]  , {i, 
    Length@result[[1]]}]
c2[result_] := 
 And @@ Table[ 
   b[[i, 1]] >= Total@result[[i]] >= b[[i, 2]]  , {i, Length@result}]

and calculate the score:

score[result_] := Total@MapThread[ Dot, { a, result }]

now for this small problem NMaximise gets the solution.. ( same approach as here https://mathematica.stackexchange.com/a/88226/2079 with different constraints )

NMaximize[{score[result], 
  c1[result] && c2[result] && 
   And @@ (0 <= # <= 1 & /@ Flatten[result])} , {v1, v2, v3, 
  v4}, Integers ]

{7., {v1 -> 1, v2 -> 0, v3 -> 0, v4 -> 1}}

result /. %[[2]]

{{1, 0}, {0, 1}}

Source Link
george2079
  • 39.1k
  • 1
  • 44
  • 111

a = {{5, 3}, {1, 2}}
b = {{1, 1}, {1, 0}}
c = {{1, 1}, {1, 1}}

some functions to check the criteria:

c1[result_] := 
 And @@ Table[ 
   c[[i, 1]] >= Total@result[[All, i]] >= c[[i, 2]]  , {i, 
    Length@result[[1]]}]
c2[result_] := 
 And @@ Table[ 
   b[[i, 1]] >= Total@result[[i]] >= b[[i, 2]]  , {i, Length@result}]

and calculate the score:

score[result_] := Total@MapThread[ Dot, { a, result }]

now for this small problem NMaximise gets the solution..

NMaximize[{score[result], 
  c1[result] && c2[result] && 
   And @@ (0 <= # <= 1 & /@ Flatten[result])} , {v1, v2, v3, 
  v4}, Integers ]

{7., {v1 -> 1, v2 -> 0, v3 -> 0, v4 -> 1}}

result /. %[[2]]

{{1, 0}, {0, 1}}