How can I edit my code on CPLEX about tuple? - tuples

Good evening! I have created this tuple in which I go to identify with 1 the jobs that can be carried out at night and 0 those during the day. in the set D I would like to select only the jobs that can be carried out at night, therefore those with nightyes equal to 1. So in my example I want D to be equal to (1,4,5). I try in this way but CPLEX returns all the jobs indiscriminately. How can I change this code? Thank you.
tuple night {
int jobs;
int nightyes;
}
{night} jobsnotturni =...;
{int} nightyes = {i.nightyes|i in jobsnotturni };
{int} D = {i.jobs|i in jobsnotturni, n in nightyes : n==1};
jobsnotturni = {<1,1>, <2,0>,<3,0>,<4,1>,<5,1>};

if you change your .mod into
tuple night {
int jobs; int nightyes;
}
{night} jobsnotturni =...;
{int} nightyes = {i.nightyes|i in jobsnotturni };
{int} D = {i.jobs|i in jobsnotturni: i.nightyes==1};
execute
{
writeln(D);
}
then you ll get
{1 4 5}

Related

Counting active tasks using start time and duration in C++

The input consists of a set of tasks given in increasing order of start time, and each task has a certain duration associated.
The first line is number of tasks, for example
3
2 5
4 23
7 4
This means that there are 3 tasks. The first one starts at time 2, and ends at 7 (2+5). Second starts at 4, ends at 27. Third starts at 7, ends at 11.
We assume each task starts as soon as it is ready, and does not need to wait for a processor or anything else to free up.
This means we can keep track of number of active tasks:
Time #tasks
0 - 2 0
2 - 4 1
4 - 11 2
11 - 27 1
I need to find 2 numbers:
Max number of active tasks at any given time (2 in this case) and
Average number of active tasks over the entire duration computed here as :
[ 0*(2-0) + 1*(4-2) + 2*(11-4) + 1*(27-11) ] / 27
For this,
I have first found the time when all tasks have come to an end using the below code:
#include "stdio.h"
#include "stdlib.h"
typedef struct
{
long int start;
int dur;
} task;
int main()
{
long int num_tasks, endtime;
long int maxtime = 0;
scanf("%ld",&num_tasks);
task *t = new task[num_tasks];
for (int i=0;i<num_tasks;i++)
{
scanf("%ld %d",&t[i].start,&t[i].dur);
endtime = t[i].start + t[i].dur;
if (endtime > maxtime)
maxtime = endtime;
}
printf("%ld\n",maxtime);
}
Can this be done using Priority Queues implemented as heaps ?
Your question is rather broad, so I am going to just give you a teaser answer that will, hopefully, get you started, attempting to answer your first part of the question, with a not necessarily optimized solution.
In your toy input, you have:
2 5
4 23
7 4
thus you can compute and store in the array of structs that you have, the end time of a task, rather than its duration, for later usage. That gives as an array like this:
2 7
4 27
7 11
Your array is already sorted (because the input is given in that order) by start time, and that's useful. Use std::sort to sort the array, if needed.
Observe how you could check for the end time of the first task versus the start time of the other tasks. With the right comparison, you can determine the number of active tasks along with the first task. Checking whether the end time of the first task is greater than the start time of the second task, if true, denotes that these two tasks are active together at some point.
Then you would do the same for the comparison of the first with the third task. After that you would know how many tasks were active in relation with the first task.
Afterwards, you are going to follow the same procedure for the second task, and so on.
Putting all that together in code, we get:
#include "stdio.h"
#include "stdlib.h"
#include <algorithm>
typedef struct {
int start;
int dur;
int end;
} task;
int compare (const task& a, const task& b) {
return ( a.start < b.start );
}
int main() {
int num_tasks;
scanf("%d",&num_tasks);
task *t = new task[num_tasks];
for (int i=0;i<num_tasks;i++) {
scanf("%d %d",&t[i].start,&t[i].dur);
t[i].end = t[i].start + t[i].dur;
}
std::sort(t, t + num_tasks, compare);
for (int i=0;i<num_tasks;i++) {
printf("%d %d\n", t[i].start, t[i].end);
}
int max_noOf_tasks = 0;
for(int i = 0; i < num_tasks - 1; i++) {
int noOf_tasks = 1;
for(int j = i + 1; j < num_tasks; j++) {
if(t[i].end > t[j].start)
noOf_tasks++;
}
if(max_noOf_tasks < noOf_tasks)
max_noOf_tasks = noOf_tasks;
}
printf("Max. number of active tasks: %d\n", max_noOf_tasks);
delete [] t;
}
Output:
2 7
4 27
7 11
Max. number of active tasks: 2
Now, good luck with the second part of your question.
PS: Since this is C++, you could have used an std::vector to store your structs, rather than a plain array. That way you would avoid dynamic memory allocation too, since the vector takes care that for you automatically.

Loop terminating prematurely for multiple queries

For this problem I need to to do multiple queries so I used a for loop for it
#include<bits/stdc++.h>
using namespace std;
int main(){
vector<int>ans; //vector to store the ans
string s,e; //real dna and virus dna
cin>>s;
int q,start,match=0; // no. of queries, starting value of the query, var to store matching occurence
unsigned int step=0; //var to keep count of virus dna iteration
cin>>q;
for(int i=0;i<q;i++){//loop to iterate q times
cin>>start;
int l,r,x,c;
if(start==2){ // for 2nd type query
cin>>l>>r>>e;
for(int i=l-1;i<=r-1;i++){
if(s[i]==e[step]){
match+=1;
}
step+=1;
if(step== e.length()){
step=0; //starting again from start of virus
}
}
}
ans.push_back(match);
match=0; //reintializing value for next query
if(start==1){ //for 1st type query
cin>>x>>c;
s[x-1]=c; //replacing char at x-1 with c
}
}
for(int j=0;j<ans.size();j++){ //loop for ans output
cout<<ans[j]<<endl;
}
return 0;
}
but it terminates before it should for ex: for this input,
ATGCATGC
4
2 1 8 ATGC
2 2 6 TTT
1 4 T
2 2 6 TA
It would stop at 5th line and print 8 ,2 ,0, 0 whereas it should be 8, 2, 4. If I do individual queries without loop things work fine but any kind of loop doesn't work. Pls help. Also any suggestion for solving this kind of problems more efficiently will be very helpful to me.
The bigger problem in your code is that the variable c is defined as int
int l,r,x,c;
when receive chars and not integers (T, in your example) so should be defined as char
int l,r,x;
char c;
If c is int, when you send 4 T to
cin>>x>>c;
x receive 4, c doesn't receive T (T isn't a valid int) so start the next iteration of the external cycle, is called
cin>>start;
when T remain in the buffer; start is integer so there is the same problem and the program end.
And, as pointed by soon, ans.push_back() should be inside the first if. Now a value (zero) is added also with rows starting with 1.

How do I get the number of a specific place in an int in C++?

So I have an int that counts upwards.
Let's say we're up to 65,000 already.
I need to get the number in the thousandth place (the 5), to be assigned to another int.
I found the following snippet which is pretty easy but not quite what I need.
You can use % operator for any number of integers you want to
separate. For example 888881%10 will give you 1 and 888881%100 will
give you 81...
Thanks!
You probably need this.
int AtPos(int number, int pos)
{
return ((number > 0 ? number : -number) / (int)pow(10, pos)) % 10;
}
If this is that you are looking for, arguments validity check should be added.
EDIT.
I just noticed, you need to assign different number in specified position. So you need this improvement:
int& SetValueAtPos(int& number, int pos, int newValue)
{
int power = (int)pow(10, pos);
number -= AtPos(number, pos) * power;
number += power * newValue;
return number;
}
Additionally you can merge those functions and cache value of pow(10, pos) so it would be more optimal.
Try something like this
int a =65432;
int b = ((a%10000)-(a%1000))/1000;
here (a%10000) = 5432
and (a%1000) = 432
so (a%10000)-(a%1000) will be 5000
and finally 5000/1000 = 5
or directly you can use
int b = (a%10000)/1000;
What #Matt suggested in comment is:
int num1 = 65000;
int num2 = num%10000; //num2 is 5000
num2 = num2/1000;
Output: 5

Discrete-event Simulation Algorithm 1.2.1 in C++

I'm currently trying to work and extend on the Algorithm given in "Discrete-event Simulation" text book pg 15. My C++ knowledge is limited, It's not homework problem just want to understand how to approach this problem in C++ & understand what going.
I want to be able to compute 12 delays in a single-server FIFO service node.
Algorithm in the book is as follow:
Co = 0.0; //assumes that a0=0.0
i = 0;
while (more jobs to process) {
i++;
a_i = GetArrival ();
if (a_i < c_i - 1)
d_i = c_i - 1 - a_i; //calculate delay for job i
else
d_i = 0.0; // job i has no delay
s_i = GetService ();
c_i = a_i + d_i + s_i; // calculate departure time for job i
}
n = i;
return d_1, d_2,..., d_n
The GetArrival and GetService procedures read the next arrival and service time from a file.
Just looking at the pseudo-code, it seems that you just need one a which is a at step i, one c which is c at step i-1, and an array of ds to store the delays. I'm assuming the first line in your pseudo-code is c_0 = 0 and not Co = 0, other wise the code doesn't make a lot of sense.
Now here is a C++-ized version of the pseudo-code:
std::vector<int> d;
int c = 0;
int a, s;
while(!arrivalFile.eof() && !serviceFile.eof())
{
arrivalFile >> a;
int delay = 0;
if (a < c)
delay = c - a;
d.push_back(delay);
serviceFile >> s;
c = a + delay + s;
}
return d;
If I understand the code right, d_1, d_2, ..., d_n are the delays you have, number of delays depends on number of jobs to process. while (more jobs to process)
thus if you have 12 processes you will have 12 delays.
In general if arrival time is less than previous departure time then the delay is the previous departure time - current arrival time
if (a_i < c_i-1)
d_i = c_i-1 - a_i;
the first departure time is set to zero
if something is not clear let me know

Returning a Linear List in C++ and Time Complexity

I'm currently studying for my data structures exam and ran across a problem I could use clarification on. I'm supposed to create a function InsertZero(int k, int i) that inserts k zeroes after element i, checking indices each time and throwing appropriate exceptions.
I've done this, but I'm stuck on how to return a LinearList& that the function definition is asking me to in the class. I've tried return *element, return &element, and a few others to no avail. Where am I going wrong?
Additionally, I'm supposed to give the time complexity of the function as a "function of list length and k". I analyzed the steps throughout the function (see comments) and came up with O(k)...this doesn't use the list length, and I'm a bit confused on how to do so.
Any help will be greatly appreciated. I'm looking for comprehension, not just answers.
template <class T>
LinearList<T>& LinearList<T>::InsertZero(int i, int k)
{
//Complexity statements are in the form of
// "Number of steps" * "Number of times executed" = total
if ( i<0 || i> (MaxSize-1) || k<0) // 3 * 1 = 3
cout<<"Bad input exception thrown"<<endl;// 1 * 1 = 1
else if (k > (MaxSize-i-1) ) // 1 * 1 = 1
cout<<"NoMem exception thrown"<<endl; // 1 * 1 =1
else
{
while (k!=0) // 1 * k = k
{
element[i+1]=0; // 1 * k = k
i++; // 1 * k = k
k--; // 1 * k = k
}
return &element; // 1 * 1 = 1
}
//Total = 3+1+1+1+k+k+k+k+1 = 4k+7 = O(k)
}
I guess element array is a data member of the LinearList class. element is the basic C++ type (array of ints) whereas LinearList is derived one. I would do return *this at the end of your method.
The return type looks to be a the same type as the class. So you should return *this. It appears that elment should be a member variable, and that there is no need to return it.