14037

“int 'object is not subscriptable”

<h3>Question</h3>

i'm starting to learn GEKKO. Now, I am solving a knapsak problem to learn, but this time I get the error "int 'object is not subscriptable". can you look at this code? what is the source of the problem How should I define the 1.10 matrices?

from gekko import GEKKO import numpy as np m = GEKKO(remote=False) x = m.Var((10),lb=0,ub=1,integer=True) #x = m.Array(m.Var,(1,10),lb=0,ub=1,integer=True) v=np.array([2, 2, 7, 8, 2, 1, 7, 9, 4, 10]) w=np.array([2, 2, 2, 2, 2, 1, 6, 7, 3, 3]) capacity=16 for j in range(10): m.Maximize(v[j]*x[j]) for i in range(10): m.Equation(m.sum(x[i]*w[i])<=capacity) m.options.solver = 1 m.solve() #print('Objective Function: ' + str(m.options.objfcnval)) print(x)

My second question is that there is a function called "showproblem ()" in MATLAB. Does GEKKO have this function?

thanks for help. new question that according to answer. can i write here this style(that doesnt work, if i can do it, please write working style)(i want to write this style, because i think this style is easier to understand.),

for i in range(10): xw = x[i]*w[i] m.Equation(m.sum(xw)<=capacity)

instead of this.

xw = [x[i]*w[i] for i in range(10)] m.Equation(m.sum(xw)<=capacity)
<h3>Answer1:</h3>

Here is a modified version that solves the mixed integer problem in gekko.

<pre class="lang-py prettyprint-override">from gekko import GEKKO import numpy as np m = GEKKO(remote=False) x = m.Array(m.Var,10,lb=0,ub=1,integer=True) v=np.array([2, 2, 7, 8, 2, 1, 7, 9, 4, 10]) w=np.array([2, 2, 2, 2, 2, 1, 6, 7, 3, 3]) capacity=16 for j in range(10): m.Maximize(v[j]*x[j]) xw = [x[i]*w[i] for i in range(10)] m.Equation(m.sum(xw)<=capacity) m.options.solver = 1 m.solve() print('Objective Function: ' + str(-m.options.objfcnval)) print(x)

Your problem formulation was close. You just needed to define a list xw that you use to form the capacity constraint. If you want to use a loop instead of a list comprehension then I recommend the following instead of xw = [x[i]*w[i] for i in range(10)].

<pre class="lang-py prettyprint-override">xw = [] for i in range(10): xw.append(x[i]*w[i])

来源:https://stackoverflow.com/questions/61881241/int-object-is-not-subscriptable

Recommend

  • JavaFX: Display PDF in WebView
  • Python 'hide' methods with __
  • Calling python from php fails when import numpy
  • gitlab throwing - failed to store ssl session
  • MethodNotAllowedException Laravel Password Grant API
  • How to delete yarn logs
  • Having several issues with a Python service for Windows
  • Starting a javascript prompt after a button is clicked
  • How to set a dynamic form fields to dirty or touched with angular?
  • Max execution time error
  • Adding custom message on Thank You page by shipping method
  • Open a cmd program with full functionality (i/o)
  • Webdriver 3.14 IE11: session lost when click link/button that opens a window/popup
  • Special characters (ë) in JSON-response
  • How to close a WebView with double-click?
  • Delete a ListItem by clicking the ImageView within the ListItem
  • Getting Microsoft Calibri font on Amazon EC2 ubuntu
  • First dynamically-added TinyMCE editor displays, others do not
  • Ruby on Rails: Get mediaplayer information (iTunes, TRAKTOR, Cog; current song + playlist)
  • Year over Year Stats from a Crossfilter Dataset
  • Calculate time from document
  • Cloud Code: Creating a Parse.File from URL
  • jQuery scrollTop if URL has hash
  • How to integrate angular2-material (alpha 8.2) with angular2-Quickstart app
  • `$http:badreq Bad Request Configuration` - from angular post method, what is wrong here?
  • Functions by reference or by variable, which to use when?
  • How to get rgb from transparent pixel in js
  • Computing the discrete fourier transform of audio data with FFTW
  • Why my AngularJS async test in Jasmine 1.3.x is not working?
  • VLOOKUP in IMPORTRANGE
  • Capture SIGFPE from SIMD instruction
  • Using Service Component Runtime
  • How to use FirstOrDefault inside Include
  • How do I use TagLib-Sharp to write custom (PRIV) ID3 frames?
  • WPF custom control and direct content support
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication