How to access the values of a dictionary property in a grid using Ocean for Petrel? - ocean

I'm tring to access the values of a dictionary property in a grid,such as Fluvial facies or lithologies etc.I have read the coursebook and help docs, but didn't find anything relevant.The coursebook only has examples of creating properties, but not accessing properties.Below is the code I tried:
Grid grid = arguments.Input_Grid;
if (grid == null)
{
PetrelLogger.ErrorStatus("HelloGrid: Arguments cannot be empty.");
return;
}
Index3 currentCell = new Index3();
int maxI = grid.NumCellsIJK.I;
int maxJ = grid.NumCellsIJK.J;
int maxK = grid.NumCellsIJK.K;
for (int i = 0; i < maxI; i++)
{
for (int j = 0; j < maxJ; j++)
{
for (int k = 0; k < maxK; k++)
{
currentCell.I = i; currentCell.J = j; currentCell.K = k;
if (grid.IsCellDefined(currentCell) && grid.HasCellVolume(currentCell))
{
//DictionaryProperty p = ???
//int val = p[currentCell] ???
}
}
}
}

You need to use the "FastDictionaryPropertyIndexer" or "FastPropertyIndexer" for regular properties.
foreach (var dictProp in grid.DictionaryProperties)
{
int numCellsI = dictProp.NumCellsIJK[0];
int numCellsJ = dictProp.NumCellsIJK[1];
int numCellsK = dictProp.NumCellsIJK[2];
float[] values = new float[dictProp.NumCells];
var dpsa = dictProp.SpecializedAccess;
using (var fdpi = dpsa.OpenFastDictionaryPropertyIndexer())
{
int index = 0;
for (int k = 0; k < numCellsK; k++)
{
for (int j = 0; j < numCellsJ; j++)
{
for (int i = 0; i < numCellsI; i++)
{
values[index] = fdpi[i, j, k];
index++;
}
}
}
}
}
You also need to be careful about the indexing since it varies by project. For instance, you may need to reverse the order of traversal in the J direction or you could end up with some strange results.

Related

Dijkstras Algorithmn Vue

I am trying to implement dijkstras pathfinding algorithm in vue. I am following the psuedocode from https://medium.com/#nicholas.w.swift/easy-dijkstras-pathfinding-324a51eeb0f. This is what I have come up with so far, however I am struggling to translate the psuedocode into vue js. How would I be able to check every node and pop the visited nodes off the list?
Dijkstra() {
this.unexploredset = [];
for (let i = 0; i < 16; i++){
for (let j = 0; j < 16; j++){
this.nodes[i][j].position = '∞';
this.nodes[i][j].distance = '∞';
if(this.nodes[i][j].hasWall == false){
this.unexploredset.push(this.nodes[i][j])
}
}
}
let current = this.nodes[3][4];
let goal = this.nodes[14][14];
for(let i = 0; i < 255; i++) {
for (let k = 0; k < 4; k++) {
if (current.distance <= current.neighbours[k].distance && current.unvisited == true)
{
current.unvisited = false;
let temp = current.neighbours[k];
current = temp
this.unexploredset.pop(current);
current = temp
if (current == goal)
{
console.log("found");
break
}
console.log(this.unexploredset.length)
}
}
}
}

How I can sort an ArrayList of int arrays?

I have this code. I'm dealing with the N-Queen problem.
The problem is when I wanna show results by screen, the arrays are not ordered. But in this code I can't order them using Comparator. It's very strange because in other Class it works perfectly using Comparator, but here it doesn't work. Hope anyone could help me. Thanks in advance.
import java.util.*;
public class NReinas {
public static void resolverReinas(int n){
String[][] tablero;
tablero = generarTablero(n);
ubicarReina(tablero, 0, n);
}
public static void ubicarReina(String[][] tablero, int etapa, int n){
ArrayList <int[]> resultados = new ArrayList<>();
for(int i = 0; i < tablero.length; i++){
if(isValido(tablero, i, etapa)){
tablero[i][etapa] = "R";
if(etapa < tablero.length - 1){
ubicarReina(tablero, etapa + 1, n); //Recursividad
}else {
resultados.add(devolverSolucion(tablero, n));
}
tablero[i][etapa] = " "; //Backtracking: vaciamos el tablero
}
}
//The ArrayList I want to order by int arrays
for (int[] r : resultados) {
System.out.println(Arrays.toString(r));
}
}
public static boolean isValido(String[][] tablero, int i, int etapa){
for(int x = 0; x < etapa; x++){
if(tablero[i][x].equals("R")){
return false;
}
}
for(int j = 0; j < tablero.length && (i-j) >= 0 && (etapa-j) >=0; j++){
if(tablero[i - j][etapa - j].equals("R")){
return false;
}
}
for(int j = 0; j < tablero.length && (i + j) < tablero.length && etapa - j >= 0; j++){
if(tablero[i + j][etapa - j].equals("R")){
return false;
}
}
return true;
}
public static String[][] generarTablero(int length){
String[][]res = new String[length][length];
for (int i = 0; i < res.length; i++) {
for (int j = 0; j < res.length; j++) {
res[i][j] = " ";
}
}
return res;
}
public static int[] devolverSolucion(String[][] tablero, int n){
int[] solucion = new int[n];
for (int i = 0; i < tablero.length; i++) {
for (int j = 0; j < tablero.length; j++) {
if(tablero[i][j] == "R"){
solucion[i] = j;
}
}
}
return solucion;
}
}
Try Using Integer instead of int and save array values on List instead, so you can use sort them
List<Integer> list = Arrays.asList(solucion);
Collections.sort(list);
If you insist in using and array you can reconverti the list to an array
(Integer[]) list.toArray();

ArrayList Method Returns a null ArrayList, main program cannot access

I'm trying to create a basic function that calls on a method that creates the 2D ArrayList that will be used further in the main program to do things like calculate the row and column sums as well as print out the triangle.
However, after it runs the ArrayList returns null. What's going on?
Thanks,
public class Trib
{
private ArrayList<ArrayList<Integer>> triangle;
private int Asize;
public Trib (int size)
{
// convert the argument to type 'int' to be used in the program
Asize = size;
// create an ArrayList of ArrayLists, it will have 'size' number ArrayLists contained within
ArrayList<ArrayList<Integer>> triangle = new ArrayList<ArrayList<Integer>>(size);
// create the inner ArrayLists
for (int i = 0; i < size; i++)
{
// add to index 'i' of our ArrayList a new ArrayList of size (i+1)
triangle.add(new ArrayList<Integer>(i+1));
for (int j = 0; j <= i; j++)
{
if (j==0 || j == i)
{
triangle.get(i).add(1);
}
else
triangle.get(i).add(triangle.get(i-1).get(j-1)+triangle.get(i-1).get(j));
System.out.print(triangle.get(i).get(j) + " ");
}
System.out.println();
}
triangle.clone();
}
public void printTriangle()
{
System.out.print(triangle.get(1).get(1));
/*for (int i = 0; i < Asize; i++)
{
for (int j = 0; j <= i; j++)
{
System.out.print(triangle.get(1).get(1) + " ");
}
System.out.println();
}*/
}
/*public Trib()
{
this(5);
}*/
/*public int Psize()
{
return triangle.size();
}
public ArrayList<Integer> sumRows()
{
ArrayList<Integer> row_sum = new ArrayList<Integer>(Asize);
for (int i = 0; i < Asize; i++)
{
for (int j = 0; j < i; j++)
{
row_sum.add(triangle.get(i).get(j));
}
}
return row_sum;
}
public ArrayList<Integer> sumCols()
{
ArrayList<Integer> col_sum = new ArrayList<Integer>(Asize);
for (int i = 0; i < Asize; i++)
{
for (int j = 0; j < i; j++)
{
col_sum.add(triangle.get(i).get(i));
}
}
return col_sum;
}*/
public static void main(String[] args)
{
if(args.length < 1)
{
System.err.println("Sorry, this program needs an integer argument.");
System.exit(1);
}
Trib pt = new Trib(Integer.parseInt(args[0]));
pt.printTriangle();
//ArrayList<Object> sum_rows = new ArrayList<Object>(pt.Psize());
// sum_rows.add;
System.out.println("\nHere are the sum of rows:");
//for (int i = 0; i < pt.Psize(); i++)
//System.out.println(sum_rows.get(i));
//ArrayList<Integer> sum_cols = new ArrayList<Integer>(pt.Psize());
System.out.println("\nHere are the sum of columns:");
//for (int i = 0; i < pt.Psize(); i++)
//System.out.printf("%-5d", sum_cols.get(i));
}
}
Watch out what's what you are doing: Notice that you have TWO variables named "triangle": The first one is an instance variable and the second is a local variable, which is the only one you have initialized.
My suggestion to avoid this common mistake is to pre-pend "this." to any use of what you intend must be an instance variable. And, if in doubt, if you use a development environment as Eclipse, press CTRL and click on your variable to navigate to the point where it is declared.

not able to assign value to list declare in model in mvc

My Model Question_Bank_Model:
public List<string> Options { get; set; }
My Controller
for (int i = 0; i < Question_IdLIst.Count; i++)
{
Question_Bank_Model Question = new Question_Bank_Model();
Question.Question = QuestionBal.FetchQuestion_ByQuestionID(Question_IdLIst[i]);
List<string> Options_Value = QuestionBal.FetchOption_ByQuestion(Question_IdLIst[i]);
for (int j = 0; j < Options_Value.Count ; j++)
{
Question.Options[j] = Options_Value.ElementAt(j);
}
q.Add(Question);
}
i am getting value in Options_Value in this list but it is throwing error on
Question.Options[j] = Options_Value.ElementAt(j); line
Error:Object reference not set to instance of object.
can any one help me???
Try this;
Question.Options = new List<string>();
for (int j = 0; j < Options_Value.Count ; j++)
{
Question.Options.Add(Options_Value.ElementAt(j));
}
Thanks!

image segmentation based on colors using k means, opencv [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to
1) Read a 3 channel image
2) Using K-Means method, create k different classes
3) I will tag pixels accordingly to show which class they belong to and store them in a matrix.
4) And after that I'm thinking using Connected Component Labeling to determine if they are in the same segment or not.
I'm new in opencv so I wanted to ask you for the implementation , some code snippet to to get me started.
Any help will be appreciated. Thank you
typedef struct {
int red;
int green;
int blue;
int b64;
int groupNo;
} IMAGE;
typedef struct _MEANS
{
int r, g, b;
int groupNo;
} point_t, *point;
Mat img = imread("C:\\253027.jpg",CV_LOAD_IMAGE_COLOR);
int clusterCount=5;
IMAGE **strct= {0};
strct = allocate_matrix(img.rows, img.cols);
int i,j,k;
if(! img.data )
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
for ( i = 0; i < img.rows; i++) {
for ( j = 0; j < img.cols; j++) {
strct[i][j].red = img.at<Vec3b>(i,j)[2];
strct[i][j].green = img.at<Vec3b>(i,j)[1];
strct[i][j].blue = img.at<Vec3b>(i,j)[0];
/* printf("%u",strct[i][j].red);
printf("%u",strct[i][j].green);
printf("%u",strct[i][j].blue);
*/
}
}
double *tempuz= (double *)malloc(sizeof(double) * clusterCount);
point_t* p = (point_t *)malloc(sizeof(point_t) * clusterCount);
for( k = 0; k < clusterCount; k++ )
{
p[k].r= rand()%255;
p[k].g=rand()%255;
p[k].b=rand()%255;
p[k].groupNo=k;
}
int max=0;
for (i = 0; i < img.rows; i++) {
for ( j = 0; j < img.cols; j++) {
for(k = 0; k < clusterCount; k++ ){
tempuz[k]= abs(strct[i][j].red - p[k].r)+abs(strct[i][j].green -p[k].g)+abs(strct[i][j].blue - p[k].b);
// cout<<"tempuz"<<tempuz[k]<<endl;
} strct[i][j].groupNo=min_element(tempuz,clusterCount);
// cout<<"grup no"<<strct[i][j].groupNo<<endl;
}
}
i=0;
int r=0,b=0,g=0,counter=0;
while(i<200){
r=0,b=0,g=0,counter=0;
for(k=0;k<clusterCount;k++){
for (i = 0; i < img.rows; i++) {
for ( j = 0; j < img.cols; j++) {
if(strct[i][j].groupNo==k){
r+=strct[i][j].red;
b+=strct[i][j].blue;
g+=strct[i][j].green;
counter++;
}
}
}
if(counter!=0){
p[k].r=r/counter;
p[k].b=b/counter;
p[k].g=g/=counter;
}
}
i++;
max=0;
for (i = 0; i < img.rows; i++) {
for ( j = 0; j < img.cols; j++) {
for(k = 0; k < clusterCount; k++ ){
tempuz[k]= abs(strct[i][j].red - p[k].r)+abs(strct[i][j].green -p[k].g)+abs(strct[i][j].blue - p[k].b);
} strct[i][j].groupNo=min_element(tempuz,clusterCount);
}
}
}//end of while
for(int k=0;k<clusterCount;k++){
for (int i = 0; i < img.rows; i++) {
for (int j = 0; j < img.cols; j++) {
if(strct[i][j].groupNo==k){
strct[i][j].red=p[k].r;
strct[i][j].blue=p[k].b;
strct[i][j].green=p[k].r;
}
}
}
}
for (int i = 0; i < img.rows; i++) {
for (int j = 0; j < img.cols; j++) {
img.at<Vec3b>(i,j)[2] = strct[i][j].red;
img.at<Vec3b>(i,j)[1] = strct[i][j].green;
img.at<Vec3b>(i,j)[0] = strct[i][j].blue;
}
}