How to make a rigid alternative constraint based on earn value - scheduling

i want to make a rescheduling model. I have 2 modes in tuple, I declare it using ID, I want to make a constraint if the costprod * duration in present's mode in day 1-7 <= earn value than choose mode 2. But I think something missing in model. but I didn't know where is it. i think it will be need a constraint like at day 1 = earn value (0) and the end day = total earn value. I stuck on this part
range IntRes= 0..NbIntRes-1;
range ExtRes=0..NbExtRes-1;
int CapIntRes[IntRes]=...;
int CapExtRes[ExtRes]=...;
tuple Task {
key int id;
{int} succs;
int RelDate;
int DueDate;
}
{Task} Tasks = ...;
tuple Mode {
key int taskId;
key int id;
int pt;
int costprod;
int dmdIntRes [IntRes];
int dmdExtRes [ExtRes];
int ExtCost;
}
{Mode} Modes = ...;
dvar interval Taskss [t in Tasks] in t.RelDate..t.DueDate ;
dvar interval mode[m in Modes] optional size m.pt;
dexpr int totalExtCost = sum(m in Modes) presenceOf(mode[m])* (m.ExtCost * m.pt); //boolean expression
cumulFunction IntResUsage[r in IntRes] =
sum (m in Modes: m.dmdIntRes[r]>0) pulse(mode[m], m.dmdIntRes[r]);
cumulFunction ExtResUsage[r in ExtRes] =
sum (m in Modes: m.dmdExtRes[r]>0) pulse(mode[m], m.dmdExtRes[r]);
execute {
cp.param.FailLimit = 10000;
}
minimize max(t in Tasks) endOf(Taskss[t]);
subject to {
//Alternative mode of resource productivity in Cost's unit
forall (t in Tasks, m in Modes) {
// if(m.costprod *m.pt == 0 && 0 <= 559717712) presenceOf(mode[first(Modes)]);
alternative(Taskss[t], all(m in Modes: m.taskId==t.id) mode[m]);
}
forall (t in Tasks, m in Modes)
(sum(t in Tasks)sum(m in Modes) m.costprod * m.pt <= 559717712 in 0..NbDays-14) != presenceOf(mode[first(Modes)]);
//External resource's budget limitation
forall ( m in Modes)
totalExtCost <= 20000000;
This one the data
NbDays= 21;
NbTask= 10;
NbIntRes= 1;
NbExtRes= 1;
CapIntRes= [25];
CapExtRes= [15];
Tasks={
< 0, {5, 6 },1, 5>,//first mobilization
< 1, { 5, 6 },2,10 >,
< 2, { 5, 6 },2,10>,
< 3, { 5, 6 },2,15>,
< 4, { 5, 6 },2,15>,
< 5, {6},4,21>,
< 6, {7},4 ,21>,
< 7, {8},5,10>,
< 8, {9},15,21>,
< 9, { },17,21 >,//end mobilization
};
Modes = {
< 0, 1, 3, 700000,[ 2 ], [ 0 ],0 >,
< 0, 2, 2, 1060000,[ 2 ], [ 1 ],300000 >,
< 1, 1, 4, 1920000,[ 6 ], [ 0 ],0 >,
< 1, 2, 3, 2600000,[ 6 ], [ 2 ],600000 >,
< 2, 1, 4, 880000,[ 4 ], [ 0 ],0 >,
< 2, 2, 3, 1140000,[ 4 ], [ 1 ],300000 >,
< 3, 1, 4, 2580000,[ 6 ], [ 0 ],0 >,
< 3, 2, 3, 3480000,[ 6 ], [ 2 ],600000 >,
< 4, 1, 4, 22500000,[ 9 ], [ 0 ],0 >,
< 4, 2, 3, 32900000,[ 9 ], [ 4 ],12000000 >,
< 5, 1,15, 8400000,[ 7 ], [ 0 ],0 >,
< 5, 2,11, 11100000,[ 7 ], [ 2 ],600000 >,
< 6, 1,15,276000000,[12 ], [ 0 ],0 >,
< 6, 2,12,348000000,[12 ], [ 3 ],900000 >,
< 7, 1, 2, 36800000,[ 8 ], [ 0 ],0 >,
< 7, 2, 1, 60300000,[ 8 ], [ 5 ],1500000 >,
< 8, 1, 6, 33800000,[ 6 ], [ 0 ],0 >,
< 8, 2, 4, 46600000,[ 6 ], [ 3 ],900000 >,
< 9, 1, 3, 700000,[ 2 ], [ 0 ],0 >,
< 9, 2, 2, 1060000,[ 2 ], [ 1 ],300000 >,
};

Related

Cannot specify explicit initializer for arrays [SystemC]

Im using VS2013 along with the SystemC library from Allegro. I was trying to initialize two arrays as follows:
int pathObs1[19] = {10,9,8,7,6,5,4,3,2,1,2,3,4,5,6,7,8,9,10};
int Map[10][4] = {
{ 0, 3, 1, 4 }, //Grid 1
{ 1, 3, 2, 4 }, //Grid 2
{ 2, 3, 3, 4 }, //Grid 3
{ 3, 3, 4, 4 }, //Grid 4
{ 4, 3, 5, 4 }, //Grid 5
{ 5, 3, 6, 4 }, //Grid 6
{ 6, 3, 7, 4 }, //Grid 7
{ 6, 2, 7, 3 }, //Grid 8
{ 6, 1, 7, 2 }, //Grid 9
{ 6, 0, 7, 1 } //Grid 10
};
However i received the error the above error. I saw some questions on SO which had the same issue, however I dont think they were dealing with SystemC. Any easy workaround for this in SystemC since im trying to initialize inside my SC_MODULE header/constructor?
Edit: I had a typo in my array initialization. Still get the same error.
2dArray[m][n] means m rows n columns so you can keep n values in each row but in your code you defined matrix which had 3 columns but still you are assigning 4 values.
You can use a loop for filling the array:
#include <iostream>
#include <stdlib>
int main()
{
srand(time(null));
int map[10][4];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 4; j++)
{
map[i][j] = rand(); // you can write smth like rand() % 5 to make a limit of the values
}
}
return 0;
}

Max subarray with start and end index

I'm trying to find the maximum contiguous subarray with start and end index. The method I've adopted is divide-and-conquer, with O(nlogn) time complexity.
I have tested with several test cases, and the start and end index always work correctly. However, I found that if the array contains an odd-numbered of elements, the maximum sum is sometimes correct, sometimes incorrect(seemingly random). But for even cases, it is always correct. Here is my code:
int maxSubSeq(int A[], int n, int &s, int &e)
{
// s and e stands for start and end index respectively,
// and both are passed by reference
if(n == 1){
return A[0];
}
int sum = 0;
int midIndex = n / 2;
int maxLeftIndex = midIndex - 1;
int maxRightIndex = midIndex;
int leftMaxSubSeq = A[maxLeftIndex];
int rightMaxSubSeq = A[maxRightIndex];
int left = maxSubSeq(A, midIndex, s, e);
int right = maxSubSeq(A + midIndex, n - midIndex, s, e);
for(int i = midIndex - 1; i >= 0; i--){
sum += A[i];
if(sum > leftMaxSubSeq){
leftMaxSubSeq = sum;
s = i;
}
}
sum = 0;
for(int i = midIndex; i < n; i++){
sum += A[i];
if(sum > rightMaxSubSeq){
rightMaxSubSeq = sum;
e = i;
}
}
return max(max(leftMaxSubSeq + rightMaxSubSeq, left),right);
}
Below is two of the test cases I was working with, one has odd-numbered elements, one has even-numbered elements.
Array with 11 elements:
1, 3, -7, 9, 6, 3, -2, 4, -1, -9,
2,
Array with 20 elements:
1, 3, 2, -2, 4, 5, -9, -4, -8, 6,
5, 9, 7, -1, 5, -2, 6, 4, -3, -1,
Edit: The following are the 2 kinds of outputs:
// TEST 1
Test file : T2-Data-1.txt
Array with 11 elements:
1, 3, -7, 9, 6, 3, -2, 4, -1, -9,
2,
maxSubSeq : A[3..7] = 32769 // Index is correct, but sum should be 20
Test file : T2-Data-2.txt
Array with 20 elements:
1, 3, 2, -2, 4, 5, -9, -4, -8, 6,
5, 9, 7, -1, 5, -2, 6, 4, -3, -1,
maxSubSeq : A[9..17] = 39 // correct
// TEST 2
Test file : T2-Data-1.txt
Array with 11 elements:
1, 3, -7, 9, 6, 3, -2, 4, -1, -9,
2,
maxSubSeq : A[3..7] = 20
Test file : T2-Data-2.txt
Array with 20 elements:
1, 3, 2, -2, 4, 5, -9, -4, -8, 6,
5, 9, 7, -1, 5, -2, 6, 4, -3, -1,
maxSubSeq : A[9..17] = 39
Can anyone point out why this is occurring? Thanks in advance!
Assuming that n is the correct size of your array (we see it being passed in as a parameter and later used to initialize midIndexbut we do not see its actual invocation and so must assume you're doing it correctly), the issue lies here:
int midIndex = n / 2;
In the case that your array has an odd number of elements, which we can represented as
n = 2k + 1
we can find that your middle index will always equate to
(2k + 1) / 2 = k + (1/2)
which means that for every integer, k, you'll always have half of an integer number added to k.
C++ doesn't round integers that receive floating-point numbers; it truncates. So while you'd expect k + 0.5 to round to k+1, you actually get k after truncation.
This means that, for example, when your array size is 11, midIndex is defined to be 5. Therefore, you need to adjust your code accordingly.

How to search for a vector in a matrix in C++ and which algorithm?

Suppose I have a matrix and a vector given by. How can I perform a search algorithm like binary search to return the index?
Example:
const int V_SIZE = 10,H_SIZE = 7;
int a1[V_SIZE][H_SIZE] = {
{1,2,0,0,0,0,0},
{1,3,0,0,0,0,0},
{2,2,4,0,0,0,0},
{2,2,6,0,0,0,0},
{3,2,4,7,0,0,0},
{4,1,3,5,9,0,0},
{4,1,4,6,8,0,0},
{4,2,3,4,7,0,0},
{5,2,3,5,7,8,0},
{6,1,3,4,5,7,10}
}; // sorted
int a2 [H_SIZE] = {4,1,3,5,9,0,0};
Perform a search for the vector a2 in the matrix a1 and the return value is 6
Thank a lot
You could use a 2D std::array in combination with std::lower_bound:
const int V_SIZE = 10,H_SIZE = 7;
std::array<std::array<int, H_SIZE>, V_SIZE> a1 {
{{{1,2,0,0,0,0,0}},
{{1,3,0,0,0,0,0}},
{{2,2,4,0,0,0,0}},
{{2,2,6,0,0,0,0}},
{{3,2,4,7,0,0,0}},
{{4,1,3,5,9,0,0}},
{{4,1,4,6,8,0,0}},
{{4,2,3,4,7,0,0}},
{{5,2,3,5,7,8,0}},
{{6,1,3,4,5,7,10}}
}}; // sorted
std::array<int, H_SIZE> a2 {{4,1,3,5,9,0,0}};
int idx = std::lower_bound(std::begin(a1), std::end(a1), a2) - std::begin(a1);
LIVE DEMO
If the matrix is sorted on the first number, you could use binary search to find an approximate index. You then have to go back until you find the first row starting with the same number as in the vector, as well as forward to find the last row starting with the same number. Then you loop over the vector, searching for a match for the second, third, etc. number in the range of rows you have.
What about something like this using std::array?
template <int HSIZE>
bool operator<(const std::array<int, HSIZE> &lhs, const std::array<int, HSIZE> &rhs)
{
for (int i = 0; i < HSIZE; i++)
if (lhs[i] != rhs[i])
return lhs[i] < rhs[i];
return false;
}
std::array<int, 7> a1[] =
{
{ 1, 2, 0, 0, 0, 0, 0 },
{ 1, 3, 0, 0, 0, 0, 0 },
{ 2, 2, 4, 0, 0, 0, 0 },
{ 2, 2, 6, 0, 0, 0, 0 },
{ 3, 2, 4, 7, 0, 0, 0 },
{ 4, 1, 3, 5, 9, 0, 0 },
{ 4, 1, 4, 6, 8, 0, 0 },
{ 4, 2, 3, 4, 7, 0, 0 },
{ 5, 2, 3, 5, 7, 8, 0 },
{ 6, 1, 3, 4, 5, 7, 10 }
};
void search(void)
{
std::array<int, 7> a2 = { 4, 1, 3, 5, 9, 0, 0 };
std::array<int, 7> *a1_end = a1 + sizeof(a1) / sizeof(std::array<int, 7>);
std::array<int, 7> *it = std::lower_bound(a1, a1_end, a2);
}

project euler brute force choosing each path

I'm trying to brute force the following problem
https://projecteuler.net/problem=18
I can't think of a solution and my head is stucked, I don't know how to choose each path. what I did is the following
vector<vector<int> > triangle =
{
{ 3 },
{ 7, 4 },
{ 2, 4, 6 },
{ 8, 5, 9, 3 }
};
int maxSum = 0;
for (int i = 0; i < triangle.size(); i++)
{
for (int j = 0; j < triangle[i].size(); j++)
{
}
}
3
7 4
2 4 6
8 5 9 3
When brute-forcing, you have to sum the numbers of following branches and find the maximum:
3, 7, 2, 8
3, 7, 2, 5
3, 7, 4, 5
...
3, 4, 6, 3
How do you generate such sequences?

problrm with array in c++

this is my Stata.h
#include "State.h"
#include <iostream>
using namespace std;
class State
{
public:
int data[4][4];
int x;
int y;
int cost[];
State(void);
void CopyState(State*);
bool equals(State*);
int h(State*);
void print(void);
private:
int getLocation(State, int, int);
};
and this is my State.cpp
#include "State.h"
#include <iostream>
using namespace std;
State::State(void)
{
State::cost[] = {0, 3, 2, 5, 3, 4, 1, 2, 2, 1, 4, 3,
5, 2, 3, 2, 3, 0, 3, 2, 2, 3, 2, 1,
1, 2, 1, 4, 2, 3, 2, 3, 2, 3, 0, 3,
1, 2, 3, 2, 4, 1, 2, 1, 3, 2, 3, 2,
5, 2, 3 ,0 ,2 , 1 , 4 , 3 ,3 , 4 ,1 , 2,
2, 3, 2, 5, 3, 2, 1, 2, 0, 3, 2, 3,
3, 2, 1 ,2 ,2 , 1 ,4 ,3 , 4 ,3 ,2 , 1 ,
3 ,0, 3, 2, 2 , 3, 2, 1 , 1 , 2, 1, 4,
1 , 2, 3 ,4 ,2 ,3 ,0 ,3 ,1 , 2 ,3 ,2 ,
4 ,1, 2, 1, 2, 1, 2, 3, 3, 2, 3, 0,
2 ,1, 2 , 3 ,3 ,4 ,1 ,2 , 2 , 1 ,4 ,3,
3 , 2, 1, 2, 0 , 3 , 2 , 3, 3 , 2, 1, 2,
1 ,2, 1 , 4 ,2 ,3 ,2 , 1 , 3 , 0 , 3 , 2,
4 , 3, 2 , 1 , 4 , 1, 2 , 1 , 1 , 2, 3 , 2,
2, 3 ,0 ,3 ,1 , 2 , 3 , 4 , 3 ,4 , 1 ,2,
2 ,1, 2, 3 , 3 , 2 , 3 , 0, 2, 1, 2, 3,
5 , 2 ,3 , 2 ,2 ,1 , 4 ,3 ,3 ,4 ,1 ,2,
0 ,3, 2, 5, 2 , 3 , 2, 3, 1, 2, 1, 4,
2 ,3 ,2 , 1 ,3 , 0 ,3 , 2, 3 , 2 ,3 ,2 ,
4 ,1, 2, 1, 1 , 2, 3 , 2 , 2, 3 , 0, 3,
2 ,3 ,2 , 5 , 3 , 4 , 1 , 2 , 2 , 1 ,4 , 3 ,
5 , 2 ,3 ,0
};
}
void State::CopyState(State *state)
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
data[i][j] = state->data[i][j];
x = state->x;
y = state->y;
}
bool State::equals(State *state)
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (data[i][j] != state->data[i][j])
return false;
return true;
}
int State::h(State *state)
{
int k = 0;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
{
int frmPos = 4 * i + j;
int toPos = getLocation(*state, i, j);
k += cost[16 * frmPos + toPos];
}
return k;
}
void State::print()
{
cout << "\n";
for (int i = 0; i < 4; i++)
{
cout << "\n";
for (int j = 0; j < 4; j++)
cout << data[i][j];
}
cout << endl;
}
the problem is in State.cpp in the constructor when I'm trying to assign values to the cost array I don't know where is the problem, please any one can solve that , sorry for my bad english
You can't do it that way. There's a couple of problems, but the first problem is this:
In the header you declare cost as an array with an unspecified size:
int cost[];
This isn't allowed in Standard C++ because now the compiler doesn't know how big the array is, and therefore doesn't know how big State will be. (Some compilers do allow it, but it's a special extension that won't work on other compilers.)
The simplest and often best way to do this is to use a vector:
#include <vector>
class State
{
// ...
std::vector <int> cost;
};
And then in the constructor:
State::State()
{
static const int startingValues [] =
{0, 3, 2, 5, 3, 4, 1, 2, 2, 1, 4, 3, ... };
static const size_t numStartingValues = sizeof (startingValues) / sizeof (startingValues [0]);
std::copy (startingValues, startingValues + numStartingValues, std::back_inserter (cost));
}
If you're using C++11, then this is made simpler with the uniform initialization syntax:
State::State()
:
cost {0, 3, 2, 5, ...}
{
}
First of all, the cost member is not an array, it is a pointer (or it was when I looked at it, but you've changed it. However, it's still not an array. It is now a compiler error.). If you want it to point to an array, first you have to allocate it. But that's a terrible idea. Change it to a vector:
class State {
...
std::vector<int> cost;
...
};
Then, in your constructor, you can initialize it like this (if your compiler supports C++11)
State::State() :
cost{0, 3, 2, 5, 3, 4} // trimmed for brevity, but you can put as many elements as you want
{}
If your compiler does not support C++11 (or does not support this feature), then you can initialize the vector like this:
State::State()
{
static int temp[] = {0, 3, 2, 5, 3, 4};
cost.assign(temp, temp + sizeof(temp) / sizeof(*temp));
}