I have been playing around with libspotify and encountered something a bit odd. Are the index parameters to sp_playlistcontainer_move_playlist 0 based or 1 based? They seem to be both, or maybe neither :) Specifically if I have three playlists I notice the following results:
sp_playlistcontainer_move_playlist(handle, 0, 3, false)
succeeds and moves the playlist at index 0 (the first one in the list of playlist) to the end of the list of playlists.
sp_playlistcontainer_move_playlist(handle, 0, 1, false)
fails, returning SP_ERROR_INVALID_INDATA, which according to the API specs seems to indicate I am 'trying to move a folder into itself'. From that I guessed that the input (original) index is 0 based, and the target index is 1 based. This is odd, but this
sp_playlistcontainer_move_playlist(handle, 0, 2, false)
does appear to move the first playlist (from index 0) to the second slot in the list of playlists (at what I would call index 1, but apparently is 2 according to libspotify).
Of course this also works
sp_playlistcontainer_move_playlist(handle, 2, 0, false)
so maybe the target index is not 1 based...or maybe 0 is just special cased. Thoughts?
It's 0-based.
Move operations always take indexes in the list's state BEFORE anything is done, so...
Moving 0 to 1 will be rejected, because it's actually a no-op.
To move the first (index 0) playlist to the second position (index 1), you actually move it to index 2, since you want it to be after the playlist that's currently at index 1. Once you make the call, libSpotify will "commit" the transaction by moving index 0 to be after index 1, then moving everything down one slot to fill in the vacant gap left at index 0.
Related
I would like to constrain locations to be served by the same vehicle.
I used capacity-constraints for achieving this. Say we have l = [[1,2], [3,4]] which means that location 1, 2 must be served by the same vehicle and 3, 4 as well. So 1, 2 ends up on route_1 and 3, 4 on route_2
My code for achieving this is:
for idx, route_constraint in enumerate(l):
vehicle_capacities = [0] * NUM_VEHICLES
vehicle_capacities[idx] = len(route_constraint)
route_dimension_name = 'Same_Route_' + str(idx)
def callback(from_index):
from_node = manager.IndexToNode(from_index)
return 1 if from_node in route_constraint else 0
same_routes_callback_index = routing.RegisterUnaryTransitCallback(callback)
routing.AddDimensionWithVehicleCapacity(
same_routes_callback_index,
0, # null capacity slack
vehicle_capacities, # vehicle maximum capacities
True, # start cumul to zero
route_dimension_name)
The idea is that 1,2 have a capacity demand of each 1 unit (all others have zero). As only vehicle 1 has a capacity of 2 it is the only one able to serve 1,2.
This seems to work fine if len(l) == 1. If greater the solver is not able to find a solution if though I put into l pairs of locations which were on the same route without the above code (hence without the above capacity constraints.
Is there a more elegant way to model my requirement?
Why does the solver fail to find a solution?
I have also considered the possibility of dropping visits (at a high cost) to give the solver the possibility to start from a solution which drops visits such that it will find his way fro this point to a solution without any drops. I had no luck.
Thanks in advance.
Each stop has a vehicle var whose values determine what vehicle is allowed to visit the stop. If you want to have stops 1 and 2 serviced by vehicle 0 use a member constraint on the vehicle var of each stop and set it to [0]. Since you might have other constraints that make stops optional add the value -1 to the list. It is a special value that indicates that the stop is not serviced by a vehicle.
In Python:
n2x = index_manager.NodeToIndex
cpsolver = routing_model.solver()
for stop in [1,2]:
vehicle_var = routing_model.VehicleVar(n2x(stop))
values = [-1, 0]
cpsolver.Add(cpsolver.MemberCt(vehicle_var, values))
I have a problem with my code and no matter what I do, I always get the index out of bounds error
I tried changing 60 to 40 or 80 but I always have the same problem
def Talus(Nbriter,taille):
M=np.zeros([taille,taille],int)
avalanch=[]
duree=[]
perte=[]
for gain in range(Nbriter):
iaj=rd.randint(1,taille-1)
jaj=rd.randint(1,taille-1)
M[iaj][jaj]+=1
a=1
atot=0
d=0
perdu=0
while a>0:
for i in range(1,taille-1):
for j in range(1,taille-1):
if (M[i][j]-M[i][j+1]>3) or (M[i][j]-M[i][j-1]>3) or (M[i]
[j]-M[i+1][j]>3) or (M[i][j]-M[i-1][j]>3):
M[i][j]-=4
M[i][j+1]+=1
M[i][j-1]+=1
M[i+1][j]+=1
M[i-1][j]+=1
a+=4
d+=1
else:
a=0
atot+=a
for i in range(taille):
perdu+=M[i][0]
M[i][0]=0
perdu+=M[i][-1]
M[i][-1]=0
for j in range(taille):
perdu+=M[0][j]
M[0][j]=0
perdu+=M[-1][j]
M[-1][j]=0
avalanch.append(atot)
duree.append(d)
perte.append(perdu)
return(M,avalanch,duree,perte)
M,avalanch,duree,perte=Talus(100000,40)
I always get this :
Traceback (most recent call last):
File "C:\Users\Poste1\Desktop\mon tipe\codeavalanche2.py", line 52, in
<module>
M,avalanch,duree,perte=Talus(100000,40)
File "C:\Users\Poste1\Desktop\mon tipe\codeavalanche2.py", line 26,
in Talus
if (M[i][j]-M[i][j+1]>3) or (M[i][j]-M[i][j-1]>3) or (M[i][j]-
M[i+1][j]>3) or (M[i][j]-M[i-1][j]>3):
IndexError: index 40 is out of bounds for axis 0 with size 40
It's for a project I need to submit, so please if anyone knows how I can fix it I would be very grateful. Thank you
By the way, my professor told me to change the range into xrange but I am using python 3 so it doesn't exist.
Correct me if I'm wrong, but I think you have some sort of problem understanding the way array indexes work in programming, as Python uses the classic 0 to (size-1) type of notation. Let me explain myself:
I think your problem is the index you are trying to access is relative to the size of the array. That is, you are using taille for the size and j, that in the last iteration is going to be j = taille-1. Here comes the problem, as your array is always going to be of a given size, determined by taille, let's say taille = 5, and then in the last iteration of j, (j+1) = (taille-1)+1 = taille = 5 => M[5] -> ERROR.
The error is completely reasonable, as array's indexes work in this way:
Element 1, Element 2, Element 3, Element 4, Element 5.
Index 0, Index 1, Index 2, Index 3, Index 4.
Or as I said in the beginning, the indexes go from 0 to (size-1), and that means you cannot access the taille-th element of an array size taille.
As to the solution to your problem, if you really want to access i+1 and j+1, you should then change the limits of your for loop from (1, taille-1) to (1, taille-2) in both cases.
Hope you found my answer helpful : )
I created a class in Pharo known as BinarySearchTreean i implemented a method called BinarySearchTree>>PreOrder and BinarySearchTree>>index
Preorder: myArray index: position
(myArray at: position) ~= -1
ifTrue: [
Transcript show: (myArray at: position).
self Preorder: myArray index: (position * 2).
self Preorder: myArray index: (position * 2) + 1.
].
I then provided this array #(90 60 95 50) with index 1 to make a PreOrder search in my binary tree which i implemented using arrays but it does not work.
Please help...
#at: will signal SubscriptOutOfBounds when the index is < 0 or greater than the size of the array (Smalltalk collections are 1-based, i.e. the first index is 1, not 0). 8 is clearly larger than 4 (the size of myArray).
The check at the start will never evaluate to False as your array has no entry -1, and hence the conditional block will be evaluated every time.
I can't really say where your problem lies as you've excluded all the code that is actually of interest. If you add that I can tell you more.
I am trying to design a state machine which will traverse all possible transitions between states. However, the state machine cannot move from a given state back to itself. From the diagram below, I have worked out that given the number of states (N), the number of transitions is equal to N^2 - N.
Any ideas on how to approach this please?
After having misunderstood the problem the first time, here is another attempt.
So we want to transverse the graph in one go, and we are not allowed to use the same transition twice. The trick is probably to leave a track free to get back to the starting state.
states = 4 # Select number of states
path = [0] # Start in state 0 (must be zero)
def walk(path):
home_state = path[-1]
for i in range(home_state + 2, states):
# We leave a state out that we go to next
path.append(i)
path.append(home_state)
if home_state + 1 < states:
path.append(home_state + 1)
walk(path)
path.append(home_state)
walk(path)
print path
should give
[0, 2, 0, 3, 0, 1, 3, 1, 2, 3, 2, 1, 0]
I am trying to write a program that returns the frequency of a certain pattern. My frequency list is initially a list of zeros, and I want to increase a certain zero by one depending on the pattern. I have tried the code below, but it does not work.
FrequencyArray[j] = FrequencyArray[j]+1
Is there another way to increase one element of the list by 1 without affecting the other elements?
While your approach should work, this would be the alternative:
FrequencyArray[j] += 1
Example:
>>> zeros = [0, 0, 0]
>>> zeros[1] += 1
>>> zeros
[0, 1, 0]