I am new in pyomo. I am trying to code three decision variables with indices t. For example y_j^t and X_ij^t and u_i^t. However, every t in these variables are belong to different categories. How can I code it? As you see, they all have t . But for example t in y means time of departure and in x means time of loading. Should i define three different sets for t?
Related
I`m using a function within a script which produces a number of variables.
The function is used at different places in the script.
It produces variables a,b,c,d,e etc
def function(input):
a,b,c,d,e = 1,2,3,4,5
return a,b,c,d,e
I call the function as follows:
a,b,c,d,e = function(input)
This way all the variables a,b,c,d,e etc... need to be repeated every time I want to use the function. Is there a way to prevent this repetition?
For example I tried to make a list of the variables and then zip them with the output of the function and then connect them but that is not working.
listofvariables = [a,b,c,d,e]
outputfunction = function(input)
gezipt = zip(listofvariables,outputfunction)
for itm in gezipt:
itm[0] = itm[1]
That way when I change the function I would have to rewrite the list with the variables once and not every time the function is used. but that doesnt work.
The answer to your question, of whether you can programmatically set variables is mostly no. There probably are ways you could make it work, but they'd be fragile or hard to understand. Generally speaking, treating variable names as part of your program's data is a bad idea.
If you have lots of related data items, you should generally be using a data structure to group them all together, rather than using separate variables for each one. Your example is dealing with a batch of five variables, and at several different points keeps them in a tuple. That tuple is probably what you want, though you could use a list if you need to modify the values, or maybe a dictionary if you wanted to be able to index them by key (e.g. {"a": 1, "b": 2}). For other more complicated situations, you might need to program up your own container for the data, such as a class. You might even use several different containers for different sub-sets of your data, and then keep those separate containers in a further container (resulting in a nested data structure).
Unfortunately, that's about as detailed as we can get without more details about your circumstances. If you need more specific help, you should probably ask another question with more details about your actual application.
I'd like to use boost units for a project I work on, but before I start I'd like to check that it's appropriate for what I want to do. I've checked the documentation and the code, but I don't see an example that assures me that I can do what I need. I am a scientist, not a programmer, so I'm not familiar with the details of how this works.
I use maps to store quantities, and the quantities have different dimensions. It looks like a quantity must be associated with a dimension or unit though. Is something like the following possible?
map<string, quantity<>> parameters; // I don't know what to put here. Is there a class that represents a quantity without immediately specificying the dimension or unit?
parameters["distance"] = 2 * meters;
parameters["duration"] = 30 * seconds;
quantity<velocity> v = parameters.at("distance") / parameters.at("duration");
My goal is a map with the most general concept of a quantity, i.e., a value with units.
Whereas all of the examples have quantity with a value and specific units.
No, boost units is for compile time checks. Dynamicly chosen objects cannot easily be checked this way.
Use a struct instead of a map.
I am working on a problem and implementing an algorithm in C++. The algorithm requires a data structure in which it is similar to a 2D array (say a 20x20 array). The main different is that every cell must connect to the eight neigbours around it (i.e. up, down, left, right and four corners).
The status of each member will change based on the data changes of the neighbors. So, every cell grow dynamically. Each cell needs to check the data of all its neighbours constantly.
Based on that requirement, I imagine this data structure is circular, like a torus or a bagel, which has no edge, so that every cell is interconnected to each other.
Any idea on the representation of this data structure? I am thinking to use a graph of adjacency linked list, in which each member contains a linked list of the surrounding eight neighbors. What do you think? Am I on the right track?
Look for an implementation of the Game of Life, which does this with zeros and ones. Unless you are doing something very sophisticated by converging a solution to a set of constraints for each iteration, you will just loop through your array each time, referencing the last complete generation, and update everything to create the new generation at the end of each loop.
Mostly it depends on your problem, but I have my doubts regarding adjacency linked list. That would be more suitable if your neighbours were growing dynamically, but in this case they seem to be fixed. So you might as well just use an array that points to your neighbours.
Problem statement seems not very clear:
The status of each member will change based on the data changes of the neighbors. So, every cell grow dynamically. Each cell needs to check the data of all its neighbours constantly.
what does this actually mean? Lets assume just one value changes. Then all the neighbours should change, and all their neighbours change again, until ALL values have changed. But what about the original value, after a change it's neighbours change, should it change again in response (and keep changing indefinitely - sounds like a bad idea)?
What about this: we have a simple case of a 1x4 2D array A B C D where A is a neighbour of B and D, and B of A and C, etc.
Say A changes. So should B and D. Now, C should change - should it instantly change based on both changes in B and D? or B first, D second? or what?
What is the meaning/definition of constantly and dynamically in your problem? Are there time steps, e.g.
time 1: a cell changes
time 2: all immediate neighbours change simultaneously
time 3: neighbours of immediate neighbours change
(and what about the original cell at this point?)
time 4: etc.
In most cases I would (as most others) suggest a 2D-array based structure, but with a setter method, which, upon invocation, would do change propagation atomically. But it really depends on your the definition of constantly and dynamically
I am a relative beginner to C++. I am working on a model related to forecasting property financials, and I am having a few issues getting my data structures setup.
A bit of background - the specific task I am trying to do it setup class variables for key data structures - one such structure called PropFinance. This structure will house all of my key information on a given property (with iterations for each property in a collection of them), including forecasts of future performance. Two of the main arguments being passed to the program are (applicable to all properties to be evaluated)
(1) number of iterations (Iterations) - how many times we are going to generate a forecast (random iterations)
(2) length of forecast (NumofPeriods) - how many periods we are going to forecast
The PropFinance class has 79 variables in it containing property details. A simple example - Expenses. For expenses, and many of my variables like it, I will need to create a 3D array of doubles - one dimension for each iteration, one dimension for each forecasted period. So ideally, I would have a variable for Expenses of:
class PropFinance {
double Expenses[Iterations][NumofPeriods];
}
but, I don't know Iterations and NumofPeriods at compile time. I do know the value of these two variables at the outset of runtime (and they will be constant for all iterations/properties of the current program execution)
My question is how can I have the size of these arrays dynamically updated when the program runs? Based on my research on this site and others, it seems like the two main ways to accomplish this are
(1) Use
(2) Use a pointer in the class definition and then use new and delete to manage
But even with those two options, I am not sure if it will work with a third dimension (all of the examples I saw needed just a single dimension to be dynamically sized). Could someone post either a verbal explanation or (better) a simple code example showing how this would work in (1) or (2) above? Any guidance on which option is preferable would be appreciated (but don't want to start a "what's better" debate). It seems like vector is more appropriate when the size of the array is going to be constantly changing, which is not the case here...
The overall speed of this model is critical, and as we expand the number of iterations and properties things get large quickly - so I want to do things as efficiently as possible.
Sorry I didn't post code - I can try to put something together if people are unable to discern what I am asking from above.
The idiomatic solution is to avoid direct heap allocations of C-arrays, and to prefer an STL container like std::vector, which automatically handles resizing, iteration, and element access in an efficient, portable manner. I would highly recommend Scott Meyers' Effective STL, which talks about appropriateness of each container for different applications - insertion/removal/retrieval complexity gaurantees, etc.
If you need more than 2 dimensions(3, 4, 5 and so on).The most easiest solution I know is using the multi_array provided by boost.
If you only need two dimension array, use vector
std::vector<std::vector<double> > Expenses;
Since you are a beginner, you better start with the higher level components provided by c++, even you are familiar with c++, you should stay with those high level components too.The basic elements of c++ are used when you need to develop some infrastructure(vector, list, smart pointers, thread and so on).
#include <iostream>
#include <vector>
int main()
{
std::vector<std::vector<double> > expenses[10]; //contains 10 std::vector<double>
expenses[0].push_back(100);
std::cout<<expenses[0][0]<<std::endl;
expenses.push_back(std::vector<double>()); //now expenses has 11 std::vector<double>
return 0;
}
how to use vector
multi array
I think you are approaching object oriented programming wrong.
Instead of having a master class PropFinance with everything in many dimensions arrays. Have you considered having classes like Iteration which has multiple Period such as
class Iteration
{
std::vector<PeriodÂ> _periods;
}
class Period
{
public:
double Expense;
}
Then as you add more dimensions you can create super classes PropFinance
class PropFinance
{
std::vector<Iteration> _iterations;
}
This makes everything more manageable instead of having deeply nested arrays [][][][]. As a rule of thumb, whenever you have multiple dimension arrays, consider creating subclasses containing the other dimension.
Suppose there are two functions of x, f and g. Both change the value of x.
Case 1: Both are unserialized and are executed in parallel.
Case 2: f is serialized and g is not. They are executed in parallel.
Question:
Let all the possible values of x after the complete execution in case 1 be N.
Let all the possible values of x after the complete execution in case 2 be M.
is M equal to N?
In other words::
Is there any difference if only one of the two functions is serialized?
Unless both the functions are serialized, will there be any use of the serialization?
As the text says,
serialization creates distinguished sets of procedures such that only one execution of a procedure in each serialized set is permitted to happen at a time
so you need to serialize both procedures that will make changes on the shared varible.
Non-rigorous (sorry if this is for homework ;-)) but practical answer: all mutations (i.e., both functions in your case) must be synchronised in order to have predictable results.